我目前正在做一个关于购物车的项目。我已经完成了购物车,但不知何故我无法计算购物车,并且它一直显示 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/