object reference not set.

Posted by Gayathri under ASP.NET on 4/8/2010 | Views : 2236 | Status : [Member] | Replies : 3
protected void chkInTheatres_CheckedChanged1(object sender, EventArgs e)
{
CheckBox cb = (CheckBox)FindControl("chkInTheatres");
if (cb.Checked == true) ---> raises error here "Object reference not set"
{
Response.Write("InTheatres");
}
else
{
Response.End();

}

}

even if i check (if cb != null) simply says it is null for everything.

please help.




Responses

Posted by: Raja on: 4/8/2010 [Member] Starter

Up
0
Down
Looks like following code is not working

CheckBox cb = (CheckBox)FindControl("chkInTheatres"); 


I think you should write in following ways

CheckBox cb = (CheckBox)this.Page.FindControl("chkInTheatres");


Have you kept this code into user control or page? if in user control then use this.FindControl if page then this.Page.FindControl if in master page then this.page.Master.FindControl



Regards,
Raja, USA

Gayathri, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Pavanandey on: 4/9/2010 [Member] Bronze

Up
0
Down


the event which ur using is directly refering to the check box so u need not go for findcontrol method u can directly use this

<asp:CheckBox ID="CB1" runat="server" oncheckedchanged="CB1_CheckedChanged" />

protected void CB1_CheckedChanged(object sender, EventArgs e)
{
bool status = CB1.Checked;
}

Thanks
Pavan Kumar
Mark Answer if this fits the need

Gayathri, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Sabarees on: 4/15/2010 [Member] Starter

Up
0
Down
Hi Gayathri,

Use the below code into your CheckedChanged event,

protected void chkInTheatres_CheckedChanged1(object sender, EventArgs e)
{
CheckBox cb = (CheckBox)sender;

if (cb.Checked == true)
{
Response.Write("InTheatres");
}
else
{
Response.End();
}
}

Best Ragards,
Sabarees M

Gayathri, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response