Click here to Skip to main content
15,891,567 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
I'm getting Null reference exception.



C#
protected void gvDetails_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
Label s_no = (Label)gvDetails.Rows[e.RowIndex].FindControl("s_no");
TextBox project_name =(TextBox)gvDetails.Rows[e.RowIndex].FindControl("project_name");
TextBox r_name = (TextBox)gvDetails.Rows[e.RowIndex].FindControl("r_name");
TextBox rel_start_date=(TextBox)gvDetails.Rows[e.RowIndex].FindControl("Release_start_date");
TextBox rel_end_date =(TextBox)gvDetails.Rows[e.RowIndex].FindControl("Release_EndDate");         
 con.Open();
            
MySqlCommand cmd = new MySqlCommand("update lut_release_name1 set project_name='" + project_name.Text + "',r_name='" + r_name.Text + "',Release_start_date='" + rel_start_date.Text + "',Release_EndDate='" + rel_end_date.Text + "' where s_no=" + Convert.ToInt32(s_no.Text), con);
            
cmd.ExecuteNonQuery();
                
gvDetails.EditIndex = -1;
con.Close();
BindEmployeeDetails();
        }
Posted
Comments
Richard Deeming 20-Apr-15 10:40am    
Your code is vulnerable to SQL Injection[^].

NEVER use string concatenation to build a SQL query. ALWAYS use a parameterized query.
Deepak Kanswal Sharma 20-Apr-15 23:33pm    
Where is the connection string ????????? "con"
ConnectingKamlesh 21-Apr-15 3:48am    
on which line are you getting null reference exception?

You don't have a Connection object defined and you are trying to open and close a connection. Instantiate the proper object and assign it its proper values.

SqlConnection con= new SqlConnection("your connections string");
 
Share this answer
 
v3
Comments
sunnykvinod 21-Apr-15 2:22am    
i have sqlconnection declared Outside that is not the problem
Richard C Bishop 21-Apr-15 8:13am    
Fair enough, but for future reference, it is helpful to put the whole context of the exception into perspective. From what you posted and said, my solution is the obvious answer, as is eluded to in the comment section above. Take care!
I guess you need to do this:-

C#
protected void gvDetails_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {


SqlConnection con =new SqlConnection("@data source="connection"; initial catalog=database name; intigrated security=true");

con.open();

-------- Your Code-----------------

con.Close();
}
 
Share this answer
 
Control name was wrong that leads to the NullReferenceException.
 
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