【发布时间】:2021-09-02 04:09:09
【问题描述】:
实际上,我正在用 c# 准备一个应用程序,比如购物车,但我的账单不起作用。当我加载页面时,它一直在加载并且没有显示错误或其他内容,它只是继续加载并且什么也没有显示。该应用程序是用 c# 和 asp.net webforms 构建的。我试过不包括循环 while 和 i 然后它只显示一行。我不知道为什么循环不起作用。
public partial class myQueries : System.Web.UI.Page
{
public SqlDataAdapter da;
public DataSet ds;
public SqlConnection sqlConn;
public SqlConnection sqlConn1;
protected void Page_Load(object sender, EventArgs e)
{
string uID = Session["uID"].ToString();
int userID = int.Parse(uID);
DataTable dt = new DataTable();
DataRow dr;
dt.Columns.Add("requestID");
dt.Columns.Add("request");
dt.Columns.Add("solution");
dt.Columns.Add("qtyn");
dt.Columns.Add("price");
dt.Columns.Add("totalprice");
sqlConn = new System.Data.SqlClient.SqlConnection(ConfigurationManager.ConnectionStrings["OHDConn"].ConnectionString);
sqlConn.Open();
string queryRequest = "select requestID,request,solution,qtyn,price from request where userID = '" + userID + "' and solution is not Null";
SqlCommand cmd = new SqlCommand();
cmd.CommandText = queryRequest;
cmd.Connection = sqlConn;
da = new SqlDataAdapter();
da.SelectCommand = cmd;
ds = new DataSet();
da.Fill(ds);
int totalrows = ds.Tables[0].Rows.Count;
int i = 0;
while (i < totalrows)
{
dr = dt.NewRow();
dr["requestID"] = ds.Tables[0].Rows[i]["requestID"].ToString();
dr["request"] = ds.Tables[0].Rows[i]["request"].ToString();
dr["solution"] = ds.Tables[0].Rows[i]["solution"].ToString();
dr["qtyn"] = ds.Tables[0].Rows[i]["qtyn"].ToString();
dr["price"] = ds.Tables[0].Rows[i]["price"].ToString();
double price = Convert.ToDouble(ds.Tables[0].Rows[i]["price"].ToString());
int qty = Convert.ToInt16(ds.Tables[0].Rows[i]["qtyn"].ToString());
double totalprice = price * qty;
dr["totalprice"] = totalprice;
dt.Rows.Add(dr);
GridView1.DataSource = dt;
GridView1.DataBind();
GridView1.FooterRow.Cells[5].Text = grandTotal().ToString();
Label2.Text = DateTime.Now.ToShortDateString();
}
}
public double grandTotal()
{
DataTable dt = new DataTable();
int nrow = dt.Rows.Count;
int i = 0;
double gtotal = 0;
while (i < nrow)
{
gtotal = gtotal + Convert.ToDouble(dt.Rows[i]["totalprice"].ToString());
i = i + 1;
}
return gtotal;
}
【问题讨论】:
-
请检查您的表是否被某些事务锁定。在这种情况下,您需要终止交易。如果该事务的会话仍然存在,则提交/回滚该事务。
-
您需要在
while循环的末尾增加 i。例如i++。没有它,您的循环将不会迭代到下一个索引并且永远不会结束。 -
你在 while 循环结束时缺少
i++! -
您将网格绑定在内部,同时将其移除并将结果绑定到外部。
标签: c# asp.net shopping-cart billing invoice