c# - This insert query in webforms is throwing an error and I'm not sure why -
it's web forms application , i'm having trouble finding out going wrong.
i trying insert info logins
table.
everything works apart when add [user]
username of person logged in @ time. there entry username that's working fine.
the aspx.cs code behind:
protected void add_click(object sender, eventargs e) { string currentuser = user.identity.name; int webid = convert.toint32(ddlwebsite.selecteditem.value); string username = username.text; string _email = email.text; string pass = password.text; string ainfo = info.text; string loguser = user.identity.name; //string connectionstring = "data source=(localdb)\\mssqllocaldb;attachdbfilename=|datadirectory|\aspnet-webprog-20170720092452.mdf;initial catalog=aspnet-webprog-20170720092452;integrated security=true"; sqlconnection connection = new sqlconnection(configurationmanager.connectionstrings["defaultconnection"].tostring()); string query = "insert logins(websiteid, username, password, additionalinfo, user, email) values ( @webid, @username, @pass, @ainfo, @loguser, @_email)"; sqlcommand insertquery = new sqlcommand(query,connection); insertquery.parameters.addwithvalue("@webid", webid); insertquery.parameters.addwithvalue("@username", username); insertquery.parameters.addwithvalue("@pass", pass); insertquery.parameters.addwithvalue("@ainfo", ainfo); insertquery.parameters.addwithvalue("@loguser", user.identity.name); insertquery.parameters.addwithvalue("@_email", _email); connection.open(); insertquery.executenonquery(); connection.close(); }
the aspx markup:
add login
<div id="websitedetails" class="col-md-4"> <label id="label1" for="websitename"> website name: <asp:sqldatasource id="sqldatasource1" runat="server" connectionstring="<%$ connectionstrings:defaultconnection %>" selectcommand="select [id], [name] [websitelist]"></asp:sqldatasource> <asp:dropdownlist id="ddlwebsite" runat="server" datasourceid="sqldatasource1" datatextfield="name" datavaluefield="id"></asp:dropdownlist> <asp:button id="button1" runat="server" text="addawebsite" /> </label> <label id="label3" for="username"> username/email: <asp:textbox id="username" runat="server"></asp:textbox> </label> <label id="label4" for="password"> password: <asp:textbox id="password" runat="server"></asp:textbox> </label> <label id="label6" for="email"> email address: <asp:textbox id="email" runat="server"></asp:textbox> </label> <label id="label5" for="info"> additional info: <asp:textbox id="info" runat="server"></asp:textbox> </label> </div> <asp:sqldatasource id="sqldatasource2" runat="server"></asp:sqldatasource> <div id="logindetails" class="col-md-4"> <p>website not showing in list? add website list.</p> <label id="label7" for="websitename"> website name: <asp:textbox id="wname" runat="server"></asp:textbox> </label> <label id="label8" for="link"> website address: <asp:textbox id="link" runat="server"></asp:textbox> </label> </div> <asp:button id="addwebsite" runat="server" text="add website" onclick="addwebsite_click" /> </div> <asp:button id="add" runat="server" text="add login" class="btn btn-default" onclick="add_click" />
well user
reserved word , that's issue here can seen in posted insert
statement. needs escaped [user]
if it's sql server or use ansi style escaping using double quotes "user"
insert logins(websiteid, username, password, additionalinfo, [user], email)
Comments
Post a Comment