0

Combobox A will load the DISTINCT Street name from Microsoft Access, when user selects the Street name, Combobox B will load the House Number of the selected Street name. Below are my codes:

Private Sub tsmiAddress_Click(sender As Object, e As EventArgs) Handles tsmiAddress.Click
        lblSearch.Text = "Search (Address)"

        If oledbCon.State = ConnectionState.Closed Then
            oledbCon.Open()
        End If

        Dim oledbCmd As New OleDbCommand("SELECT DISTINCT Jalan FROM House", oledbCon)
        Dim oledbCmd2 As New OleDbCommand("SELECT House_No FROM House WHERE Jalan = '" & cmbJalan.SelectedValue.ToString & "'", oledbCon)
        Dim oledbDr As OleDbDataReader = oledbCmd.ExecuteReader()
        Dim oledbDr2 As OleDbDataReader = oledbCmd2.ExecuteReader()

        If oledbDr.HasRows Then
            While oledbDr.Read()
                cmbJalan.Items.Add(oledbDr.Item(0))
            End While

            While oledbDr2.Read()
                cmbNo.Items.Add(oledbDr2.Item(0))
            End While

            cmbJalan.SelectedIndex = 0
            cmbNo.SelectedIndex = 0
            oledbDr.Close()

        End If


        oledbCon.Close()
    End Sub

But I get an error:

An unhandled exception of type 'System.NullReferenceException' occurred in ThePeak.exe

Additional information: Object reference not set to an instance of an object.

Visual Vincent
  • 17,424
  • 5
  • 24
  • 66
Lim Min Yi
  • 113
  • 10
  • Run it through the debugger and see on which line you're getting the null ref error. Possibly nothing selected in cmbJalan.SelectedValue ? – Andrew Mortimer Mar 04 '17 at 12:34
  • @AndrewMortimer This line. oledbCmd2 As New OleDbCommand("SELECT House_No FROM House WHERE Jalan = '" & cmbJalan.SelectedValue.ToString & "'", oledbCon) – Lim Min Yi Mar 04 '17 at 12:44
  • @AndrewMortimer When I click on the tsmiAddress, and the error pops out already, and I can't even select what's on my Combobox A. – Lim Min Yi Mar 04 '17 at 12:46
  • I think maybe I'll add a IF statement to only load the Combobox B when item is selected from Combobox A. – Lim Min Yi Mar 04 '17 at 12:48
  • [**Please don't put tags in the title**](http://meta.stackexchange.com/questions/19190/should-questions-include-tags-in-their-titles). – Visual Vincent Mar 04 '17 at 12:50
  • Yes definitely validate cmbJalan before trying to load other combos. What kind of control is tsmiAddress ? – Andrew Mortimer Mar 04 '17 at 12:56
  • @AndrewMortimer alright, thanks! ToolStripMenuItem – Lim Min Yi Mar 04 '17 at 12:58

0 Answers0