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)
(i = 0; i < dataGridView1.Rows.Count; i++)(strictly less than)