0

I am trying to validate a user input to a given string. I am able to do this in java with this code

        String username = request.getParameter("txtUsername");
        String password = request.getParameter("txtPassword"];)

   if (username.equals("john") && (password.equals("smith"))){

        out.println("Success");

    }
    else{

         out.println("validation failed");
     }

but it returns a NullReferenceException in C# using this same code.

       {

    }

    protected void btnLogin_Click(object sender, EventArgs e)
    {
        String username = Request.QueryString["txtUsername"] ?? "";
        String password = Request.QueryString["txtPassword"] ?? "";

        try {
        if(username.Equals("john") && (password.Equals("smith"))){

            lblLogin.Text = "Success";
            Response.Redirect("ModelProfile.aspx");
        }
        else

        {
            lblLogin.Text = "Failed";
             Response.Redirect("Login.aspx");
        }

        }
        catch
        {
            lblLogin.Text = "Please type in some valid credentials";

        }

    }

Here is the text boxes in the aspx page looks like:

            <div id="loginUsername">
                <asp:Label ID="lblUsername" runat="server" Text="Username:"></asp:Label>

                <asp:TextBox ID="txtUsername" runat="server"   CssClass="mytext"></asp:TextBox>
            </div>

        <div id="loginPassword">
            <asp:Label ID="lblPassword" runat="server" Text="Password:"></asp:Label>
            <asp:TextBox ID="txtPassword" runat="server" CssClass="mytext"></asp:TextBox>

        </div>
        <div id="loginButton">

            <asp:Button ID="btnLogin" runat="server" Text="Login" CssClass="button" OnClick="btnLogin_Click" />
            <asp:Label ID="lblLogin" runat="server" Text=""></asp:Label>

        </div>
    </div>

Please any ideas on how I can solve this will be appreciated. Thanks

4
  • Either username or password is most likely null, as a result of the Request.QueryString failing to succeed in finding a match. Put a breakpoint in ans step through - you'll be able to see where it's failing. Commented May 31, 2015 at 20:56
  • @Baldrick I did and it said username is null. this is clearly not null as i always type in a value Commented May 31, 2015 at 21:02
  • I would check the casing of "txtUsername". Does that exactly match the request string casing? Commented May 31, 2015 at 21:04
  • @Baldrick I have had a careful look at my code and I cant find any issues with the casing Commented May 31, 2015 at 21:12

1 Answer 1

2

You could easily fix your problem with

    String username = Request.QueryString["txtUsername"] ?? "";
    String password = Request.QueryString["txtPassword"] ?? "";

The ?? is the C# Null Coalescing operator

However looking at your code, it seems that you don't have to work with the QueryString because the button login is on the same page of your textboxes. If this is correct then you should reference the textbox directly. No need to check for null because the textbox text property is never null (if you really want to be very precise you could add a Trim)

    String username = txtUsername.Text.Trim();
    String password = txtPassword.Text.Trim();
Sign up to request clarification or add additional context in comments.

7 Comments

This will certainly treat the symptoms, but in his case he actually has a supplied username and password... :)
Then he has some other part of its code that is wrong. The only thing that comes to mind is a mispelled name
@Baldrick Yes something like that. In any case, a defensive attitude would suggest to leave the checks in place. You cannot predict the user behavior
@Steve I have tried that and it did stop the NullReferenceException error. but my code doesn't seem to do anything. it takes the input but doesnt redirect to the requested page. when I did a debug the value of username is null
@Buzz clearly, something is wrong elsewhere. Not in the code above. You could try to add the code of the Page_Load method of this page and the point where you redirect to this page. Just to have a more comprensive picture of your code.
|

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.