Click here to Skip to main content
15,922,894 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more: , +
Hello

I am getting error in this code
C#
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            GridViewRow row = (GridViewRow)GridView1.Rows[e.RowIndex];
            Label lblID = (Label)row.FindControl("lblID");
            
            TextBox textName = (TextBox)row.FindControl("txtName");
            TextBox textDesc = (TextBox)row.FindControl("txtDesc");
            TextBox textImage = (TextBox)row.FindControl("txtImage");
            TextBox textActive = (TextBox)row.FindControl("txtActive");
            TextBox textCreatedBy = (TextBox)row.FindControl("txtCreatedBy");
            TextBox textCreatedDate = (TextBox)row.FindControl("txtCreatedDate");
            GridView1.EditIndex = -1;
            conn.Open();
            
            
            SqlCommand cmd = new SqlCommand("UPDATE tbl_SqlImage set Name=" + textName.Text + " , Description='" + textDesc.Text + " , Image='" + textImage.Text + " , Active='" + textActive.Text + " , CreatedBy='" + textCreatedBy.Text + " , CreatedDate='" +textCreatedDate.Text + "' where ID=" + lblID.Text + "", conn);
            
            
            cmd.ExecuteNonQuery();
            conn.Close();
            bind();
            //GridView1.DataBind();
            
        }

Please Help
ID is auto generating

I am getting null in ID
Please Help
Posted
Updated 21-Jun-12 0:33am
v3
Comments
[no name] 21-Jun-12 12:25pm    
Are we supposed to guess which line is throwing the error?

Object reference not set to an instance of an object

This error happens when you try to use a property or call a method of an object that is null. More details: here[^]

A simple use of Visual studio DEBUGGER can tell you the object because of which it is happening. Just look at the stack trace and put a debugger on that line. Check the objects of that line and see if any one is null and you are trying to use that objects property. Handle the same.

Here, there are lots of places starting from line 1 itself! any of the explicit cast might have raised it.
 
Share this answer
 
Comments
Prasad_Kulkarni 21-Jun-12 6:36am    
:) A +5!
You are trying to access an attribute of an object that is null.
More details: Click here[^]

A simple use of Visual studio DEBUGGER can tell you the cause.

Just look at the stack trace and put a debugger on that line where you're getting this error.

This will easily help you identify and solve such type of errors.
 
Share this answer
 
v2
C#
SqlCommand cmd = new SqlCommand("UPDATE tbl_SqlImage set Name='" + textName.Text + "' , Description='" + textDesc.Text + "' , Image='" + textImage.Text + "' , Active='" + textActive.Text + "' , CreatedBy='" + textCreatedBy.Text + "' , CreatedDate='" +textCreatedDate.Text + "' where ID='" + lblID.Text.Trim() + "' ", conn);
 
Share this answer
 
v2
Comments
Prasad_Kulkarni 21-Jun-12 6:37am    
Is it really gonna solve OP's problem?
Kamalkant(kk) 21-Jun-12 7:05am    
I think problem in lblID.Text value.
[no name] 21-Jun-12 12:24pm    
You could at the very least caution the OP about not using string concatenation to build SQL queries (SQL injection attacks).

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