Results 1 to 6 of 6

Thread: specified cast is invalid

  1. #1

    Thread Starter
    Hyperactive Member r0k3t's Avatar
    Join Date
    Dec 2005
    Location
    Cleveland
    Posts
    361

    specified cast is invalid

    I am working on someone else's code and I ran into a situation where I was able to cut and paste a rather large chunk of their work - cool, saved a lot of time. The only weird thing was that they have this.
    Code:
    contact.Id = (int)Session["ContactId"];
    When i tried to run that in my new page I get an error saying that the specified cast is invalid. First I thought maybe I was losing the session var somehow - that isn't the case so I did this...

    Code:
    int contactId = int.Parse(Session["ContactId"].ToString());
    contact.Id = contactId;
    Works just fine!

    Anyone have any idea why this works in one page and not the other? Maybe the other developer is just cooler than I am? (just kidding)

    Thanks

  2. #2
    Banned timeshifter's Avatar
    Join Date
    Mar 2004
    Location
    at my desk
    Posts
    2,465

    Re: specified cast is invalid

    Firstly, a Session[] return is an object type. You can't cast an object as an int. If you were to do (int)Session["ContactID"].ToString(), you'd get a similar error, because you can't cast a string as an int. The last line works because that's precisely what the int.Parse() method is for. It's purpose is to convert as much of a string as possible to an int.

  3. #3
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: specified cast is invalid

    Just to clarify something timeshifter posted. You CAN cast an object as type int as long as the object refers to an int. This is valid:
    CSharp Code:
    1. object obj = 100;
    2. int n = (int)obj
    The obj variable refers to a boxed int value so the cast is valid. This, on the other hand, is not valid:
    CSharp Code:
    1. object obj = "100";
    2. int n = (int)obj
    The obj variable refers to a string, which is not an int so it cannot be cast as type int. In order for a cast to be valid the object referred to by the variable being cast must actually be the type being cast to.

    Now, the question is, what type of object does Session["ContactId"] actually refer to? You can determine that like this:
    Code:
    Session["ContactId"].GetType().ToString()
    Write that value somewhere and you'll know what type the object is.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  4. #4

    Thread Starter
    Hyperactive Member r0k3t's Avatar
    Join Date
    Dec 2005
    Location
    Cleveland
    Posts
    361

    Re: specified cast is invalid

    Hmm? I just stuck a gettype in and put in a break point. This is what is strange. The object in the session variable gets set - as an int - just like you pointed out in the previous post... On the one page it works cause the cast indeed sees that it is an int, on the other page for some reason the system is seeing it as a string. It is still the same session variable - does asp.net at some point say "I don't know what to do with this? I will make it a string..." I don't know - it is weird.

    Thanks

  5. #5
    Frenzied Member
    Join Date
    May 2006
    Location
    Toronto, ON
    Posts
    1,093

    Re: specified cast is invalid

    Is the page where it works the one where it gets set? I'm not sure of the inner workings of the Session object, but it may be that the page where it gets set remembers that it's an int, but when you access it from a different page, it just grabs a bunch of objects from the Session data and so doesn't know the underlying type for them and you can't just box it.

  6. #6
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: specified cast is invalid

    I can't see anything in the documentation to say that all session data become strings, but then I'm not an ASP.NET expert.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width