Click here to Skip to main content
15,888,329 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am trying build a function, when I check a checkbox control, i create cart session which stories list of data and from that data, I would like to get product_id.

when I try to execute the below function, I get a cast exception error on the line below:
System.InvalidCastException: Unable to cast object of type 'System.Collections.Generic.List`1[System.String]' to type 'System.String'.
string data = (string)Session["Cart"];


I tried calling data from the checkbox variable as well, but i also kept getting another error, informing me that the input format is incorrect.
Whole Method:
C#
protected void AddToCart_Click(object sender, EventArgs e)
{
    var selectedProducts = GridView1.Rows.Cast<GridViewRow>()
      .Where(row => ((CheckBox)row.FindControl("SelectedProducts")).Checked)
      .Select(row => GridView1.DataKeys[row.RowIndex].Value.ToString()).ToList();
    if (Session["Cart"] == null)
    {
        Session["Cart"] = selectedProducts;
    }
    else
    {
        List<String> cart = new List<String>();
        cart = (List<string>)Session["Cart"];
       // cart.Add(Session["Cart"]);
        foreach (var product in selectedProducts)
            cart.Add(product);
        Session["Cart"] = cart;
    }
    foreach (GridViewRow row in GridView1.Rows)
    {
        CheckBox cb = (CheckBox)row.FindControl("SelectedProducts");
        if (cb.Checked)
            cb.Checked = true;

            {
              string data = (string)Session["Cart"];
                Label1.Text = data.ToString();

                // Access chb.Text;
                //int num = Convert.ToInt32(cb.Text);
               // Label1.Text = num.ToString();
            }
    }
}

Any help, would be very much appreciated.
Many thanks
Posted
Comments
Kornfeld Eliyahu Peter 24-Nov-14 9:44am    
It seams Session["Cart"] has the type List<t> - it can not be casted into a single string!!!

Why would you expect it to convert a List of strigns to a string for you?
If you wrote this:
C#
List<string> list = new List<string>();
string s = (string) list;
You would expect an error woudln;t you?
So when you do this:
C#
            var selectedProducts = GridView1.Rows.Cast<gridviewrow>()
              .Where(row => ((CheckBox)row.FindControl("SelectedProducts")).Checked)
              .Select(row => GridView1.DataKeys[row.RowIndex].Value.ToString()).ToList();
            if (Session["Cart"] == null)
            {
                Session["Cart"] = selectedProducts;
...
                      string data = (string)Session["Cart"];</gridviewrow>

That is exactly what your are trying to do!
Try getting the list from the session instead?
C#
List<string> data = (List<string>)Session["Cart"];</string></string>
 
Share this answer
 
Assign Data
Session["Cart"] = selectedProducts;

Retrieve Data
string data = (string)Session["Cart"];
 
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