Home > DeveloperSection > Forums > Object Reference is not set to an instance of an object error in my code
Tanuj Kumar

Total Post:134

Points:940
Posted on    November-14-2013 3:17 AM

 C# C# 
Ratings:


 1 Reply(s)
 860  View(s)
Rate this:

protected void ImageButton_enable_Click(object sender, ImageClickEventArgs e)   

    {

        foreach(GridViewRow gvrow in GridView_enable.Rows)

        {

            CheckBox chk1=(CheckBox) gvrow.FindControl("CheckBox_select");

            if (chk1.Checked == true)

            {

                Label lblEmail = (Label)gvrow.FindControl("Label1");

                string email = lblEmail.Text;

            }

         }          

    }

What is wrong in my code? i am getting this error "Object Reference is not set to an instance of an object".


ezra heywood
ezra heywood

Total Post:145

Points:1019
Posted on    November-14-2013 4:52 AM

Hi Tanuj,

You should do like this:

protected void ImageButton_enable_Click(object sender, ImageClickEventArgs e)

{

    foreach(GridViewRow gvrow in GridView_enable.Rows)

    {

        CheckBox chk1= gvrow.FindControl("CheckBox_select") as CheckBox;

        if (chk1 != null && chk1.Checked == true)

        {

            Label lblEmail = gvrow.FindControl("Label1") as Label;

            if (lblEmail != null)

                 Console.WriteLine(lblEmail.Text);

        }

     }

}


Don't want to miss updates? Please click the below button!

Follow MindStick