When the gridview is set to display values ​​from a session array array, the following error is displayed: "Object must implement IConvertible." in c #

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

on the shopping cart page ive got a gridview that reads name, price from db element where session ["array"] = itemID

the error is displayed when loading the shopping cart page.

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

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

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

      

}

Cart page only has gridview where sql statement

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

value ([ItemID] = @ItemID) is the session ("array")

what appears to be the problem here?

early.

// 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 itemID now contains the values ​​"3.16"

I put a query string in the query builder window and got this error when I start the site.

"Invalid column name ' + itemIDs + '. "

      

0


a source to share


1 answer


It looks like you are trying to assign an ArrayList as a parameter to your request, which is not possible. You will need to collect a string containing a comma-separated list of the contents of the ArrayList and use that to build the query rather than passing it as a parameter. Note that this will also require changing the query string, 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 + ")"

      

0


a source







All Articles