0

I have a datagridview in c# that i am filling from a mysql database. The user reviews the info and then saves it to a different mysql database. I know the connections to both work and like my title says I'm getting a null exception on the first string in the for loop.

there are only 6 columns in the row and the only thing i can think of is i am looking at rows and not columns but not sure what the code should be for that edit I changed it as most had said to < instead of <= and I know why i get the error but don't know how to get the information i need. The error is coming from the fact that the cell shows as null even though there is a value in the cell.

string ConnectionString2 = ConfigurationSettings.AppSettings["ConnectionString2"];
MySqlConnection connection2;
connection2 = new MySqlConnection(ConnectionString2);

int i;
for (i = 0; i < dataGridView1.Rows.Count; i++)
{
    string day = dataGridView1.Rows[i].Cells[0].Value.ToString();
    string Desc = dataGridView1.Rows[i].Cells[1].Value.ToString();
    string item = dataGridView1.Rows[i].Cells[2].Value.ToString();
    string prod = dataGridView1.Rows[i].Cells[3].Value.ToString();
    string vol = dataGridView1.Rows[i].Cells[4].Value.ToString();
    string qty = dataGridView1.Rows[i].Cells[5].Value.ToString();

    MySqlCommand cmdwk = new MySqlCommand("INSERT INTO spt_proposal_line_lab (proposal_Id,day_Name,proposal_Desc,proposal_Vol,product_Id,proposal_Qty,item_Id) VALUES (@propid,@day,@desc,@vol,@prod,@qty,@item)", connection2);
    MySqlParameter propid = new MySqlParameter("@propid", b);
    MySqlParameter day1 = new MySqlParameter("@day", day);
    MySqlParameter Desc1 = new MySqlParameter("@desc", Desc);
    MySqlParameter vol1 = new MySqlParameter("@vol", vol);
    MySqlParameter prod1 = new MySqlParameter("@prod", prod);
    MySqlParameter qty1 = new MySqlParameter("@qty", qty);
    MySqlParameter item1 = new MySqlParameter("@item", item);

    cmdwk.Parameters.Add(day1);
    cmdwk.Parameters.Add(propid);
    cmdwk.Parameters.Add(Desc1);
    cmdwk.Parameters.Add(prod1);
    cmdwk.Parameters.Add(vol1);
    cmdwk.Parameters.Add(qty1);
    cmdwk.Parameters.Add(item1); 
}

The datagrid is called and filled in the load this is done on a button Edit Seeing I am one point away from my img post ability I will try to explain this more When the button is clicked and this code runs the error comes up at

string day = dataGridView1.Rows[i].Cells[0].Value.ToString();

the error is

Object reference not set to an instance of an object.

System.NullReferenceException was unhandled
  HResult=-2147467261
   Message=Object reference not set to an instance of an object.
    Source=SpectLabRemake2
    StackTrace:
   at SpectLabRemake2.Form6.btn_Save_Click(Object sender, EventArgs e) 
7
  • 2
    Can't you just debug and see that is null? Also you loop looks wrong, should be for (i = 0; i < dataGridView1.Rows.Count; i++) (strictly less than) Commented Mar 27, 2012 at 21:50
  • Check that dataGridView1.Rows[i].Cells[0].Value is filled with something ... Commented Mar 27, 2012 at 21:50
  • 1
    Apart from the fact that cmdwk is never executed, which line gives the null exception? Commented Mar 27, 2012 at 21:51
  • the strings show the right info in them in debug but its comes up as System.NullReferenceException was unhandled Message=Object reference not set to an instance of an object. on the first string day but if hover over it or any other of the strings it shows the right information? Commented Mar 27, 2012 at 21:53
  • 1
    You can set a breakpoint where the exception is raised and then inspect all relevant values or get them from the quick-watch window. Commented Mar 27, 2012 at 21:56

2 Answers 2

1

I think it's this line:

for (i = 0; i <= dataGridView1.Rows.Count; i++)

You are going one row too far. Try:

for (i = 0; i < dataGridView1.Rows.Count; i++)
Sign up to request clarification or add additional context in comments.

2 Comments

I did try that and still have the same issue as my edit says the cell says it null but it has a value
You didn't include the EXACT text of the exception (and you should do so). You may be thinking you have a null value when you really have a null reference. And you haven't indicated what line the error occurs on. Don't add any more comments until you have edited your original post and included the information.
0

Well for everyone that has this problem I will post my code that fixed the issue and Thank YOU soo much to everyone that was SOOOO helpful here with this issue.

  for (i = 0; i < dataGridView1.Rows.Count-1; i++)
        {

            string ConnectionString2 = ConfigurationSettings.AppSettings["ConnectionString2"];
            MySqlConnection connection2;








            connection2 = new MySqlConnection(ConnectionString2);
            connection2.Open();


                string day = dataGridView1.Rows[i].Cells[0].Value.ToString();
                string Desc = dataGridView1.Rows[i].Cells[1].Value.ToString();
                string item = dataGridView1.Rows[i].Cells[2].Value.ToString();
                string prod = dataGridView1.Rows[i].Cells[3].Value.ToString();
                string vol = dataGridView1.Rows[i].Cells[4].Value.ToString();
                string qty = dataGridView1.Rows[i].Cells[5].Value.ToString();


                MySqlCommand cmdwk = new MySqlCommand("INSERT INTO spt_proposal_line_lab (proposal_Id,day_Name,proposal_Desc,proposal_Vol,product_Id,proposal_Qty,item_Id) VALUES (@propid,@day,@desc,@vol,@prod,@qty,@item)", connection2);
                MySqlParameter propid = new MySqlParameter("@propid", b);
                MySqlParameter day1 = new MySqlParameter("@day", day);
                MySqlParameter Desc1 = new MySqlParameter("@desc", Desc);
                MySqlParameter vol1 = new MySqlParameter("@vol", vol);
                MySqlParameter prod1 = new MySqlParameter("@prod", prod);
                MySqlParameter qty1 = new MySqlParameter("@qty", qty);
                MySqlParameter item1 = new MySqlParameter("@item", item);

                cmdwk.Parameters.Add(day1);
                cmdwk.Parameters.Add(propid);
                cmdwk.Parameters.Add(Desc1);
                cmdwk.Parameters.Add(prod1);
                cmdwk.Parameters.Add(vol1);
                cmdwk.Parameters.Add(qty1);
                cmdwk.Parameters.Add(item1);
                cmdwk.ExecuteNonQuery();



            }
        }

    }
}

Brent

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.