Monday, 15 July 2013

c# - C Sharp Stored procedure Timeout on second run -



c# - C Sharp Stored procedure Timeout on second run -

i calling stored procedure via c sharp , unusual reason timeouts on sec run.

the code phone call stored procedure:

private void loaddata() { backgroundworker bw = new backgroundworker(); bw.dowork += new doworkeventhandler(bw_loaddata); bw.runworkercompleted += new runworkercompletedeventhandler(bw_loaddatacomplete); busy.isbusy = true; busy.busycontent = "loading data"; bw.runworkerasync(); } void bw_loaddata(object sender, doworkeventargs e) { sqlconnection con = new sqlconnection(logic.getconnectionstring()); con.open(); sqlcommand com = new sqlcommand("spgetuserdata", con); com.commandtype = system.data.commandtype.storedprocedure; com.parameters.add(new sqlparameter("@uid", uid)); //timeouts here on sec run sqldatareader readuserdata = com.executereader(); while (readuserdata.read()) { origname = readuserdata[0].tostring(); origemail = readuserdata[1].tostring(); origcontact = readuserdata[2].tostring(); origadd1 = readuserdata[3].tostring(); origadd2 = readuserdata[4].tostring(); origstate = readuserdata[5].tostring(); origcity = readuserdata[6].tostring(); origzip = readuserdata[7].tostring(); origcountry = readuserdata[8].tostring(); } con.close(); con.dispose(); e.result = "ok"; } void bw_loaddatacomplete(object sender, runworkercompletedeventargs e) { busy.isbusy = false; txtfullname.text = origname; txtemail.text = origemail ; txtcontact.text= origcontact; txtadd1.text= origadd1; txtadd2.text= origadd2 ; txtstate.text= origstate; txtcity.text= origcity; txtzip.text = origzip; cbocountry.selecteditem = origcountry; }

first method phone call during window load event.. works expected.

private void window_loaded_1(object sender, routedeventargs e) { loaddata(); }

second method phone call in timeout occurs.

void bw_changeemailcomplete(object sender, runworkercompletedeventargs e) { if (e.result.tostring() == "ok") { busy.isbusy = false; messagebox.show("the email address changed successfully", "message", messageboxbutton.ok, messageboximage.information); } else { busy.isbusy = false; messagebox.show("an unexpected error occured or email exist", "error", messageboxbutton.ok, messageboximage.error); } loaddata(); }

and finnaly stored procedure

create proc [dbo].[spgetuserdata] @uid varchar(50) select fullname,email,contact,address1,address2,state,city,zip,country,subdate,sid users uid = @uid

update

still not working after trying , manually disposing info reader

using (sqlconnection con = new sqlconnection(logic.getconnectionstring())) { using (sqlcommand com = new sqlcommand("spgetuserdata", con)) { com.commandtype = system.data.commandtype.storedprocedure; com.parameters.add(new sqlparameter("@uid", uid)); con.open(); using (var readuserdata = com.executereader()) { while (readuserdata.read()) { origname = readuserdata[0].tostring(); origemail = readuserdata[1].tostring(); origcontact = readuserdata[2].tostring(); origadd1 = readuserdata[3].tostring(); origadd2 = readuserdata[4].tostring(); origstate = readuserdata[5].tostring(); origcity = readuserdata[6].tostring(); origzip = readuserdata[7].tostring(); origcountry = readuserdata[8].tostring(); } } } }

you forgot dispose datareader, readuserdata.

put in using statement:

using (var readuserdata = com.executereader()) { while (readuserdata.read()) ... }

(use using sqlconnection too-- using strictly improve manually calling close() and/or dispose().)

c# sql-server wpf sql-server-2012

No comments:

Post a Comment