Click here to Skip to main content
15,902,840 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i am making a webmethod in asmx.... but whenever i run this method i will get this error... i want to add manually row in dataset,with the 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 as voucher,gt.Acct_Nirration,gr.InstrumentNo,gt.Dr_Amount,gt.Cr_Amount  from gl_transaction gt, Gl_Ref gr where gt.Accountno = '" + accno + "'  and gt.Voucher_No=gr.Voucher_no  and gt.Value_Date between '" + fromdate + "' and '" + 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();
    return ds;





}
Posted
Updated 23-Jun-14 5:11am
v2

1 solution

See this : http://msdn.microsoft.com/en-us/library/haa3afyz(v=vs.110).aspx[^] and especially refer to the section Closing the DataReader.

Regards..
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900