Can you please let me know why the following piece of code is not working, I am getting the error message when the debugger past the variable “strStatus”. The error message is: “Object reference not set to an instance of an object.” Can you please help. Thanks – Yagya
protected void Button1_Click(object sender, EventArgs e)
{
if (Y0130_chkNew.Checked == true)
{
bool isChecked = true; // This is required for later retrieval.
string strAction = "Reporting";
string strFromRole = ddSelectRole.SelectedValue;
string TxtBoxID = myProject.getTextBox(strAction, strPath);
TextBox txtb = new TextBox();
txtb = (TextBox)Page.FindControl(TxtBoxID);
string strStatus = txtb.Text;
string ddID = myProject.getDropDown(strAction, strPath);
DropDownList ddLst = new DropDownList;
ddLst = (DropDownList)Page.FindControl(ddID);
string strForwardRole = ddLst.SelectedValue;
// Call the function now
my.updateXML(strFromRole, strAction, strStatus, strForwardRole, strPath);
}
}
Page.FindControl(TxtBoxID);returns null what is the reason for your exception.FindControldoes not search controls recursively, only in the givenNamingContainer.If the control is not nested in another
NamingContaineron the page(f.e. aGridViewRow), there’s no reason at all to useFindControlsince you could reference it directly:If you’re using
MasterPagestheNamingContainerof controls in theContentPageis not the page but theContenPlaceHolder:Cannot find TextBox on Page