title: Read data from DataGridView and save to database
body: <p>I want to save the data from datagrid to database, I used the following code.</p>
  
  <pre><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();
          }
  </code></pre>
  
  <p>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.</p>