asp.net - StoredProcedure in C#, passing parameters from text boxes -
i have simple page wan find out how pass data text boxes procedure. kind of new passing parameters code behind in db,i have 1 page code
<%@ page language="c#" autoeventwireup="true" codefile="default.aspx.cs" inherits="_default" %> <!doctype html> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title></title> </head> <body> <form id="form1" runat="server"> <div> <asp:textbox id="lastname" runat="server"></asp:textbox> <asp:textbox id="firstname" runat="server"></asp:textbox> <asp:textbox id="phone1" runat="server"></asp:textbox> <asp:textbox id="phone2" runat="server"></asp:textbox> <br /> <asp:button id="button1" runat="server" onclick="button1_click" text="button" /> <br /> <asp:sqldatasource id="sqldatasource1" runat="server" connectionstring="<%$ connectionstrings:ticketsconnectionstring %>" selectcommand="select * [employee]"></asp:sqldatasource> </div> </form> </body> </html>
and aspx code
using system; using system.collections.generic; using system.linq; using system.web; using system.web.ui; using system.web.ui.webcontrols; using system.data; using system.data.sqlclient; using system.configuration; public partial class _default : system.web.ui.page { protected void page_load(object sender, eventargs e) { } string connectionstring = configurationmanager.connectionstrings["ticketsconnectionstring"].connectionstring; protected void button1_click(object sender, eventargs e) { dataset ds = new dataset(); sqlconnection con = new sqlconnection(connectionstring); sqlcommand cmd = new sqlcommand("insertintoemployee", con); cmd.commandtype = commandtype.storedprocedure; cmd.parameters.add("@lastname", sqldbtype.varchar).value = lastname.text; cmd.parameters.add("@firstname", sqldbtype.varchar); cmd.parameters.add("@phone1", sqldbtype.varchar); cmd.parameters.add("@phone2", sqldbtype.varchar); cmd.parameters["@lastname"].value = lastname.text; cmd.parameters["@firstname"].value = firstname.text; cmd.parameters["@phone1"].value = phone1.text; cmd.parameters["@phone2"].value = phone2.text; con.open(); cmd.executenonquery(); con.close(); } }
web.config looks
<?xml version="1.0"?> <!-- more information on how configure asp.net application, please visit http://go.microsoft.com/fwlink/?linkid=169433 --> <configuration> <connectionstrings> <add name="ticketsconnectionstring" connectionstring="data source=(localdb)\v11.0;attachdbfilename="c:\users\asya\documents\visual studio 2012\websites\website6\app_code\tickets.mdf";integrated security=true;connect timeout=30" providername="system.data.sqlclient" /> </connectionstrings> <system.web> <compilation debug="true" targetframework="4.5"/> <httpruntime targetframework="4.5"/> </system.web> </configuration>
when pass simple data in prosedure throws error
an sqlparameter parametername '@lastname' not contained sqlparametercollection.
how resolve problem? because on db side procedure works saed before new in it. , fixed previous problems here 1
the string not recognized valid datetime. there unknown word starting @ index 0. description: unhandled exception occurred during execution of current web request. please review stack trace more information error , originated in code. exception details: system.formatexception: string not recognized valid datetime. there unknown word starting @ index 0. source error: line 34: cmd.parameters["@phone2"].value = phone2.text; line 35: con.open(); line 36: cmd.executenonquery(); line 37: con.close(); line 38:
procedure use
procedure insertintoemployee @lastname nvarchar (25) , @firstname nvarchar (25) , @phone1 nvarchar (25) , @phone2 nvarchar (25) insert employee (lastname , firstname,phone1,phone2)values (@lastname,@firstname,@phone1,@phone2);
when run prosedure on db side exec insertintoemployee n'петров', n'петр', 99999999,null
works asp.net side throwing errors
try
protected void button1_click(object sender, eventargs e) { dataset ds = new dataset(); sqlconnection con = new sqlconnection(connectionstring); sqlcommand cmd = new sqlcommand("insertintoemployee", con); cmd.commandtype = commandtype.storedprocedure; cmd.parameters.add("@lastname", sqldbtype.nvarchar(max)).value = lastname.text; cmd.parameters.add("@firstname", sqldbtype.nvarchar(max)).value = firstname.text; cmd.parameters.add("@phone1", sqldbtype.nvarchar(max)).value = phone1.text; cmd.parameters.add("@phone2", sqldbtype.nvarchar(max)).value = phone2.text; con.open(); cmd.executenonquery(); con.close(); // gvquarterlyreport.databind(); }
Comments
Post a Comment