Results 1 to 5 of 5

Thread: Whats the wrong in this code ?? Vb.net

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Feb 2013
    Posts
    22

    Whats the wrong in this code ?? Vb.net

    Hello All


    why this error message is show to me ??


    Name:  vbforums.jpg
Views: 667
Size:  34.1 KB



    i call this function "SetAll()" from another function

    how to fix it

  2. #2
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Location
    South Louisiana
    Posts
    11,753

    Re: Whats the wrong in this code ?? Vb.net

    Please do not post an image of the issue, instead copy/paste the relevant code along with a text copy of the error message.
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | Code Tags | Sword of Fury - Jameram

  3. #3
    You don't want to know.
    Join Date
    Aug 2010
    Posts
    4,578

    Re: Whats the wrong in this code ?? Vb.net

    I can't read that language to provide a very accurate guess, but I do know this:

    InvalidCastException means you have "a thing" and tried to cast it to "something else", but it can't be converted that way.

    headElements is an HtmlElement, that's what VB is going to try to cast to. WebBrowser1.Document.GetElementsByTagName("head")(0) is obviously not an HtmlElement, or you wouldn't get this exception. You'll have to use the debugger to figure out what it is, and make sure to adjust for it.
    This answer is wrong. You should be using TableAdapter and Dictionaries instead.

  4. #4

    Thread Starter
    Junior Member
    Join Date
    Feb 2013
    Posts
    22

    Re: Whats the wrong in this code ?? Vb.net

    ah sorry bro

    this is the lines which has the error

    Code:
        Public Function SetAll()
            'On Error Resume Next
    
            Dim headElements As HtmlElement = WebBrowser1.Document.GetElementsByTagName("head")(0)
            Dim scriptElements As HtmlElement = WebBrowser1.Document.CreateElement("script")
            Dim element As IHTMLScriptElement = DirectCast(scriptElements.DomElement, IHTMLScriptElement)
    when i call it from another function

    the error message is

    Code:
    an unhandled exception of type ' System.InvalidCastException ' occurred system windows forms.dll

  5. #5
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    39,038

    Re: Whats the wrong in this code ?? Vb.net

    Sitten has as much of an answer as we can really provide at this time. Whatever is in

    WebBrowser1.Document.GetElementsByTagName("head")(0)

    is not something that can be implicitly converted to an htmlElement. When the exception happens, you can highlight that whole portion and press Shift+F9 to see what is in there. This should also give you the type of the item.
    My usual boring signature: Nothing

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