asp.net - c#.net Error: There is already an open DataReader associated with this Command which must be closed first -
help, new asp.net c#. tried codes once before didn't seems have error. can't seems figure make changes to.
sever error : there open datareader associated command must closed first.
codes:
string strconnectionstring = configurationmanager.connectionstrings["fypdb"].connectionstring; sqlconnection myconnect = new sqlconnection(strconnectionstring); string strcommandtext = "select promoid fypdb.dbo.promotions membershiptype '%' + @membership + '%' , defaults '%' + @defaults + '%'"; try { string ddlmembership = ((dropdownlist)dvinsertpromotion.findcontrol("ddladdmembershiplist")).selecteditem.tostring(); string ddldefault = ((radiobuttonlist)dvinsertpromotion.findcontrol("radiobuttonlist2")).text.tostring(); dataset da = dal.retrievemembership(ddlmembership, ddldefault); sitegridview.datasource = da; sitegridview.databind(); sqlcommand cmd = new sqlcommand(strcommandtext, myconnect); cmd.parameters.add("@membership", sqldbtype.nvarchar); cmd.parameters.add("@defaults", sqldbtype.nvarchar); cmd.parameters["@membership"].value = ddlmembership; cmd.parameters["@defaults"].value = ddldefault; myconnect.open(); sqldatareader reader = cmd.executereader(); if (reader.read()) { //get number of count //int count = da.tables[0].rows.count; int count = (int)cmd.executescalar(); if (count == 1) { defaultcomfirm.show(); promotion = false; } } else { label6.text = "error didnt go through"; label6.forecolor = system.drawing.color.gray; label6.visible = true; } reader.close(); } { myconnect.close(); }
you using same command object 2 times:
- sqldatareader reader = cmd.executereader();
- int count = (int)cmd.executescalar();
this not possible.
it not necessary use reader if want have count of rows. in case can use executescalar.
like this:
string strcommandtext = "select count(promoid) fypdb.dbo.promotions membershiptype '%' + @membership + '%' , defaults '%' + @defaults + '%'"; try { ... int count = (int)cmd.executescalar(); ...
regards oliver
Comments
Post a Comment