c# - 如何使用 session 用户计算购物车

标签 c# mysql asp.net shopping-cart

我目前正在做一个关于购物车的项目。我已经完成了购物车,但不知何故我无法计算购物车,并且它一直显示 0。 我已经检查了用户的 session 是否已被抓取。

这是我使用的方法:

    //check cart count
    public int getCartCount(string Username)
    {

        MySql.Data.MySqlClient.MySqlConnection msqlConnection = null;
        msqlConnection = new MySql.Data.MySqlClient.MySqlConnection("server=localhost;User Id=root;password=rootPassw0rd;Persist Security Info=False;database=infosec;Integrated Security=False");
        MySql.Data.MySqlClient.MySqlCommand msqlCommand = new MySql.Data.MySqlClient.MySqlCommand();
        //define the connection used by the command object
        msqlCommand.Connection = msqlConnection;

        msqlCommand.CommandText = "SELECT COUNT(*) FROM shoppingcart WHERE Item_Id = @Username ";
        msqlCommand.Parameters.AddWithValue("@Username", _Username);

        msqlConnection.Open();

        string nofRow = "";

        MySql.Data.MySqlClient.MySqlDataReader msqlReader = msqlCommand.ExecuteReader();

        if (msqlReader.Read())
        {
            nofRow = msqlReader["COUNT(*)"].ToString();
        }

        msqlConnection.Close();
        msqlConnection.Close();
        msqlReader.Dispose();

        int cart = Convert.ToInt32(nofRow);


        return cart;

这是 .axps 页面的隐藏代码:

 //cart increase if there items added.
        if (Session["customer_Username"] == null)
        {
            cartCount.InnerText = "Cart (0)";
        }
        else
        {
            cartBLL cBLL = new cartBLL();
            string a = Session["customer_Username"].ToString();
            int count = cBLL.getCartCount(a);
            cartCount.InnerText = "Cart (" + count + ")";
        }

希望大家帮我找出问题所在。 提前致谢。

最佳答案

改变这一行:

nofRow = msqlReader["COUNT(*)"].ToString();

nofRow = msqlReader[0].ToString();

也许,它对你有帮助。

关于c# - 如何使用 session 用户计算购物车,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24913101/

相关文章:

mysql - 事件记录(mysql db)文本字段中的字符数是否有限制

ASP.NET MVC6 : Setting webroot programmatically

c# - 创建一个使用 CAGradientLayer 作为其层的 UIView 子类

Mysql错误的日期格式

c# - 条件运算符会感到困惑,但为什么呢?

mysql - 外键的语法差异 MySQL/PostgresQL

mysql - 如何在我的 sql 中应用多个或嵌套游标并将数据存储在临时表中?

c# - ASP.Net 和 GetType()

c# - 电影院座位排c#

文本框的 C# 自定义掩码