locked
cart display problem(InvalidCastException) RRS feed

  • Question

  • User-566823024 posted

    Well basically im having a problem in this line below

    CartItem = CType(CartEntry.Value, CartItem)

    It says - Unable to cast object of type 'CartItem' to type 'CartItem'.

    This is the public cart item coding

    Public Class CartItem
    Public Product As Productpage
    Public Quantity As Integer

    End Class


    and this is my display cart item class

    Private Sub DisplayCart()
    ListBox1.Items.Clear()
    Dim CartItem As CartItem
    Dim CartEntry As DictionaryEntry
    For Each CartEntry In Cart
    CartItem = CType(CartEntry.Value, CartItem)
    ListBox1.Items.Add(CartItem.Display)
    Next
    End Sub


    Thanks for help in advance

    Sunday, March 28, 2010 11:49 AM

Answers

  • User-876702696 posted

    Hey - try and debug this - I am guessing you might have a null object or maybe you have CartItem defined more than once in different namespaces - try and do the debugging as described above and let us know the full types of both the object it's trying to case - and the type it's trying to cast it to.

    You should be able to get both by doing the hovering as described above.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Sunday, March 28, 2010 12:33 PM

All replies

  • User-876702696 posted

    Hey - could you post the full exception - you'd normally get the full qualified type of each object on exceptions like this one.

    Alternatively - have you tried attaching the debugger (or just running in debug mode) and then force the error to happen - if you have your VS set to break on exceptions (in debug -> Exceptions -< Common Language Runtime -> then select ur language which looks like VB) you shoudl be able to then hover over the objects with your mouse pointer and inspect what you're trying to cast.


    Sunday, March 28, 2010 11:59 AM
  • User-566823024 posted

    Here's the exception

    System.InvalidCastException was unhandled by user code
      Message="Unable to cast object of type 'CartItem' to type 'CartItem'."
      Source="App_Web_hcggexoc"
      StackTrace:
           at Cart.DisplayCart() in C:\Users\CoLdANg3L\Documents\Visual Studio 2008\WebSites\WebSite3\Cart.aspx.vb:line 28    at Cart.Page_Load(Object sender, EventArgs e) in C:\Users\CoLdANg3L\Documents\Visual Studio 2008\WebSites\WebSite3\Cart.aspx.vb:line 12    at System.Web.UI.Control.OnLoad(EventArgs e)    at System.Web.UI.Control.LoadRecursive()    at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)
      InnerException:


    and an additional info, this happens whenever i try to continue to the cart page with a quantity inserted into my product page

    Sunday, March 28, 2010 12:11 PM
  • User-876702696 posted

    Hey - try and debug this - I am guessing you might have a null object or maybe you have CartItem defined more than once in different namespaces - try and do the debugging as described above and let us know the full types of both the object it's trying to case - and the type it's trying to cast it to.

    You should be able to get both by doing the hovering as described above.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Sunday, March 28, 2010 12:33 PM
  • User-566823024 posted

    thank you for making me checkin upon variables, double checked and found some overlapping, it works fine now!thanks alot! :)

    Sunday, March 28, 2010 6:37 PM