C# 如何使用C在asp.net中的executeScalar()内部使用executeReader()
我得到一个错误: 已存在与此命令关联的打开的DataReader 必须先关闭它 代码如下: 我使用1个表生成动态表&在该表的每一行中,我希望从另一个表进行求和计算 我首先使用sqlcommand cmd和ExecuteReader调用了readDr。之后,我调用了CalTotAmtstring CC,它使用sqlcommand cmdtotam&ExecuteScalar。 我已经使用了2diff-sqlcommand,但仍然存在它的给定错误C# 如何使用C在asp.net中的executeScalar()内部使用executeReader(),c#,asp.net,ado.net,sqlcommand,C#,Asp.net,Ado.net,Sqlcommand,我得到一个错误: 已存在与此命令关联的打开的DataReader 必须先关闭它 代码如下: 我使用1个表生成动态表&在该表的每一行中,我希望从另一个表进行求和计算 我首先使用sqlcommand cmd和ExecuteReader调用了readDr。之后,我调用了CalTotAmtstring CC,它使用sqlcommand cmdtotam&ExecuteScalar。 我已经使用了2diff-sqlcommand,但仍然存在它的给定错误 protected void readDr() {
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永远不会被输入您应该始终说出抛出错误的位置(即哪一行)…您应该编写一个查询,将SUM作为附加列,而不是将每个SUM作为单独的数据库调用;n voo:我没注意到。更糟糕的是,后来他在某些地方使用m+n来生成唯一的ID,这将非常令人困惑…:@克里斯:欢呼的有趣代码永远不会少;