pethu raj

pethu raj

  • NA
  • 41
  • 3.8k

Object reference not set to an instance of an object.

Oct 1 2017 2:21 PM
Hi,
How can i solve the error "Object reference not set to an instance of an object." .
for the below code.
The error throws in RowUpdating line
cmd.Parameters.AddWithValue("@p1", NAME.Text)
Kindly give a solution for this
 
 
protected void getdata()
{
string s = "Select * from Empdetail";
cmd = new SqlCommand(s, conn);
cmd.CommandType = CommandType.Text;
da = new SqlDataAdapter(cmd);
ds = new DataSet();
da.Fill(ds);
singlegrid.DataSource = ds;
singlegrid.DataBind();
}

protected void singlegrid_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
singlegrid.PageIndex = e.NewPageIndex;
getdata();
}

protected void singlegrid_RowEditing(object sender, GridViewEditEventArgs e)
{
singlegrid.EditIndex = e.NewEditIndex;
getdata();
}

protected void singlegrid_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
string bid = singlegrid.DataKeys[e.RowIndex].Value.ToString();
string s = "delete from Empdetail where EMPID=@p2";
SqlCommand cmd = new SqlCommand(s, conn);
cmd.Parameters.AddWithValue("@p2", bid);
conn.Open();
cmd.ExecuteNonQuery();
conn.Close();
getdata();
}

protected void singlegrid_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
singlegrid.EditIndex = -1;
getdata();
}

protected void singlegrid_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
string bid = singlegrid.DataKeys[e.RowIndex].Value.ToString();
TextBox NAME = (TextBox)singlegrid.Rows[e.RowIndex].FindControl("NAME");
TextBox DESIG = (TextBox)singlegrid.Rows[e.RowIndex].FindControl("DESIG");
string s = "update Empdetail set NAME=@p1, DESIG=@p2 where EMPID=@p3";
cmd = new SqlCommand(s, conn);
cmd.CommandType = CommandType.Text;
cmd.Parameters.AddWithValue("@p1", NAME.Text);
cmd.Parameters.AddWithValue("@p2", DESIG.Text);
cmd.Parameters.AddWithValue("@p3", bid);
conn.Open();
cmd.ExecuteNonQuery();
conn.Close();
singlegrid.EditIndex = -1;
getdata(); 

Answers (1)