c# - System.InvalidOperationException: There is already an open DataReader associated with this Command which must be closed first -



c# - System.InvalidOperationException: There is already an open DataReader associated with this Command which must be closed first -

i making webmethod in asmx.... whenever run method error... want add together manually row in dataset,with help of datareader [webmethod]

public dataset newleger(string accno, string fromdate, string todate) { sqlconnection con = new sqlconnection(@"data source=123-pc;initial catalog=bcounts;persist security info=true;user id=saba;password=123"); con.open(); sqlcommand cmd = new sqlcommand("select gt.value_date,gt.voucher_no+'-'+gr.vchrtype voucher,gt.acct_nirration,gr.instrumentno,gt.dr_amount,gt.cr_amount gl_transaction gt, gl_ref gr gt.accountno = '" + accno + "' , gt.voucher_no=gr.voucher_no , gt.value_date between '" + fromdate + "' , '" + todate + "'", con); sqldataadapter adp = new sqldataadapter(cmd); sqldatareader rdr = cmd.executereader(); decimal crsum = 0; decimal drsum = 0; decimal balance = 0; dataset ds = new dataset("ledger");; adp.fill(ds); if (rdr.hasrows) { while (rdr.read()) { if (rdr.getdecimal(4) > 0) { balance = balance + rdr.getdecimal(4); drsum += rdr.getdecimal(4); datarow dr = ds.tables[0].newrow(); dr[0] = rdr.getdatetime(0).toshortdatestring(); dr[1] = rdr.getstring(1); dr[2] = rdr.getstring(2); dr[3] = rdr.getstring(3); dr[4] = rdr.getdecimal(4).tostring(); dr[5] = "-"; ds.tables[0].rows.add(dr); //data.add(new datalist7(rdr.getdatetime(0).toshortdatestring(), rdr.getstring(1), rdr.getstring(2), rdr.getstring(3), rdr.getdecimal(4).tostring(), "-", balance.tostring())); } else { balance = balance - rdr.getdecimal(5); crsum += rdr.getdecimal(5); datarow dr = ds.tables[0].newrow(); dr[0] = rdr.getdatetime(0).toshortdatestring(); dr[1] = rdr.getstring(1); dr[2] = rdr.getstring(2); dr[3] = rdr.getstring(3); dr[4] = "-"; dr[5] = rdr.getdecimal(5).tostring(); ds.tables[0].rows.add(dr); // data.add(new datalist7(rdr.getdatetime(0).toshortdatestring(), rdr.getstring(1), rdr.getstring(2), rdr.getstring(3), "-", rdr.getdecimal(5).tostring(), balance.tostring())); } } datarow dd = ds.tables[0].newrow(); dd[0] = "-"; dd[1] = "-"; dd[2] = "-"; dd[3] = "-"; dd[4] = drsum.tostring(); dd[5] = crsum.tostring(); //data.add(new datalist7("-", "-", "-", "-", drsum.tostring(), crsum.tostring(), "-")); } con.close(); homecoming ds; }

try this

public dataset newleger(string accno, string fromdate, string todate) { sqlconnection con = new sqlconnection(@"data source=123-pc;initial catalog=bcounts;persist security info=true;user id=saba;password=123"); con.open(); sqlcommand cmd = new sqlcommand("select gt.value_date,gt.voucher_no+'-'+gr.vchrtype voucher,gt.acct_nirration,gr.instrumentno,gt.dr_amount,gt.cr_amount gl_transaction gt, gl_ref gr gt.accountno = '" + accno + "' , gt.voucher_no=gr.voucher_no , gt.value_date between '" + fromdate + "' , '" + todate + "'", con); sqldataadapter adp = new sqldataadapter(); adp.selectcommand = cmd; decimal crsum = 0; decimal drsum = 0; decimal balance = 0; dataset ds = new dataset("ledger");; adp.fill(ds); foreach (datarow dr in ds.tables[0].rows) { //do stuff here.... if(convert.todecimal(dr[4]) > 0) { balance = balance + convert.todecimal(dr[4]); drsum += convert.todecimal(dr[4]); } else { balance = balance - convert.todecimal(dr[5]); crsum += convert.todecimal(dr[5]); } } //add totals datarow dd = ds.tables[0].newrow(); //dd[0] = "-"; //dd[1] = "-"; //dd[2] = "-"; //dd[3] = "-"; dd[4] = drsum; dd[5] = crsum; //data.add(new datalist7("-", "-", "-", "-", drsum.tostring(), crsum.tostring(), "-")); homecoming ds; }

c#

Comments

Popular posts from this blog

php - Android app custom user registration and login with cookie using facebook sdk -

django - Access session in user model .save() -

php - .htaccess Multiple Rewrite Rules / Prioritizing -