Object Reference is not set to an instance of an object error in my code

Total Post:134

Points:940
C# 
C#
 1417  View(s)
Ratings:
Rate this:
protected void ImageButton_enable_Click(object sender,ImageClickEventArgs e)    
    {
        foreach(GridViewRow gvrow in GridView_enable.Rows)
        {
            CheckBoxchk1=(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. 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);
            }
         }
    }

      Modified On Apr-03-2018 06:02:10 AM

Answer

NEWSLETTER

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