Click here to Skip to main content
15,891,375 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
in second line i'm getting this error.
"Object reference not set to an instance of an object"
please help me out



C#
int index = Convert.ToInt32(e.CommandArgument);
                    var id = ((Label)GridView1.Rows[index].FindControl("lblid")).Text.Trim(); // in this line

                    SqlConnection con = new SqlConnection(connstring);
                    string qry = "Delete from ['ISB VAS Nodes$'] where ID = " + id + "";
                    SqlCommand com = new SqlCommand(qry, con);

                    // open connection
                    con.Open();
                    int numberDeleted = com.ExecuteNonQuery();

                    if (numberDeleted > 0)
                    {
                        Label2.Text = "Deleted";
                        binddata();
                    }
                    else
                    {
                        Label2.Text = "Not Deleted";
                    }

                    // close connection
                    con.Close();
                }
                catch (System.Data.SqlClient.SqlException ex)
                {
                    string msg = "Insert/Update Error:";
                    msg += ex.Message;
                    throw new Exception(msg);

                }

            }
Posted
Updated 24-Apr-13 0:48am
v2
Comments
[no name] 24-Apr-13 6:47am    
"lblid" is not found.
I.explore.code 24-Apr-13 6:50am    
did you put a breakpoint on that line and see whats null? unfortunately, CodeProject doesn't have the feature to put a breakpoint on code posted on here!
Thanks7872 24-Apr-13 6:51am    
Edit that line with Label id = (Label)GridView1.Rows[index].FindControl("lblid");
Rockstar_ 24-Apr-13 6:57am    
Check the lblid and the value of Index variable by putting break point.
Jameel VM 24-Apr-13 7:00am    
The error happens when the value of ((Label)GridView1.Rows[index].FindControl("lblid")).Text is null. Before executing the code check whether the text value is not null. for example if(((Label)GridView1.Rows[index].FindControl("lblid")).Text!=null)
{
your remaining code here
}

1 solution

I think mostly Jameel's comment would work, but better to do this check as well. Before taking the "Text" just see if the control is getting or not.

C#
if(((Label)GridView1.Rows[index].FindControl("lblid"))!=null)
{
}
 
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