添加的购物车项目显示在数据库 [英] Added cart items display in database

查看:51
本文介绍了添加的购物车项目显示在数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想通过点击图片按钮获取我的数据库表中的产品名称,价格,图片路径......即购物车。任何人都可以帮我这样做并更正我的代码??







使用System.Data .SqlClient;



公共部分类Shopping_pro:System.Web.UI.Page

{



SqlConnection con;

SqlCommand com;



protected void Page_Load(object sender,EventArgs e)

{

Label7.Text = Session [cart]。ToString();

}

protected void ImageButton3_Click(object sender,ImageClickEventArgs e)

{

if(Session [name]!= null)

{



会话[购物车] =((int)(会话[购物车])+ 1);



// int pid;

//字符串pname,pimg;



con = new SqlConnection(ConfigurationManager.ConnectionStrings [connectionstring]。ConnectionString) ;



com = con.CreateCommand();



com.CommandText =插入购物车选择pid,pname,pimg,来自table_name的价格,其中pid = @ pid; < br $>
//com.Parameters.AddWithValue(\"@pid,pid);

//com.Parameters.Add(\"id,SqlDbType.Int).Value = 1;



com.Parameters.AddWithValue(@ pname,pname);

com.Parameters.AddWithValue(@ pimg ,pimg);

com.Parameters.AddWithValue(@ price,price);

con.Open();

int r = com.ExecuteNonQuery();

con.Close();



}

else

{

Response.Redirect(user_login.aspx);

}



}

}

I want to get the product name,price,image path...in my database table i.e. "Cart" by clicking imagebutton. Can anyone please help me to do so and correct my code??



using System.Data.SqlClient;

public partial class Shopping_pro : System.Web.UI.Page
{

SqlConnection con;
SqlCommand com;

protected void Page_Load(object sender, EventArgs e)
{
Label7.Text = Session["cart"].ToString();
}
protected void ImageButton3_Click(object sender, ImageClickEventArgs e)
{
if (Session["name"]!=null)
{

Session["cart"] = ((int)(Session["cart"]) + 1);

//int pid;
//string pname, pimg;

con = new SqlConnection(ConfigurationManager.ConnectionStrings["connectionstring"].ConnectionString);

com = con.CreateCommand();

com.CommandText = "insert into cart select pid,pname,pimg,price from table_name where pid=@pid";
//com.Parameters.AddWithValue("@pid", pid);
//com.Parameters.Add("id", SqlDbType.Int).Value = 1;

com.Parameters.AddWithValue("@pname",pname);
com.Parameters.AddWithValue("@pimg",pimg);
com.Parameters.AddWithValue("@price",price);
con.Open();
int r = com.ExecuteNonQuery();
con.Close();

}
else
{
Response.Redirect("user_login.aspx");
}

}
}

推荐答案





到目前为止代码你除了belo之外,上面发布的内容很好w part。

Hi,

So far the code you''ve posted above is fine except the below part.
//int pid;
//string pname, pimg;

con = new SqlConnection(ConfigurationManager.ConnectionStrings["connectionstring"].ConnectionString);

com = con.CreateCommand();

com.CommandText = "insert into cart select pid,pname,pimg,price from table_name where pid=@pid";
//com.Parameters.AddWithValue("@pid", pid);
//com.Parameters.Add("id", SqlDbType.Int).Value = 1;

com.Parameters.AddWithValue("@pname",pname);
com.Parameters.AddWithValue("@pimg",pimg);
com.Parameters.AddWithValue("@price",price);





这里没有声明像pid,pname,pimg和price这样的变量。

你必须在方法的开始或从用户输入获取值并在此处分配。



Here the variables like pid, pname, pimg and price are not declared.
You have to either declare them in the start of the method or get the values from user input and assign here.


这篇关于添加的购物车项目显示在数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆