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"];


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

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

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

               foreach (var product in selectedProducts)
                   cart.Add(product);
               Session["UserAuthentication"] = cart;
           }
           foreach (GridViewRow row in GridView1.Rows)
           {
               CheckBox cb = (CheckBox)row.FindControl("SelectedProducts");
               if (cb.Checked)
                   cb.Checked = true;
               try
               {
                   {
                       insert_port();
                   }
               }
               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?

C#
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;
                    con.Open();


                    SqlDataReader dr = cmd.ExecuteReader();
                    if (dr.Read())
                    {
                        con.Close();
                        Session["UserAuthentication"] = Login1.UserName;
                        Response.Redirect("~/Default.aspx");
                       // FormsAuthentication.RedirectFromLoginPage(Login1.UserName, Login1.RememberMeSet);
                    }


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

1 solution

You would be using this code instead of that one,

C#
List<string> cart = new List<string>();
// Add it to the list
cart.Add(Convert.ToString(Session["UserAuthentication"]));
</string></string>


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
 
v2
Comments
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    
Cheers...
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