Checking ID against Access Table and looping through an SQL statement
I posted a question yesterday for Selecting an item in a gridview and then adding it to the list, which I found a solution for, it takes the value of the gridview, in this case an ID num开发者_StackOverflowber and then outputs them on another page in a gridview.
My question is, is it possible to check the value of the ID against a table in an access database to then display more information about the product?
I have the following code on my Basket page:
protected void Page_Init(object sender, EventArgs e)
{
if (Session["CartSess"] != null)
{
List<BasketClass> cart = (List<BasketClass>)Session["CartSess"];
foreach (BasketClass BookID in (List<BasketClass>)Session["CartSess"])
{
GridView1.DataSource = cart;
GridView1.DataBind();
AccessDataSource1.SelectCommand = "SELECT [BookID], [book_title] FROM [tblBook] WHERE [BookID]=" + BookID;
}
}
}
The problem is, it only returns one value based upon the last one that the user has chosen, is there a way to loop through each of them so it will keep adding to the new gridview that I have placed on the page which is bound to AccessDataSource1?
I would try something like:
string items = "";
string sql = "SELECT [BookID], [book_title] FROM [tblBook] WHERE [BookID] in (";
foreach (BasketClass BookID in (List<BasketClass>)Session["CartSess"])
{
items += BookID & ",";
}
//Remove the trailing ","
items.Remove(str.Length - 1, 1);
GridView1.DataSource = cart;
GridView1.DataBind();
AccessDataSource1.SelectCommand = sql & items & ")";
精彩评论