Click here to Skip to main content
15,940,550 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am writing to seek advice, if you can store a user_id in a session.

when execute the below method (add_to_cart()), it throws the following error on the line below:
System.InvalidCastException: Unable to cast object of type 'System.String' to type 'System.Collections.Generic.List`1[System.String]'.

List<String> cart = new List<String>();<br />
cart = (List<string>)Session["UserAuthentication"];

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["UserAuthentication"] == null)
               Session["UserAuthentication"] = selectedProducts;

             //  string SessionData = Session["UserAuthentication"].ToString();

               List<String> cart = new List<String>();
               cart = (List<string>)Session["UserAuthentication"];

               foreach (var product in selectedProducts)
               Session["UserAuthentication"] = cart;
           foreach (GridViewRow row in GridView1.Rows)
               CheckBox cb = (CheckBox)row.FindControl("SelectedProducts");
               if (cb.Checked)
                   cb.Checked = true;
               catch (Exception ex)
                   throw ex;

could this error be related how the data is stored in the session variable. I start the session, when user logs in, but I am only storing username(string). Could this issue be causing the error in the (add_to_cart() method?

protected void ValidateUser(object sender, EventArgs e)
            string constr = ConfigurationManager.ConnectionStrings["#######"].ConnectionString;
            using (SqlConnection con = new SqlConnection(constr))
                using (SqlCommand cmd = new SqlCommand("Validate_User"))
                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.Parameters.AddWithValue("@Username", Login1.UserName);
                    cmd.Parameters.AddWithValue("@Password", Login1.Password);
                    cmd.Connection = con;

                    SqlDataReader dr = cmd.ExecuteReader();
                    if (dr.Read())
                        Session["UserAuthentication"] = Login1.UserName;
                       // FormsAuthentication.RedirectFromLoginPage(Login1.UserName, Login1.RememberMeSet);

Please advice further.
Thank you.
Updated 21-Nov-14 3:01am

1 solution

You would be using this code instead of that one,

List<string> cart = new List<string>();
// Add it to the list

This way you can add a new element to the generic list you're having. You donot convert a string to a List<string>.
Share this answer
miss786 21-Nov-14 9:03am    
thank you for your quick response. I changed my code, as you suggested but the above line, still gives a similar error (i.e. cannot convert from object to string). Please advice further if possible. Thanks
Thanks7872 21-Nov-14 9:29am    
Try cart.Add(Convert.Tostring(Session["UserAuthentication"]));
Afzaal Ahmad Zeeshan 21-Nov-14 10:08am    
Thank you for standing up for me Rohan. :-)
Thanks7872 21-Nov-14 11:25am    
Afzaal Ahmad Zeeshan 21-Nov-14 10:08am    
Try the code now. :-)

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