【问题标题】:shopping cart in c# does not work correctlyc#中的购物车无法正常工作
【发布时间】: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


【解决方案1】:

这里有几个问题:

  1. 您没有在 while 循环中增加 'i'。
  2. 您将网格绑定到生成数据的 while 循环中。

我建议进行以下更改:

  1. 提取一个返回 DataTable 的方法并将其设置为 GridView 的 DataSource
  2. 在!IsPostback 检查中包装将数据绑定到GridView 的代码。这将有助于处理 GridView 中的事件。

示例代码

protected void Page_Load(object sender, EventArgs e)
{
    if(!IsPostBack)
    {
        GridView1.DataSource = GetData().DefaultView;
        GridView1.DataBind();

        GridView1.FooterRow.Cells[5].Text = grandTotal().ToString();
    }
    
    Label2.Text = DateTime.Now.ToShortDateString();
}

private DataTable GetData()
{
    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);

        i++;
    }

    return dt;
}

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;
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-04-19
    • 2017-02-18
    • 2021-04-29
    • 1970-01-01
    • 1970-01-01
    • 2016-11-08
    • 2014-01-21
    相关资源
    最近更新 更多