1

I have 2 buttons on my form and here is the two bits of code.

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



    If LoggedInToAdastra = True Then
        LogOutOfAdastra()
        Exit Sub
    End If

    strUsername = txtUsername.Text
    strPassword = txtPassword.Text
    LoggedInToAdastra = False
    ProgressBar1.Value = 10

    Try
        WebBrowser1.ScriptErrorsSuppressed = True
        WebBrowser1.Navigate("https://nww.awv.nems.nhs.uk/AWA/login.aspx?ReturnUrl=%2fAWA%2fdefault.aspx")
        WaitForPageLoad()
        WebBrowser1.Document.All("ctl00$MainContent$txtUserName").SetAttribute("value", strUsername)
        WebBrowser1.Document.All("ctl00$MainContent$txtPassWord").SetAttribute("value", strPassword)
        WebBrowser1.Document.All("ctl00$MainContent$btnLogin").InvokeMember("click")
        ProgressBar1.Value = 40
        WaitForPageLoad()
        ProgressBar1.Value = 80
        WebBrowser1.Document.All("ctl00$MainContent$ctl01").InvokeMember("click")
        WaitForPageLoad()
        ProgressBar1.Value = 100

        LoggedInToAdastra = True
        txtPassword.Enabled = False
        txtUsername.Enabled = False
        Button2.Enabled = False
        Button1.Text = "Log Out"
        Button2.BackColor = Color.MintCream
        Button1.BackColor = Color.MintCream
        txtPassword.BackColor = Color.MintCream
        txtUsername.BackColor = Color.MintCream
        ProgressBar1.Value = 0
        btnUpdate.Enabled = True
        btnDoAll.Enabled = True
        btnSearchSelected.Enabled = True
        doc = Nothing

    Catch ex As Exception
        MsgBox("Login Failed")
        Button2.BackColor = Color.MistyRose
        Button1.BackColor = Color.MistyRose
        txtPassword.BackColor = Color.MistyRose
        txtUsername.BackColor = Color.MistyRose
        txtUsername.Text = ""
        txtPassword.Text = ""
        ProgressBar1.Value = 0
    End Try



End Sub

And the second button, once logged in, you should be able to search for a patient using this button and choosing a patient from a listbox...

Private Sub btnUpdate_Click(sender As Object, e As EventArgs) Handles btnUpdate.Click

    If LoggedInToAdastra = False Then
        MsgBox("Please re-login to Adastra")
    End If

    strPatientNHS = lstPatients.SelectedItems(0).SubItems(0).Text
    EditNote(strPatientNHS)



End Sub

Public Sub EditNote(strPatientNHS As String)


    'Try

    doc = WebBrowser1.Document
    WebBrowser1.ScriptErrorsSuppressed = True
    WebBrowser1.Document.OpenNew(True)
    WebBrowser1.Navigate("https://nww.awv.nems.nhs.uk/AWA/login.aspx?ReturnUrl=%2fAWA%2fdefault.aspx")
    ProgressBar1.Value = 10
    WaitForPageLoad()
    ProgressBar1.Value = 20
    WebBrowser1.Document.All("ctl00$MainContent$txtUserName").SetAttribute("value", strUsername)
    WebBrowser1.Document.All("ctl00$MainContent$txtPassWord").SetAttribute("value", strPassword)
    WebBrowser1.Document.All("ctl00$MainContent$btnLogin").InvokeMember("click")
    'WebBrowser1.Navigate("https://nww.awv.nems.nhs.uk/AWA/MainMenu.aspx")
    WaitForPageLoad()
    ProgressBar1.Value = 30
    WebBrowser1.Document.All("ctl00$MainContent$ctl01").InvokeMember("click")
    WaitForPageLoad()
    ProgressBar1.Value = 40

   WebBrowser1.Document.GetElementById("ct100_MainContent_txtNationalNumber").SetAttribute("value", strPatientNHS)
    WebBrowser1.Document.GetElementById("ct100_MainContent_cboSurgery2").SetAttribute("value", "All Practices")
    WebBrowser1.Document.All("ct100$MainContent$bSearch").InvokeMember("click")


End Sub

It errors here on the EditNote sub

 WebBrowser1.Document.GetElementById("ct100_MainContent_txtNationalNumber").SetAttribute("value", strPatientNHS)
    WebBrowser1.Document.GetElementById("ct100_MainContent_cboSurgery2").SetAttribute("value", "All Practices")
    WebBrowser1.Document.All("ct100$MainContent$bSearch").InvokeMember("click")

I have tried all sorts, but not sure why its bringing through the Error (Object reference not set to an instance of an object.)

The control does exist on the web page, and that is the correct ID for it. I have tried using Document.All and looking for the element too but to no avail.

Filburt
  • 17,626
  • 12
  • 64
  • 115
Jay
  • 21
  • 2
  • Welcome to Stack Overflow! Nearly all NullReference Exceptions have the same set of causes. See [NullReference Exception in Visual Basic](http://stackoverflow.com/a/26761773/1070452) for help on this. – Ňɏssa Pøngjǣrdenlarp Jul 20 '15 at 13:29
  • In this case,`WebBrowser1` or `Document` might be Nothing. If the element doesnt exist, you'll get a NRE when you try to Set its attribute – Ňɏssa Pøngjǣrdenlarp Jul 20 '15 at 13:32

1 Answers1

0

I did resolve this in the end, in quite bizarre fashion...

so it was giving me an error basically because it couldn't find the control

WebBrowser1.Document.All("ctl00$MainContent$txtUserName")

so as a last resort, I copied and pasted the exact control from the page source and it worked... even though every character was the same...

This fixed my problem anyway. Don't type the control out, copy and paste it! Maybe someone with more knowledge can explain how this works? is there hidden characters in the HTML?

Jay
  • 21
  • 2