Click here to Skip to main content
11,496,146 members (635 online)
The site is currently in read-only mode for maintenance. Posting of new items will be available again shortly.
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
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.
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 RyanDev 230
1 Sergey Alexandrovich Kryukov 114
2 Richard Deeming 100
3 OriginalGriff 89
4 Mostafa Asaduzzaman 59
0 Sergey Alexandrovich Kryukov 10,401
1 OriginalGriff 8,910
2 Sascha Lefèvre 3,899
3 Maciej Los 3,422
4 Richard Deeming 2,600


Advertise | Privacy | Mobile
Web03 | 2.8.150520.1 | Last Updated 18 Apr 2013
Copyright © CodeProject, 1999-2015
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