Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
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 18-Apr-13 7:48am
Comments
Sergey Alexandrovich Kryukov at 18-Apr-13 15:38pm
   
This is exception. Not error, exception. Always provide comprehensive exception information.
—SA
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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.
  Permalink  
Comments
Sergey Alexandrovich Kryukov at 18-Apr-13 15:41pm
   
Sure, a 5.
—SA
Marcus Kramer at 18-Apr-13 16:15pm
   
+5.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

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
  Permalink  
Comments
Arshad Jugon at 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 at 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 Ferreira Casimiro at 18-Apr-13 14:53pm
   
Your problem begins in this line: "LocalCart = (DataTable)Session["cart"];"
Do some work now please.
Sergey Alexandrovich Kryukov at 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 Ferreira Casimiro at 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
0 OriginalGriff 587
1 Sergey Alexandrovich Kryukov 479
2 Maciej Los 305
3 Mathew Soji 195
4 Afzaal Ahmad Zeeshan 170
0 OriginalGriff 7,356
1 Sergey Alexandrovich Kryukov 6,777
2 DamithSL 5,461
3 Manas Bhardwaj 4,916
4 Maciej Los 4,475


Advertise | Privacy | Mobile
Web01 | 2.8.1411023.1 | Last Updated 18 Apr 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100