I am trying to write the user login details to the Database.
When I click the submit button Im getting a NullReferenceException.
There are 4 TextBoxes
Username, Email, Password and ConfirmPassword.
protected void Button1_Click(object sender, EventArgs e)
{
if ((RegisterUserWizardStep.FindControl("Password") as TextBox).Text == (RegisterUserWizardStep.FindControl("ConfirmPassword") as TextBox).Text)
{
//call the method to execute insert to the database
ExecuteInsert((RegisterUserWizardStep.FindControl("UserName") as TextBox).Text,
(RegisterUserWizardStep.FindControl("Email") as TextBox).Text,
(RegisterUserWizardStep.FindControl("Password") as TextBox).Text);
Response.Write("Record was successfully added!");
ClearControls(Page);
}
else
{
Response.Write("Password did not match");
(RegisterUserWizardStep.FindControl("Password") as TextBox).Focus();
}
}
Thank you.
Its likely that FindControl didn’t find the control you are after, possibly because the TextBoxes are nested under another child control like a panel etc.
Instead of
try
This will also prevent you repeating the FindControl code on the same control (DRY principle)