Hi!
I have created a simple online shopping application, where if a user clicks on a product image he will be directed to the Product Details page. My question is how to get the Product Id of that particular Product that the user has clicked using query string. I have tried the following code.
protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
{
Response.Redirect("Product_Details.aspx?Product_Id={0}");
}
In the Product details page, the code is as given below:-
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
FillDataList();
}
private void FillDataList()
{
cmd = new SqlCommand("select Product_Category_Name, Product_Id, Product_Name, Product_Image, Price from Product_Category_Mst a, Product_Mst b where a.Product_Category_Id = b.Product_Category_Id and Product_Id = @Product_Id", con);
int Product_Id = Convert.ToInt16(Request.QueryString["Product_Id"]);
cmd.Parameters.AddWithValue("@Product_Id", Product_Id);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
da.Fill(dt);
DataList1.DataSource = dt;
DataList1.DataBind();
}
Kindly help me out in this regard.
Abhishek KumarPosted Oct 6, 2014, 6:22 AM
Seems issue with the code response.Redirect as the query string is not properly sent.
try this.
protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
{
int PID=4;
Response.Redirect("Product_Details.aspx?Product_Id=" + PID ");
}
You can sent PID from the image button click not sure about your image button code that why hardcoded the value.
Hope this will help.
Abhishek
arun prasathPosted Oct 6, 2014, 4:23 AM
Anoop Kumar SharmaPosted Oct 6, 2014, 4:10 AM
I am sharing some Code which I have used earlier in my Online Shopping Web project. Try this.
====================
SqlConnection con = new SqlConnection(@"Data Source=.........;AttachDbFilename=|DataDirectory|\........;Integrated Security=True;");
con.Open();
//Change the query according to your need
SqlCommand com = new SqlCommand("select * from YourTable where ProductId=@Id",con);
SqlDataAdapter adapt = new SqlDataAdapter(com);
DataSet ds = new DataSet();
com.Parameters.AddWithValue("@Id", Page.Request.QueryString["ProductId"].ToString());
com.ExecuteNonQuery();
adapt.Fill(ds,"Product");
====================Hope this will Help you.