Results 1 to 8 of 8

Thread: Downloading Data from an Internal

  1. #1

    Thread Starter
    New Member
    Join Date
    Aug 2011
    Posts
    4

    Downloading Data from an Internal

    Hello, this is my first post here and I am fairly new at VB. Using VB Express2010.

    I am trying to design a form that automates a report from my company's internal reporting website. I basically want the form to take the users login info and run a preset report. I manage to create a form with a WebBrowser control with the required website as the default URL. I also successfully put in text boxes and a button to accept the users username and password and log in to the site. The reports are a list of hyperlinks in a table on the left side of the website, once the hyperlink for a particular report is clicked it brings up a form in the middle of the website with some text boxes for the date range of the report. I have coded up to the point where the fields come up to enter the dates. See code below:

    Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click

    // take user login info from form

    WebBrowser1.Document.GetElementById("txtUserName").SetAttribute("value", TextBox1.Text)
    WebBrowser1.Document.GetElementById("txtPassword").SetAttribute("value", TextBox2.Text)

    WebBrowser1.Document.GetElementById("cmdSubmit").InvokeMember("click")

    //Click the reqired report's hyperlink

    WebBrowser1.Navigate("http://reports/LoadReport.aspx?r=0002")

    //Enter report date

    WebBrowser1.Document.GetElementById("txtDate").SetAttribute("value", "20110801")

    When I compile the code does the login and brings up the date entry fields for the report. When I added the last line of code above to enter the required value to the date field on the site it gives an error message (NullReference Exception was Unhandled) on that line. I seached the forums and google but did not find anything and I'm pretty sure I'm using the correct field ID from the HTML source code. Any assistance will be appreciated.

    DR.NET

  2. #2
    Master Of Orion ForumAccount's Avatar
    Join Date
    Jan 2009
    Location
    Canada
    Posts
    2,802

    Re: Downloading Data from an Internal

    Welcome to the forums.

    You have to wait for the WebBrowser to finish navigating after you call the Navigate method. To do this, you should handle the DocumentCompleted event of the WebBrowser. This means your coding structure will have to change a bit. For future reference, please use code tags to post any related code. This preserves the white space so that we can read it better.

  3. #3

    Thread Starter
    New Member
    Join Date
    Aug 2011
    Posts
    4

    Re: Downloading Data from an Internal

    I am doing some Research on the DocumentCompleted event but I'm not sure how its used. I will have multiple navigations within the code so I need to figure this out. I will keep searching but if anyone has an example of how to use it I would be grateful. This is my first attempt at web automation so please bear with me.

    DR.NET

  4. #4
    Master Of Orion ForumAccount's Avatar
    Join Date
    Jan 2009
    Location
    Canada
    Posts
    2,802

    Re: Downloading Data from an Internal

    Did you read the link I gave you? When you call Navigate, it asynchronously will navigate the WebBrowser to the specified URL. You cannot call code on the Document immediately after you call Navigate because the Document won't have been loaded yet. You need to wait until it's finished building the Document. The way you will know it's done is when it raises the DocumentCompleted event.

  5. #5

    Thread Starter
    New Member
    Join Date
    Aug 2011
    Posts
    4

    Re: Downloading Data from an Internal

    Hi, yes i did read the post and created a Document Completed event handler following the example and I managed to now go beyond my first error and got the date values I want in the fields however when the "submit" button is clicked it triggers another Document Completed event and uses the same handler which is now causing another error. I am playing around with this but I dont think I grasp how to use the handler properly.

    DR.NET

  6. #6
    Master Of Orion ForumAccount's Avatar
    Join Date
    Jan 2009
    Location
    Canada
    Posts
    2,802

    Re: Downloading Data from an Internal

    Every time you call the Navigate method, it will raise that event (as per documentation). That means you are going to have to keep track of what code should be run in the event if you are going to make subsequent calls to Navigate. I understand that you are new, however, .NET is an event-driven language. As such, I would suggest gaining a deeper understand of that concept. Otherwise you will end up with code that works, but you won't understand why.

  7. #7

    Thread Starter
    New Member
    Join Date
    Aug 2011
    Posts
    4

    Re: Downloading Data from an Internal

    Hi thanks for your assistance. One further question, after I added the Document Completed handler as per the example I did a step through the code and realized that after the Webbrowser1.Navigate command was processed it did not go to the handler and continued to go through the remaining lines of code. Only when all the code was executed then it jumped to the Handler which is why I'm having trouble. I was under the impression that once the Navigate was processed it would jump to the Handler then once the handler code was executed it resumes the original code. Apperently this is not the case but I will look at some .NET documentation as you suggested and keep looking for more document completed examples.

  8. #8
    Master Of Orion ForumAccount's Avatar
    Join Date
    Jan 2009
    Location
    Canada
    Posts
    2,802

    Re: Downloading Data from an Internal

    Quote Originally Posted by DR.NET View Post
    Hi thanks for your assistance. One further question, after I added the Document Completed handler as per the example I did a step through the code and realized that after the Webbrowser1.Navigate command was processed it did not go to the handler and continued to go through the remaining lines of code. Only when all the code was executed then it jumped to the Handler which is why I'm having trouble. I was under the impression that once the Navigate was processed it would jump to the Handler then once the handler code was executed it resumes the original code. Apperently this is not the case but I will look at some .NET documentation as you suggested and keep looking for more document completed examples.
    The Navigate method brings in another concept to the table: Threading. As I stated in my previous post (#4):
    When you call Navigate, it asynchronously will navigate the WebBrowser to the specified URL.
    This is why your issue was occurring in the first place. The Document wasn't loaded yet because it was being loaded on a different thread. While it was trying to load in a separate thread, you were attempting to access it on the main UI thread, and "it" was 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