android
android

Reputation: 125

ASP.NET NullReferenceException

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.

Upvotes: 0

Views: 1603

Answers (4)

StuartLC
StuartLC

Reputation: 107247

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

if ((RegisterUserWizardStep.FindControl("Password") as TextBox).Text 

try

TextBox passwordTextBox = RegisterUserWizardStep.FindControl("Password") as TextBox;
// .. same for username and email
if ((passwordTextBox != null) && (usernameTextBox != null) ... )
{
 // Do something with the textboxes
}
// else you have a bug

This will also prevent you repeating the FindControl code on the same control (DRY principle)

Upvotes: 0

Martin Liversage
Martin Liversage

Reputation: 106826

Code like RegisterUserWizardStep.FindControl("UserName") as TextBox will return null either if there is no control named UserName or if the control named UserName can't be cast to a TextBox. This is most likely the source of your exception because you attempt to get the property Text of a reference that might be null.

To better understand where the problem is you can define an extension function:

static class ControlExtensions {

  public T Find(this Control parent, String name) where T : Control {
    var control = parent.FindControl(name);
    if (control == null)
      throw new ArgumentException(String.Format("Cannot find control named '{0}'.", name);
    var t = control as T;
    if (t == null)
      throw new ArgumentException(String.Format("Control named '{0}' does not have type '{1}.", name, typeof(T).Name);
    return t;
  }

}

You can then get the Text property of the UserName control:

RegisterUserWizardStep.Find<TextBox>("UserName").Text

This call will throw a more descriptive exception if the control isn't found.

Upvotes: 0

Jon Egerton
Jon Egerton

Reputation: 41549

In your description you've said that you have a Username TextBox.

The code is looking for RegisterUserWizardStep.FindControl("UserName").

Is this a typo in the question? otherwise it could be the cause of the exception.

Upvotes: 0

dash
dash

Reputation: 91480

You mention there are four controls - Username, Email, Password and ConfirmPassword

The null exception you are seeing is almost certainly because FindControl(X) is returning null

A better way of checking is to do something like:

TextBox myTextBox = RegisterUserWizardStep.FindControl(X) as TextBox;

if(myTextBox != null){
  //Continue
}
else{
  //Write out some error information - now you know what the problem is.
}

Further, and this isn't related to your immediate error, but then you feed the contents of each of the text boxes directly into your ExecuteInsert method - you'd be better off doing some validation, too, just to check you have expected values.

Upvotes: 1

Related Questions