java - Validating user input from textbox c# -
i trying validate user input given string. able in java 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 returns nullreferenceexception in c# using 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 valid credentials"; } }
here text boxes in 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 ideas on how can solve appreciated. thanks
you fix problem with
string username = request.querystring["txtusername"] ?? ""; string password = request.querystring["txtpassword"] ?? "";
the ?? c# null coalescing operator
however looking @ code, seems don't have work querystring because button login on same page of textboxes. if correct should reference textbox directly. no need check null because textbox text property never null (if want precise add trim)
string username = txtusername.text.trim(); string password = txtpassword.text.trim();
Comments
Post a Comment