Wednesday 15 May 2013

sql server - Executing an update stored procedure in C# -



sql server - Executing an update stored procedure in C# -

i need know, if writing stored procedure correctly , if c# code executing correct. reason error beingness returned wrong syntax near 'c16b'. old error

the new error is: procedure or function 'sptimeupdate' expects parameter '@date', not supplied.

the nvarchar string validating , updating in column clientid 3fc8ffa1-c16b-4d7b-9e55-1e88dfe15277, part in bold showing in debug test intel sense in error handling

alter procedure sptimeupdate @id nvarchar(50), @date datetime begin set nocount on; update clienttable set today_date=(@date) clientid=(@id) end //--------------above stored procedure-------------------------------- //--------------executing stored procedure in c# ienumerable<xelement> searchclientid = clientid in main.xpathselectelements("network/clientid") (string)clientid.attribute("id") == intializedports[i].tostring() select clientid; foreach (string clientid in searchclientid) { (int = 0; < intializedports.count(); up++) { //update current time in clientid tble. //renames table re-create groups seek { string[] clientid; //client id array clientid = new string[intializedports.count()]; //intialization of array clientid[up] = clientid.tostring(); datetime td = convert.todatetime(tooldate.text); //just added datetime object withdate sqlconnection sqlconnectioncmdstring = new sqlconnection(@"data=.\sqlexpress;attachdbfilename=c:\users\shawn\documents\visual studio 2010\projects\server\database\clientregit.mdf;integrated security=true;user instance=true"); //execute stored procedure sptimedate // string updatecommand = "sptimeupdate" + clientid[up].tostring() + tooldate.text; string updatecommand = "sptimeupdate" + "'" + clientid[up].tostring() + "'" + "'" +td.tostring()+ "'"; //this new updatecommand string pass parameters stored procedure sqlcommand sqlrenamecommand = new sqlcommand(updatecommand, sqlconnectioncmdstring); sqlconnectioncmdstring.open(); sqlrenamecommand.executenonquery(); sqlconnectioncmdstring.close(); } catch(dataexception ex) { messagebox.show("failed updatecurrenttime","dataerror",messageboxbuttons.ok,messageboxicon.error); } } }

when phone call stored procedure code need create command command type set storedprocedure, otherwise engine tries utilize command text sql text select insert etc... mose of import thing need pass parameters required stored procedure in parameters collection of command

so code replace actual one

string updatecommand = "sptimeupdate"; using(sqlconnection sqlconnectioncmdstring = new sqlconnection(......)) using(sqlcommand sqlrenamecommand = new sqlcommand(updatecommand, sqlconnectioncmdstring)) { datetime td = convert.todatetime(tooldate.text); sqlrenamecommand.commandtype = commandtype.storedprocedure; sqlrenamecommand.parameters.add("@id", sqldbtype.nvarchar).value = clientid[up].tostring(); sqlrenamecommand.parameters.add("@date", sqldbtype.datetime).value = td; sqlconnectioncmdstring.open(); sqlrenamecommand.executenonquery(); }

notice 2 things. using statement best practice follow when create connection ensure right closing , disposing of connection, second, parameter datetime expected sp should passed datetime not string- of course of study means should content of tooldate convertible datetime value.

c# sql-server stored-procedures

No comments:

Post a Comment