I want to save the data from datagrid to database, I used the following code.
foreach (DataGridViewRow r in dataGridView_displaycount.Rows)
{
SqlCommand cmd = new SqlCommand("insert into StocktransferlocationDetails(date,stocktransferlocation,Itemcode,Count,Description) values(@date,@stocktransferlocation,@Itemcode,@Count,@Description) ", con);
cmd.Parameters.AddWithValue("@date",System.DateTime.Now);
cmd.Parameters.AddWithValue("@stocktransferlocation", comboBox_locationfrom.SelectedText+"-"+comboBox_locationto.SelectedText);
cmd.Parameters.AddWithValue("@Itemcode", r.Cells[1].Value.ToString());
cmd.Parameters.AddWithValue("@Count", r.Cells[2].Value.ToString());
cmd.Parameters.AddWithValue("@Description", r.Cells[3].Value.ToString());
cmd.ExecuteNonQuery();
}
Here my problem is the datagrid contains the empty row at the last. So I write the data and when it comes to the empty row it throws me an error.