Click here to Skip to main content
15,894,343 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Object reference not set to an instance of an object.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.

Source Error:

C++
Line 68:             TextBox txtEmail = (TextBox)gvDetails.Rows[e.RowIndex].FindControl("txtEmail");
Line 69:             con.Open();
Line 70:             SqlCommand cmd = new SqlCommand("update Employee_Details set PrinterID='" + txtPrinterID.Text + "',Name='" + txtName.Text + "',Department='" + txtDepartment.Text + "',Description='" + txtDescription.Text + "',Email='" + txtEmail.Text + "' where Id=" + Id, con);
Line 71:             cmd.ExecuteNonQuery();
Line 72:             con.Close();<pre>


line 70 is in red.
Posted
Comments
Bun Leap_kh 21-Oct-12 23:15pm    
Please check with your 'con' object first. Did you initialize it?

First of all I would like to suggest that Always make a habbit to use Parameterized Query. Regarding to your issue it may possible that CONNECTION is not established or the parameter ID is null.

Thanks
 
Share this answer
 
This error normally occurs when you are trying to access a member of an object that is null.
Try debugging your code and tracing out the object that is null.

One cause could be this line returns null - gvDetails.Rows[e.RowIndex].FindControl("txtEmail").
When you try and access the .Text property of this object you could be getting an error.
 
Share this answer
 
C#
protected void gvDetails_RowUpdating(object sender, GridViewUpdateEventArgs e)
       {
           int Id = Convert.ToInt32(gvDetails.DataKeys[e.RowIndex].Value.ToString());
           TextBox txtPrinterID = (TextBox)gvDetails.Rows[e.RowIndex].FindControl("txtPrinterID");
           string username = gvDetails.DataKeys[e.RowIndex].Values["Username"].ToString();
           TextBox txtName = (TextBox)gvDetails.Rows[e.RowIndex].FindControl("txtName");
           TextBox txtDepartment = (TextBox)gvDetails.Rows[e.RowIndex].FindControl("txtDepartment");
           TextBox txtDescription = (TextBox)gvDetails.Rows[e.RowIndex].FindControl("txtDescription");
           TextBox txtEmail = (TextBox)gvDetails.Rows[e.RowIndex].FindControl("txtEmail");
           con.Open();
           SqlCommand cmd = new SqlCommand("update Employee_Details set PrinterID='" + txtPrinterID.Text + "',Name='" + txtName.Text + "',Department='" + txtDepartment.Text + "',Description='" + txtDescription.Text + "',Email='" + txtEmail.Text + "' where Id=" + Id, con);
           cmd.ExecuteNonQuery();
           con.Close();
           lblresult.ForeColor = Color.Green;
           lblresult.Text = username + " Details Updated successfully";
           gvDetails.EditIndex = -1;
           BindEmployeeDetails();
       }
       protected void gvDetails_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
       {
           gvDetails.EditIndex = -1;
           BindEmployeeDetails();
       }<pre>



my code is this. is it wrong?
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900