i trying create connection c# sql server , nullreferenceexception
occurred.
i have tried may times didn't found cure problem. says error occurred @ line 3
. think maybe problem of connection between sql server , c# code.
here code of c#:
protected void button1_click(object sender, eventargs e) { int userid = 0; string constr = configurationmanager.connectionstrings["constr"].connectionstring; using (sqlconnection con = new sqlconnection(constr)) { using (sqlcommand cmd = new sqlcommand("insert_user")) { using (sqldataadapter sda = new sqldataadapter()) { cmd.commandtype = commandtype.storedprocedure; cmd.parameters.addwithvalue("@username", textboxun.text.trim()); cmd.parameters.addwithvalue("@email", textboxemail.text.trim()); cmd.parameters.addwithvalue("@password", textboxpswd.text.trim()); cmd.connection = con; con.open(); userid = convert.toint32(cmd.executescalar()); con.close(); } } string message = string.empty; switch (userid) { case -1: message = "username exists.\\nplease choose different username."; break; case -2: message = "supplied email address has been used."; break; default: message = "registration successful.\\nuser id: " + userid.tostring(); break; } clientscript.registerstartupscript(gettype(), "alert", "alert('" + message + "');", true); }
html code of login form below:
<table class="auto-style1"> <tr> <td class="auto-style2">username</td> <td class="auto-style3"> <asp:textbox id="textboxun" runat="server" ontextchanged="textboxun_textchanged" width="180px"></asp:textbox> </td> <td> <asp:requiredfieldvalidator id="label1" runat="server" controltovalidate="textboxun" errormessage="username required" forecolor="red"></asp:requiredfieldvalidator> </td> </tr> <tr> <td class="auto-style4"> </td> <td class="auto-style5"> </td> <td class="auto-style6"> </td> </tr> <tr> <td class="auto-style2">e-mail</td> <td class="auto-style3"> <asp:textbox id="textboxemail" runat="server" width="180px"></asp:textbox> </td> <td> <asp:requiredfieldvalidator id="requiredfieldvalidator3" runat="server" controltovalidate="textboxemail" errormessage="e-mail required" forecolor="red"></asp:requiredfieldvalidator> <br /> <asp:regularexpressionvalidator id="regularexpressionvalidator1" runat="server" controltovalidate="textboxemail" errormessage="e-mail entered not correct" forecolor="red" validationexpression="\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*"></asp:regularexpressionvalidator> </td> </tr> <tr> <td class="auto-style2">password</td> <td class="auto-style3"> <asp:textbox id="textboxpswd" runat="server" textmode="password" width="180px"></asp:textbox> </td> <td> <asp:requiredfieldvalidator id="requiredfieldvalidator4" runat="server" controltovalidate="textboxpswd" errormessage="password required" forecolor="red"></asp:requiredfieldvalidator> </td> </tr> <tr> <td class="auto-style2">confirm-password</td> <td class="auto-style3"> <asp:textbox id="textboxrpswd" runat="server" textmode="password" width="180px"></asp:textbox> </td> <td> <asp:requiredfieldvalidator id="requiredfieldvalidator5" runat="server" controltovalidate="textboxrpswd" errormessage="confirm password" forecolor="red"></asp:requiredfieldvalidator> <br /> <asp:comparevalidator id="comparevalidator1" runat="server" controltocompare="textboxpswd" controltovalidate="textboxrpswd" errormessage="enter correct password" forecolor="red"></asp:comparevalidator> </td> </tr> <tr> <td class="auto-style7"></td> <td class="auto-style8"></td> <td class="auto-style9"></td> </tr> <tr> <td class="auto-style2"> </td> <td class="auto-style3"> </td> <td> </td> </tr> </table> <asp:button id="button1" runat="server" borderstyle="ridge" onclick="button1_click" text="register" /> <input id="reset1" type="reset" value="reset" />
i guess error because of sqldataadapter, didn't appointed anything. also, don't need sqldataadapter 1 value. change code to:
int userid = 0; string constr = configurationmanager.connectionstrings["constr"].connectionstring; using (sqlconnection con = new sqlconnection(constr)) { using (sqlcommand cmd = new sqlcommand("insert_user", con)) { cmd.commandtype = commandtype.storedprocedure; cmd.parameters.addwithvalue("@username", textboxun.text.trim()); cmd.parameters.addwithvalue("@email", textboxemail.text.trim()); cmd.parameters.addwithvalue("@password", textboxpswd.text.trim()); try { con.open(); userid = convert.toint32(cmd.executescalar().tostring()); } catch(exception ex) { response.write("<script language='javascript'>alert('" + ex.message.tostring() + "');</script>");//it throw alert exception } { con.close(); } } string message = string.empty; switch (userid) { case -1: message = "username exists.\\nplease choose different username."; break; case -2: message = "supplied email address has been used."; break; default: message = "registration successful.\\nuser id: " + userid.tostring(); break; } clientscript.registerstartupscript(gettype(), "alert", "alert('" + message + "');", true); }
hope helps
Comments
Post a Comment