Invalid Cast Exception using Server.Transfer

Discussion in 'ASP .Net' started by buzz, Mar 18, 2005.

  1. buzz

    buzz Guest

    I am attempting to pass data between two web forms pages. I
    have found the method to do this on the msdn site here:

    After attempting to do this myself, I kept getting an Invalid Cast
    Exception error, so I finally resorted to simply copying and pasting
    the MS code in this article into two pages for the purpose of testing.
    Surprisingly, I found myself with the exact same invalid cast error. No
    idea what I'm missing here.
    buzz, Mar 18, 2005
    1. Advertisements

  2. buzz

    Scott Allen Guest


    Do you know what line of code was throwing an InvalidCast error?
    Scott Allen, Mar 18, 2005
    1. Advertisements

  3. buzz

    buzz Guest

    Line 16, where i am casting the Context.Handler to the classname
    specified in the page directive of the sending page.
    buzz, Mar 18, 2005
  4. buzz

    Scott Allen Guest

    HI Buzz:

    If it's a line like this:

    FirstPageClass fp = (FirstPageClass)Context.Handler;

    then Handler isn't a FirstPageClass object. What was the name of the
    webform class that did the Server.Transfer?
    Scott Allen, Mar 18, 2005
  5. buzz

    buzz Guest

    Well, I'm presuming that the name of the webform class is the what was
    specified as the ClassName="whatever" portion of the @ Page directive
    on the sending page. At least, that is the way it is explained in the
    article. Like I said in my original post, I copied the code from the
    microsoft article exactly, so the ClassName in the page directive of
    the sending page is "FirstPageClass"

    My understanding was that the Context.Handler contained all the
    pertinent info when the Server.Transfer took place, and that you needed
    to create an instance of the sending page class, then set it equal to
    the Context.Handler. Because the Context.Handler is generic, it has to
    be cast as the sending page class in order to do this.

    But that's where I have a problem. I'm just lost, given that the MS
    code does the exact same thing. I haven't made any code changes to the
    code they posted in the article.
    buzz, Mar 18, 2005
  6. buzz

    Scott Allen Guest

    Hi buzz:

    Yes, that should be working for you then. I'm not sure what would be
    going wrong.

    that's all correct, except your code doesn't need to create an
    instance of the sending page class, it just declares a variable of the
    type and gets the instance from Context.Handler.
    Are you using VisualStudio? In the debugger you can stop execution on
    that line of code and inspect Context.Handler to get information about
    Scott Allen, Mar 18, 2005
  7. buzz

    buzz Guest

    No, i'm not using studio. Just using DreamweaverMX using code view to
    do my stuff. Actually, I've narrowed down the problem to something on
    my laptop. I loaded the files up to a server and it worked ok. Not sure
    what is different, or if there is maybe something I need to install on
    my laptop??? Laptop is an XP Pro workstation... My web stuff has always
    worked previously on my laptop...
    buzz, Mar 18, 2005
  8. buzz

    Guest Guest

    Write out to the Trace or Response.write the Context.Handler type. Usually
    ToString() will give you this.
    Guest, Mar 18, 2005
  9. buzz

    buzz Guest

    Well, i think i have it figured out. The problem was not with my code,
    but rather with how i was accessing the pages. I was using the preview
    function of Dreamweaver, which renders your page out to a temp file and
    then displays it in a browser. That was my problem. The page reference
    in the second page was not working because the first page was a temp
    file. I accessed the pages normally using a browser, outside of DW, and
    it worked perfectly. Thanks for all the suggestions.
    buzz, Mar 18, 2005
  10. buzz

    Scott Allen Guest

    Ah, I'm glad it's working now. It sounded like you had everything
    lined up right.
    Scott Allen, Mar 19, 2005
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.