I'm building a website with a shopping cart.
I want all Cart's Item will appear on master page.
This is my SiteMaster.Master.cs:
protected void Page_Load(object sender, EventArgs e)
{
System.Web.Security.MembershipUser user = System.Web.Security.Membership.GetUser();
IBLClient b = new FrontEnd();
if (user != null)
Session["UserID"] = b.GetUserIDByUsername(user.UserName);
if (Session["Cart"] == null)
{
Session["Cart"] = new List<string>();
}
if (!(Roles.IsUserInRole("Admin") || Roles.IsUserInRole("Seller")))
{
ManagePage.Visible = false;
}
}
public BindingList<Product> GetProductsCart()
{
IBLClient b = new FrontEnd();
BindingList<Product> ProductsList = new BindingList<Product>();
if (Session["Cart"] != null)
{
foreach (string ProductId in (List<string>)Session["Cart"])
{
ProductsList.Add(b.GetProduct(int.Parse(ProductId)));
}
}
return ProductsList;
}
And this is my Source code:
<asp:DataList ID="ProductsList" runat="server" CellPadding="4" DataSourceID="ProductsListObjectDataSource" ForeColor="#333333">
<AlternatingItemStyle BackColor="White" />
<ItemTemplate>
</ItemTemplate>
<SelectedItemStyle BackColor="#C5BBAF" Font-Bold="True" ForeColor="#333333" />
</asp:DataList>
<asp:ObjectDataSource ID="ProductsListObjectDataSource" runat="server" SelectMethod="GetProductsCart" TypeName="WebGUI.SiteMaster"></asp:ObjectDataSource>
When the Page is load it's look like this:
But after the ObjectDataSource
's SelectMethod
GetProductsCart() function, I got this exeption and the this
changed.
Any idea why it's happening?
What should I do to avoid it?
I should do context.Session
this is the problem!!