Click here to Skip to main content
15,665,166 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
This Is my Gridview
when user click add to cart button corresponding product id will got from using

link Button Event Code

My problem is when user click addtocart for multiple products,
how to store values in variable one by one ,example like(9,11,15) and session that total clicked productid to next page,and convert back to string format ,because my sql query like this (select * from products where product='9'AND'14'And'16')
please help me

protected void LinkButton1_Click(object sender, EventArgs e)

GridViewRow gr = ((sender as LinkButton).NamingContainer as GridViewRow);
List<string> myList = new List<string>();

string s = string.Join(",", myList.ToArray());
Session["ss"] = s;


other page
String h = Session["ss"].ToString();
but itshow only last item cliked
Updated 1-Mar-14 2:07am

1 solution

That SQL query won't work anyway:
product='9' AND '14' AND '16'
Is not a valid boolean expression (ignoring that it logically can't be true under any circumstances even if your corrected it)

So what you want to use is
SELECT * FROM products WHERE product IN (9, 14, 16)
Assuming you have stored numbers in your table not strings.
So...can you convert a list to a string, with commas? Yes:
List<int> myList = new List<int>();
string s = string.Join(",", myList.Select(i => i.ToString()));

Or, you could store the Session variable as a string:
Session["MyList"] = (string) Session["MyList"] + "," + product;

In fact, you don't need the Select:
List<int> myList = new List<int>();
string s = string.Join(",", myList);
Will do exactly the same thing... :doh:
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