我想创建动态<asp:table ID=""...>
,我希望通过从我的datareader读取第一个单元格并将其整合到一个totalAmount
列,此列必须显示累计数字....
try
{
sqlconnection();
sqlcommand("", sqlconnection);
sqlconnection.open();
sqldatereader dr;
dr = sqlcommand.executereader();
if (dr.HasRows)
{
//create column names;
table_ShowReport.Rows.Add(new TableRow());
table_ShowReport.Rows[table_ShowReport.Rows.Count - 1].Cells.Add(new TableCell());
table_ShowReport.Rows[table_ShowReport.Rows.Count - 1].Cells[table_ShowReport.Rows[table_ShowReport.Rows.Count - 1].Cells.Count - 1].BorderWidth = 2;
table_ShowReport.Rows[table_ShowReport.Rows.Count - 1].Cells[table_ShowReport.Rows[table_ShowReport.Rows.Count - 1].Cells.Count - 1].Text = "Amount";
table_ShowReport.Rows[table_ShowReport.Rows.Count - 1].Cells.Add(new TableCell());
table_ShowReport.Rows[table_ShowReport.Rows.Count - 1].Cells[table_ShowReport.Rows[table_ShowReport.Rows.Count - 1].Cells.Count - 1].BorderWidth = 2;
table_ShowReport.Rows[table_ShowReport.Rows.Count - 1].Cells[table_ShowReport.Rows[table_ShowReport.Rows.Count - 1].Cells.Count - 1].Text = "Total";
//here I wantend to get the initial value to my total:
double total = double.parse(dr["amount"].tostring());
while (dr.Read())
{
table_ShowReport.Rows.Add(new TableRow());
table_ShowReport.Rows[table_ShowReport.Rows.Count - 1].Cells.Add(new TableCell());
table_ShowReport.Rows[table_ShowReport.Rows.Count - 1].Cells[table_ShowReport.Rows[table_ShowReport.Rows.Count - 1].Cells.Count - 1].BorderWidth = 2;
table_ShowReport.Rows[table_ShowReport.Rows.Count - 1].Cells[table_ShowReport.Rows[table_ShowReport.Rows.Count - 1].Cells.Count - 1].Text = dr["amount"].tostring();
table_ShowReport.Rows[table_ShowReport.Rows.Count - 1].Cells.Add(new TableCell());
table_ShowReport.Rows[table_ShowReport.Rows.Count - 1].Cells[table_ShowReport.Rows[table_ShowReport.Rows.Count - 1].Cells.Count - 1].BorderWidth = 2;
table_ShowReport.Rows[table_ShowReport.Rows.Count - 1].Cells[table_ShowReport.Rows[table_ShowReport.Rows.Count - 1].Cells.Count - 1].Text = total.tostring();
total += double.parse(dr["amount"].tostring());
}
}
sqlconnection.close();
}
catch (exception ex)
{
respose.write(ex.message);
}
但它向我显示了这个错误:
无数据时无效尝试读取 在场。
请帮帮我。 感谢
丹娘
答案 0 :(得分:1)
您必须在尝试从SqlDataReader读取之前调用dr.Read()
。
我建议在读取行之前将total
变量初始化为零。然后在调用total
之后但在显示总金额之前,将dr.Read()
设置为更新后的金额。像这样:
double runningTotal = 0d;
while (dr.Read())
{
table_ShowReport.Rows.Add(new TableRow());
table_ShowReport.Rows[table_ShowReport.Rows.Count - 1].Cells.Add(new TableCell());
table_ShowReport.Rows[table_ShowReport.Rows.Count - 1].Cells[table_ShowReport.Rows[table_ShowReport.Rows.Count - 1].Cells.Count - 1].BorderWidth = 2;
table_ShowReport.Rows[table_ShowReport.Rows.Count - 1].Cells[table_ShowReport.Rows[table_ShowReport.Rows.Count - 1].Cells.Count - 1].Text = dr["amount"].ToString();
table_ShowReport.Rows[table_ShowReport.Rows.Count - 1].Cells.Add(new TableCell());
table_ShowReport.Rows[table_ShowReport.Rows.Count - 1].Cells[table_ShowReport.Rows[table_ShowReport.Rows.Count - 1].Cells.Count - 1].BorderWidth = 2;
runningTotal += double.Parse(dr["amount"].ToString());
table_ShowReport.Rows[table_ShowReport.Rows.Count - 1].Cells[table_ShowReport.Rows[table_ShowReport.Rows.Count - 1].Cells.Count - 1].Text = runningTotal.ToString();
}
}
sqlconnection.Close();
以下是重构的完整代码,所以更清楚:
private static void PopulateReport(Table table, string connection)
{
table.Rows.Clear();
PopulateReportRow(table, "Amount", "Total");
string commandText = @"...";
using (SqlConnection conn = new SqlConnection(connection))
{
conn.Open();
using (SqlCommand comm = new SqlCommand(commandText, conn))
{
using (SqlDataReader reader = comm.ExecuteReader())
{
double total = 0d;
while (reader.Read())
{
double amount = (double)reader["amount"];
total += amount;
PopulateReportRow(table, amount.ToString("N"), total.ToString("N"));
}
}
}
}
}
private static void PopulateReportRow(Table table, string amountValue, string totalValue)
{
const int cellBorderWidth = 2;
int rowIndex = table.Rows.Add(new TableRow());
TableCell amountCell = new TableCell();
table.Rows[rowIndex].Cells.Add(amountCell);
amountCell.BorderWidth = cellBorderWidth;
amountCell.Text = amountValue;
TableCell totalCell = new TableCell();
table.Rows[rowIndex].Cells.Add(totalCell);
totalCell.BorderWidth = cellBorderWidth;
totalCell.Text = totalValue;
}
答案 1 :(得分:0)
在你到达第一行之前,你试图从读者那里阅读。
您应该将其设置为0
并在您正在进行的循环中填充它。
答案 2 :(得分:0)
protected void Btnlogin_Click(object sender,EventArgs e)
{
SqlConnection con=newSqlConnection("connection string");
SqlCommand cmd = new SqlCommand();
cmd.CommandText = "select * from tbluser where username='" + txtusername + "' and passworde='" + txtpassworde + "'";
cmd.Connection = con;
if (con.State == ConnectionState.Closed) con.Open();
SqlDataReader dr = cmd.ExecuteReader();
while (dr.Read())
{
if (dr["username"] == txtusername.Text.Trim() && dr["passworde"] == txtpassworde)
{
Response.Redirect("kala.aspx");
return;
}
}
}
代码正确但未进入下一页