Click here to Skip to main content
15,917,538 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
writing this code on gridview_rowupdating event a error is comming object reference not set to instance of object.why?
my code is as follows:
C#
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        GridViewRow row = GridView1.Rows[e.RowIndex];
        string constr = ConfigurationManager.ConnectionStrings["constr"].ToString();
        SqlConnection conn = new SqlConnection(constr);

        string id = (((Label)row.FindControl("lblid")).Text).ToString();

        string comments = (((TextBox)row.FindControl("txtcomments")).Text).ToString();
        conn.Open();
        SqlCommand CmdSql = new SqlCommand("Update leave set admin_comments='" + comments + "', granted='" + true + "' where id=" + id + " ", conn);
        CmdSql.ExecuteNonQuery();
        GridView1.EditIndex = -1;
        conn.Close();

Also it is not updating any row why?
Posted
Updated 5-Jun-13 22:07pm
v2
Comments
ridoy 6-Jun-13 4:10am    
do you check your database isn't null?
Rambo_Raja 6-Jun-13 4:14am    
yes i hv checked it.its not null.

1 solution

Please debug your code and find out on which line you are getting that exception.

Some Points

  1. This may throw exception, if there is no connection string names as "constr" in the web.config.
    C#
    string constr = ConfigurationManager.ConnectionStrings["constr"].ToString();

    So, you should read it like below...
    C#
    if(ConfigurationManager.ConnectionStrings["constr"] != null 
        &&   
       !String.IsNullOrEmpty(Convert.ToString(ConfigurationManager.ConnectionStrings["constr"]))
      )
    {
        string constr = ConfigurationManager.ConnectionStrings["constr"].ToString();
    }


  2. Extra .ToString() called, not necessary.
    C#
    string id = (((Label)row.FindControl("lblid")).Text).ToString();

    Here .Text gives you a string, then why you again call .ToString(), which is not needed.
  3. Here you are passing the id as a string. Please check what is the field type in Table.
    Identity fields are mostly integer field, in that case it will throw exception.
    Then you need to convert that id to integer and then pass in the query.
    C#
    SqlCommand CmdSql = new SqlCommand("Update leave set admin_comments='" + comments + "', granted='" + true + "' where id=" + id + " ", conn);
 
Share this answer
 
Comments
Rambo_Raja 6-Jun-13 5:35am    
it still not working. same error..object ref not set to instance of object even when i have set it like CmdSql.Connection = conn. my question is very simple what code i should write on gridview rowupdating event so update occur.
You can refer one nice article - Asp.net insert, Edit, update, delete data in gridview.

But I would suggest you to find the problem by debugging each line and find out on which line exactly it is giving the error.
Rambo_Raja 6-Jun-13 6:31am    
CmdSql.ExecuteNonQuery().showing error:Must declare the scalar variable "@EmpId".
Can you post the cmdSql here? Just after line...

SqlCommand CmdSql = new SqlCommand("Update leave set admin_comments='" + comments + "', granted='" + true + "' where id=" + id + " ", conn);

Check what is the value of CmdSql by debugger.

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