Click here to Skip to main content
15,892,517 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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");
}

}
}
Posted

1 solution

Hi,

So far the code you've posted above is fine except the below part.
C#
//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);


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.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900