0

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.

5
  • 1
    Which line does the exception occur on? Commented Feb 24, 2012 at 12:09
  • And when you break on the exception which values that it is using are null? Answer these questions and you have pretty much solved your problem. ;-) Commented Feb 24, 2012 at 12:11
  • Also, just to check. 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? Commented Feb 24, 2012 at 12:11
  • hello..previoulsy i used the if statement if(Passord.Text==ConfirmPassword.Text). But i got an error saying The name "Password" is not in the current context. So i used the function Find Control(). And now I put a breakpoint on the if Statement and all the field seem to have the values that i have inserted. so nothin is null. Commented Feb 24, 2012 at 12:17
  • Hello...and my textBox is "UserName" and not Username. Sorry..:) Commented Feb 24, 2012 at 12:21

4 Answers 4

1

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.

Sign up to request clarification or add additional context in comments.

Comments

0

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)

Comments

0

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.

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.