Click here to Skip to main content
15,896,497 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
This Is my Gridview
http://i59.tinypic.com/2l94i1k.jpg
when user click add to cart button corresponding product id will got from using
http://i58.tinypic.com/2cymrt4.jpg

link Button Event Code


http://i62.tinypic.com/212d6e0.jpg


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>();

myList.Add(gr.Cells[0].Text.Trim());
string s = string.Join(",", myList.ToArray());
Session["ss"] = s;

}

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

1 solution

That SQL query won't work anyway:
SQL
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
SQL
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:
C#
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:
C#
Session["MyList"] = (string) Session["MyList"] + "," + product;



[edit]
In fact, you don't need the Select:
C#
List<int> myList = new List<int>();
...
string s = string.Join(",", myList);
Will do exactly the same thing... :doh:
[/edit]
 
Share this answer
 
v2

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