簡體   English   中英

如何使用C#在asp.net中的executeReader()內部使用executeScalar()

[英]How to use executeScalar() inside executeReader()in asp.net using C#

我收到錯誤:

“已經有一個與此命令關聯的開放DataReader必須先關閉。”

代碼如下:


我正在使用1個表生成動態表,並且在該表的每一行中我想要從另一個表計算總和

我首先使用sqlcommand“cmd”和ExecuteReader()調用了readDr() ExecuteReader()
之后我調用了CalTotAmt(string CC) ,它使用sqlcommand“cmdTotAmt”和ExecuteScalar()
我使用2 diff sqlcommand仍然給出錯誤。

 protected void readDr() { string str = "select CCNo,TotalAmt,NoOfRect,Energy,New1,Theft,Misc from ChallanTable;"; cmd = new SqlCommand(str, con); rdr = cmd.ExecuteReader(); } protected void CreateChallanTable() { table.Caption = "Challan Entry"; table.ID = "Challan Entry"; table.BackColor = System.Drawing.Color.Maroon; table.ForeColor = System.Drawing.Color.Gray; readDr(); //call to function readDr() Panel2.Controls.Add(table); for (i = 0; i < 1; i++) { row = new TableRow(); row.BorderStyle = BorderStyle.Ridge; m = 0; while (rdr.Read()) { row = new TableRow(); row.ID = "Row" + m; row.BorderStyle = BorderStyle.Ridge; for (n = 0; n <= 6; n++) { cell = new TableCell(); cell.ID = "cell" + m + n; cell.BorderWidth = 5; cell.BorderStyle = BorderStyle.Ridge; //cell.BorderColor = System.Drawing.Color.Azure; for (n = 0; n <= 0; n++) { Label lbl = new Label(); lbl.ID = "lblCCRow" + m + n; lbl.Text = Convert.ToString(rdr[0]); lbl.Width = 70; CCNo = lbl.Text; // Add the control to the TableCell cell.Controls.Add(lbl); } for (n = 1; n <= 1; n++) { Label lbl = new Label(); lbl.ID = "lblTotAmt" + m + n; lbl.Text = Convert.ToString(rdr[1]); lbl.Width = 70; TotAmt = Convert.ToDouble(lbl.Text); // Add the control to the TableCell cell.Controls.Add(lbl); Label2.Text = Convert.ToString(CalTotAmt(CCNo)); } for (n = 2; n <= 2; n++) { Label lbl = new Label(); lbl.ID = "lblNoRect" + m + n; lbl.Text = Convert.ToString(rdr[2]); lbl.Width = 70; NoofRects = Convert.ToDouble(lbl.Text); // Add the control to the TableCell cell.Controls.Add(lbl); } for (n = 2; n <= 2; n++) { Label lbl = new Label(); lbl.ID = "lblEnergy" + m + n; lbl.Text = Convert.ToString(rdr[3]); lbl.Width =70; Energy = Convert.ToDouble(lbl.Text); // Add the control to the TableCell cell.Controls.Add(lbl); } for (n = 3; n <= 3; n++) { Label lbl = new Label(); lbl.ID = "lblNew" + m + n; lbl.Text = Convert.ToString(rdr[4]); lbl.Width =70; NewSD = Convert.ToDouble(lbl.Text); // Add the control to the TableCell cell.Controls.Add(lbl); } for (n = 4; n <= 4; n++) { Label lbl = new Label(); lbl.ID = "lblTheft" + m + n; lbl.Text = Convert.ToString(rdr[5]); lbl.Width = 70; Theft = Convert.ToDouble(lbl.Text); // Add the control to the TableCell cell.Controls.Add(lbl); } for (n = 5; n <= 5; n++) { Label lbl = new Label(); lbl.ID = "lblMisc" + m + n; lbl.Text = Convert.ToString(rdr[6]); lbl.Width = 70; Misc = Convert.ToDouble(lbl.Text); // Add the control to the TableCell cell.Controls.Add(lbl); } row.Cells.Add(cell); } // Add the TableRow to the Table table.Rows.Add(row); //dr.NextResult(); //outer for-loop end m++; } rdr.Close(); } protected double CalTotAmt(string CC) { double Total = 0; string str = "select Sum(Amount) from MainDataTable Where CC='" + CC + "' and BU ='" + LogInBU + "'"; SqlCommand cmdTotAmt = new SqlCommand(str,con); Total = Convert.ToDouble(cmdTotAmt.ExecuteScalar()); Label2.Text = Total.ToString(); return Total; } 

請幫幫我。

在這里,您嘗試在同一連接上同時打開多個記錄集。 您可以通過將MultipleActiveResultSets = True添加到連接字符串來實現。

嘗試修改代碼,以確保正確關閉DataReader:

示例代碼:

protected DataTable readDr()
{
        con.Open();
        string str = "select CCNo,TotalAmt,NoOfRect,Energy,New1,Theft,Misc from ChallanTable;";
        cmd = new SqlCommand(str, con);
        rdr = cmd.ExecuteReader();
        DataTable dt = new DataTable(); 
        dt.Load(rdr);
        rdr.Close();
        con.Close();
}

您通常會通過編寫一個查詢來一次性計算所有結果來修復它 - 而不是強迫您的代碼一遍又一遍地繼續查詢數據庫。

就像是:

select CCNo,TotalAmt,NoOfRect,Energy,New1,Theft,Misc,SumTotal
from ChallanTable ct
      cross apply
     (select Sum(Amount) as SumTotal from MainDataTable Where CC=ct.CCNo) t

然后你只需要處理結果。

(另外,你目前有一個bug在你的顯示器的代碼-你有兩個試圖做一些事情,當n == 2,第二個for將永遠不會進入)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM