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
usernameorpasswordis most likely null, as a result of theRequest.QueryStringfailing to succeed in finding a match. Put a breakpoint in ans step through - you'll be able to see where it's failing."txtUsername". Does that exactly match the request string casing?