Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Web Development
  3. ASP.NET
  4. C# ASP - System.InvalidCastException in foreach loop

C# ASP - System.InvalidCastException in foreach loop

Scheduled Pinned Locked Moved ASP.NET
csharphelp
8 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • I Offline
    I Offline
    InvalidTypecast
    wrote on last edited by
    #1

    I keep getting this error when trying to read items from my shopping cart session. I've tracked it down to the foreach loop I am doing. Unable to cast object of type 'BasketItem' to type 'BasketItem'. Exception Details: System.InvalidCastException: Unable to cast object of type 'BasketItem' to type 'BasketItem'. foreach (BasketItem theItem in items) { count++; total += theItem.ReturnTotal(); } items is defined as this: ArrayList items = (ArrayList)Session["shoppingCart"]; Session["shoppingCart"] is defined as the ArrayList cartArray: ArrayList cartArray; Session["shoppingCart"] = cartArray; The BasketItem class is a protected class with a string, an int, and 2 doubles that holds information about the product and price. I checked the session to make sure the data is in it correctly. It seems to be fine because I can get the data out of the session and bind it to a datagrid with no issues. For some reason it doesn't like me doing a foreach loop. This works fine: ArrayList items = (ArrayList)Session["shoppingCart"]; GridView2.DataSource = items; GridView2.DataBind(); I am lost on this one and hope someone can provide some insight. Thank you.

    S A 2 Replies Last reply
    0
    • I InvalidTypecast

      I keep getting this error when trying to read items from my shopping cart session. I've tracked it down to the foreach loop I am doing. Unable to cast object of type 'BasketItem' to type 'BasketItem'. Exception Details: System.InvalidCastException: Unable to cast object of type 'BasketItem' to type 'BasketItem'. foreach (BasketItem theItem in items) { count++; total += theItem.ReturnTotal(); } items is defined as this: ArrayList items = (ArrayList)Session["shoppingCart"]; Session["shoppingCart"] is defined as the ArrayList cartArray: ArrayList cartArray; Session["shoppingCart"] = cartArray; The BasketItem class is a protected class with a string, an int, and 2 doubles that holds information about the product and price. I checked the session to make sure the data is in it correctly. It seems to be fine because I can get the data out of the session and bind it to a datagrid with no issues. For some reason it doesn't like me doing a foreach loop. This works fine: ArrayList items = (ArrayList)Session["shoppingCart"]; GridView2.DataSource = items; GridView2.DataBind(); I am lost on this one and hope someone can provide some insight. Thank you.

      S Offline
      S Offline
      Sathesh Sakthivel
      wrote on last edited by
      #2

      InvalidTypecast wrote:

      Exception Details: System.InvalidCastException: Unable to cast object of type 'BasketItem' to type 'BasketItem'.

      Represents the error that occurs when an explicit conversion (casting operation) fails because the source type cannot be converted to the destination type. InvalidCastException Class[^]

      Regards, Satips.

      I 1 Reply Last reply
      0
      • I InvalidTypecast

        I keep getting this error when trying to read items from my shopping cart session. I've tracked it down to the foreach loop I am doing. Unable to cast object of type 'BasketItem' to type 'BasketItem'. Exception Details: System.InvalidCastException: Unable to cast object of type 'BasketItem' to type 'BasketItem'. foreach (BasketItem theItem in items) { count++; total += theItem.ReturnTotal(); } items is defined as this: ArrayList items = (ArrayList)Session["shoppingCart"]; Session["shoppingCart"] is defined as the ArrayList cartArray: ArrayList cartArray; Session["shoppingCart"] = cartArray; The BasketItem class is a protected class with a string, an int, and 2 doubles that holds information about the product and price. I checked the session to make sure the data is in it correctly. It seems to be fine because I can get the data out of the session and bind it to a datagrid with no issues. For some reason it doesn't like me doing a foreach loop. This works fine: ArrayList items = (ArrayList)Session["shoppingCart"]; GridView2.DataSource = items; GridView2.DataBind(); I am lost on this one and hope someone can provide some insight. Thank you.

        A Offline
        A Offline
        Arun Immanuel
        wrote on last edited by
        #3

        what does cartArray points to? try this: foreach (Object theItem in items) { count++; total += ((BasketItem)theItem).ReturnTotal(); }

        Regards, Arun Kumar.A

        I 1 Reply Last reply
        0
        • S Sathesh Sakthivel

          InvalidTypecast wrote:

          Exception Details: System.InvalidCastException: Unable to cast object of type 'BasketItem' to type 'BasketItem'.

          Represents the error that occurs when an explicit conversion (casting operation) fails because the source type cannot be converted to the destination type. InvalidCastException Class[^]

          Regards, Satips.

          I Offline
          I Offline
          InvalidTypecast
          wrote on last edited by
          #4

          I understand the error, but I just can't figure out why it can't convert. It seems like it should.

          1 Reply Last reply
          0
          • A Arun Immanuel

            what does cartArray points to? try this: foreach (Object theItem in items) { count++; total += ((BasketItem)theItem).ReturnTotal(); }

            Regards, Arun Kumar.A

            I Offline
            I Offline
            InvalidTypecast
            wrote on last edited by
            #5

            Thanks everyone. I figured it out. I believe what I was doing wrong was declaring the BasketItem class in Default.aspx.cs and Default2.aspx.cs (where I was reading the session passed from Default.aspx). I moved the class into the AppCode folder into its own .cs file, commented out the class definition from the other two cs files, and the problem went away.

            A 1 Reply Last reply
            0
            • I InvalidTypecast

              Thanks everyone. I figured it out. I believe what I was doing wrong was declaring the BasketItem class in Default.aspx.cs and Default2.aspx.cs (where I was reading the session passed from Default.aspx). I moved the class into the AppCode folder into its own .cs file, commented out the class definition from the other two cs files, and the problem went away.

              A Offline
              A Offline
              Arun Immanuel
              wrote on last edited by
              #6

              Still why your name "InvalidTypecast" is unaltered? ;););)

              Regards, Arun Kumar.A

              S 1 Reply Last reply
              0
              • A Arun Immanuel

                Still why your name "InvalidTypecast" is unaltered? ;););)

                Regards, Arun Kumar.A

                S Offline
                S Offline
                Sathesh Sakthivel
                wrote on last edited by
                #7

                Still he he could not get any other Name it seems.

                Regards, Satips.

                A 1 Reply Last reply
                0
                • S Sathesh Sakthivel

                  Still he he could not get any other Name it seems.

                  Regards, Satips.

                  A Offline
                  A Offline
                  Arun Immanuel
                  wrote on last edited by
                  #8

                  ;););)

                  Regards, Arun Kumar.A

                  1 Reply Last reply
                  0
                  Reply
                  • Reply as topic
                  Log in to reply
                  • Oldest to Newest
                  • Newest to Oldest
                  • Most Votes


                  • Login

                  • Don't have an account? Register

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • World
                  • Users
                  • Groups