2
Reply

object reference is not set to an instance of an object

Viswa Kousika

Viswa Kousika

Dec 9 2016 3:33 AM
248
hi, good afternoon
 
i am getting null reference error "(object reference is not set to an instance of an object)"...
this is my code..........
try
{
string ConStr = ConfigurationManager.ConnectionStrings["MYDB"].ToString();
SqlConnection con = new SqlConnection(ConStr);
con.Open();
for (int i = 0; i < dataGridView1.Rows.Count - 1; i++)
{

string sql = "INSERT INTO [MyAssignment].[dbo].[Tblcustomer]([custname],[custtype],[address],[phonenumber],[emailid],[active])"
+ "VALUES('" + dataGridView1.Rows[i].Cells["Column2"].Value.ToString() + "','"
+ dataGridView1.Rows[i].Cells["Column3"].Value.ToString() + "','"
+ dataGridView1.Rows[i].Cells["Column4"].Value.ToString() + "',"
+ Convert.ToInt32(dataGridView1.Rows[i].Cells["Column5"].Value.ToString()) + ",'"
+ dataGridView1.Rows[i].Cells["Column6"].Value.ToString() + "','"
+ dataGridView1.Rows[i].Cells["Column7"].Value.ToString() + "')";
SqlCommand cmd = new SqlCommand(sql, con);
cmd.ExecuteNonQuery();

}
MessageBox.Show("Inserted");
}

catch (Exception ex)
{
MessageBox.Show(ex.Message.ToString());


Answers (2)