Click here to Skip to main content
15,885,366 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
This error keeps popping up and I can't seem to figure out where it's coming from. Any jelp is appreciated. Thanks

if (!IsPostBack)
{
    DataTable LocalCart = new DataTable();
    LocalCart = (DataTable)Session["cart"];
    int LocalCartItemCount = (int) Session["CartItemCount"];
    Decimal LocalCartAmount = (Decimal)Session["CartAmount"];

    if (LocalCart.Rows.Count == 0)
    {
        titleLabel.Text = "Your shopping cart is empty!";
        GridCart.Visible = false;
        updateButton.Enabled = false;
        checkoutButton.Enabled = false;
        totalAmountLabel.Text = String.Format("{0:c}", 0);
    }
    else
    {
        GridCart.DataSource = LocalCart;
        GridCart.DataBind();
        titleLabel.Text = "These are the products in your shopping cart:";
        GridCart.Visible = true;
        updateButton.Enabled = true;
        checkoutButton.Enabled = true;
        totalAmountLabel.Text = String.Format("{0:c}", LocalCartAmount);
    }


It's saying the error is here ->
int LocalCartItemCount = (int) Session["CartItemCount"];
Posted
Comments
Sergey Alexandrovich Kryukov 18-Apr-13 15:38pm    
This is exception. Not error, exception. Always provide comprehensive exception information.
—SA

Well, it's easy really. The statement Session["CartItemCount"] is returning null. This is probably because there is no value assigned to the "CartItemCount" in the Session variable or "CartItemCount" doesn't exist at all.
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 18-Apr-13 15:41pm    
Sure, a 5.
—SA
fjdiewornncalwe 18-Apr-13 16:15pm    
+5.
Hi,

Most probably Session["CartItemCount"] does not have a value inside.

Change the following line please:
int LocalCartItemCount = (int) Session["CartItemCount"];

for
int LocalCartItemCount = Session["CartItemCount"]==null ? 0 : Convert.ToInt32(Session["CartItemCount"]);


good luck,
jafc
 
Share this answer
 
Comments
Arshad Jugon 18-Apr-13 13:13pm    
I tried it works, but the same error popped up at another place.

here -> if (LocalCart.Rows.Count == 0) [Same Error]

my updated code

protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{


DataTable LocalCart = new DataTable();
LocalCart = (DataTable)Session["cart"];
int LocalCartItemCount = Session["CartItemCount"] == null ? 0 : Convert.ToInt32(Session["CartItemCount"]);
Decimal LocalCartAmount = Session["CartAmount"] == null ? 0 : Convert.ToDecimal(Session["CartAmount"]);


if (LocalCart.Rows.Count == 0)
{
titleLabel.Text = "Your shopping cart is empty!";
GridCart.Visible = false;
updateButton.Enabled = false;
checkoutButton.Enabled = false;
totalAmountLabel.Text = String.Format("{0:c}", 0);
}
else
{
GridCart.DataSource = LocalCart;
GridCart.DataBind();
titleLabel.Text = "These are the products in your shopping cart:";
GridCart.Visible = true;
updateButton.Enabled = true;
checkoutButton.Enabled = true;
totalAmountLabel.Text = String.Format("{0:c}", LocalCartAmount);
}
}
}
Sergey Alexandrovich Kryukov 18-Apr-13 15:40pm    
Look, are you going to ask the same question again and again every time you face with the exception? Instead of trying to understand the answer you got?
—SA
José Amílcar Casimiro 18-Apr-13 14:53pm    
Your problem begins in this line: "LocalCart = (DataTable)Session["cart"];"
Do some work now please.
Sergey Alexandrovich Kryukov 18-Apr-13 15:41pm    
Agree, OP needs to do some brain work. My 5 for the answer, by the way.
—SA
José Amílcar Casimiro 18-Apr-13 16:24pm    
Agree. Thx.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900