Prabhat 7154it

Prabhat 7154it

  • NA
  • 61
  • 8.6k

Fix: Object reference not set to instance of object

Dec 2 2019 3:02 AM
 The exception is thrown at line if (bool.Parse(row.Cells[0].Value.ToString())).
 Exception details:
System.NullReferenceException: 'Object reference not set to an instance of an object.'
System.Windows.Forms.DataGridViewCell.Value.get returned null.
private void btnDeleteCategory_Click(object sender, EventArgs e)
{
foreach (DataGridViewRow row in CategoryGV.Rows)
{
if (bool.Parse(row.Cells[0].Value.ToString()))
{
string cs = ConfigurationManager.ConnectionStrings["aladin"].ConnectionString;
SqlConnection con = new SqlConnection(cs);
SqlCommand cmd = new SqlCommand("DELETE FROM lwpos_categories WHERE id= '" + Convert.ToInt32(row.Cells[1].Value) + "' ", con);
con.Open();
cmd.ExecuteNonQuery();
con.Close();
}
}
MessageBox.Show("Deleted Successfully", "Successfull operation", MessageBoxButtons.OK, MessageBoxIcon.Information);
}

Answers (2)