views:

870

answers:

1

when the user selects "add to cart" the itemID of that row item is added to arraylist which is then stored in a session.

in the shopping cart page ive got a gridview which reads name, price from item db where session["array"]=itemID

the error is displayed when the shopping cart page loads.

all i want to do is to get the list of items the user has selected which is in the arraylist and populate them in the gridview in the shopping cart page.

public partial class Drama_k : System.Web.UI.Page { ArrayList array;

protected void Page_Load(object sender, EventArgs e)
{
    if (Session["array"] == null)
    {
        array = new ArrayList();
        Session.Add("array", array);
    }
    else
        array = Session["array"] as ArrayList;
    GridView1.DataSource = array;
    GridView1.DataBind(); //Edit 2           
}



protected void GridView2_RowCommand(object sender, GridViewCommandEventArgs e)
{
    if (e.CommandName == "AddToCart")
    {
        int index = Convert.ToInt32(e.CommandArgument);
        array.Add(GridView2.DataKeys[index].Value.ToString());
    }
}

}

shopping cart page only has a gridview where the sql statement is

SELECT [ItemID], [Name], [RelDate], [Price], [Status] FROM [item_k] WHERE ([ItemID] = @ItemID)

value of ([ItemID] = @ItemID) is Session("array")

what seems to be the issue here?

thanks in advance.

//edit 2

ArrayList tmpArrayList = new ArrayList(); 
tmpArrayList = (ArrayList)Session["array"]; 
string itemIDs = string.Empty; 
foreach (object itemID in tmpArrayList) 
{ 
   itemIDs += itemID.ToString() + ','; 
} 
itemIDs = itemIDs.Substring(0, itemIDs.Length - 1);

where itemIDs now hold values "3,16"

I placed the query string in the query builder window and i get this error when i run the website.

"Invalid column name ' + itemIDs + '. "
A: 

It looks like you're trying to assign an ArrayList as a parameter to your query, which isn't possible. You'll have to assemble a string that contains a comma-separated list of the ArrayList's contents and use that to build the query, rather than passing it in as a parameter. Note that this will require changing the query string as well, using WHERE...IN instead of WHERE...=.

string itemIDs = string.Empty;
foreach (int itemID in array)
{
    itemIDs += itemID.ToString() + ',';
}
itemIDs = itemIDs.Substring(0, itemIDs.Length - 1);  // remove last comma

string query = "SELECT [ItemID], [Name], [RelDate], [Price], [Status] FROM [item_k] WHERE [ItemID] IN (" + itemIDs + ")"
refer //edit 2. this didnt work.
pier

related questions