Click here to Skip to main content
15,886,919 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Hello
I want to create an online store web site.
I have created a datalist which shows my products in my site,I put the LinkButton in my datalist for getting the productID of the product which the user select and add it to shopping cart .

ASP.NET
<asp:LinkButton ID="LinkButton1" runat="server" Text="Add To Cart" CommandName="addtocart" CommandArgument='<%# Eval("id")%>' >


when the user click the linkbutton this event fire:

C#
 protected void theDataList_ItemCommand(object source, DataListCommandEventArgs e)
    {
    //this add the selected product-id to the list<int> and put it in the 
//  session["addtocart"]
            if (e.CommandName == "addtocart")
            {                                 
                int productid = Convert.ToInt32(e.CommandArgument);
                Label6.Text = productid.ToString();
                
           
                List<int> productsincart = (List<int>)Session["addtocart"];
                if (productsincart == null)
                {
                    productsincart = new List<int>();
                }
                
                productsincart.Add(productid);
               
                Session["addtocart"] = productsincart;
    } 

after that when the user click the button which It's text is "show shopping cart",
the user will see the shoppingcart.aspx page
C#
Response.Redirect("shoppingcart.aspx");

in this page I have a gridview and I want to bind it to session["addtocart"];
when the page load the gridview show the selected products which their ids are in the session["cart"]
but it does not work and this error happened:
System.InvalidCastException: Object must implement IConvertible.
this is the related code:
ASP.NET
<asp:GridView ID="GridView3" runat="server"  
            DataSourceID="SqlDataSource1"   >
            
            <columns>
                <asp:BoundField DataField="id" HeaderText="id" SortExpression="id" />
                <asp:BoundField DataField="name" HeaderText="post" SortExpression="post" />
                <asp:BoundField DataField="price" HeaderText="salary" 
                    SortExpression="salary" />
                <asp:BoundField DataField="color" HeaderText="years" SortExpression="years" />
               
            </columns>
        
        <asp:SqlDataSource ID="SqlDataSource1" runat="server" 
            ConnectionString="<%$ ConnectionStrings:employeeConnectionString4 %>" 
            SelectCommand="SELECT * FROM [products] WHERE ([productid] = @productid)">
            <SelectParameters>
                <asp:SessionParameter DefaultValue="7" Name="cart" SessionField="cart" 
                    Type="Int32" />
            </SelectParameters>

I dont know how to solve it if any body help me I become so Thankful.
Posted
Updated 4-Oct-12 6:31am
v2
Comments
elmirag 6-Oct-12 10:47am    
can anyone help me?I really need help

1 solution

I just recommend you to put a break point inside the method 'protected void theDataList_ItemCommand' and start debugging it so that you can know where the exception is coming from.
It seems to me the error is coming from the conversion from the session object to the Integer List, but I am not sure, just debug it and good luck.
 
Share this answer
 
Comments
elmirag 5-Oct-12 6:19am    
I know the problem is related to the GridView3 and the Session["addtocart"],I Think the GridView3 can not get the value from the list<int> object which is stored in the Session["addtocart"] but I Dont know how to solve this problem

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