OBJECT REFERENCE IS NOT SET TO AN INSTANCE OF AN OBJECT ERROR IN MY CODE

Tanuj Kumar

Total Post:134

Points:940
Posted by  Tanuj Kumar
C# 
C#
 1180  View(s)
Ratings:
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".
  1. ezra heywood

    Post:145

    Points:1019
    Re: Object Reference is not set to an instance of an object error in my code

    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);

            }

         }

    }

Answer

NEWSLETTER

Enter your email address here always to be updated. We promise not to spam!