Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 206
  • Last Modified:

"Object reference not set to an instance of an object." How to correct this during combobox binding.

The error comes at bold line. I am binding state combo with district combo, i.e when any state is selected then only district should get selected.


 Private Sub cmbstate_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmbstate.SelectedIndexChanged
        cmbstate.SelectedIndex = -1
       If CType(cmbstate.SelectedItem, DataRowView)("state").ToString() <> "--Select State--" Then
            binddistricts()
        End If
    End Sub
    Public Function binddistricts()
        s = "select distinct district from statedistrict where flag=1 and state='" & cmbstate.SelectedItem & "' order by district"
        da = New SqlDataAdapter(s, con)
        dt = New DataTable()
        da.Fill(dt)

        Dim newCustomersRow As DataRow = dt.NewRow()

        newCustomersRow("district") = "--Select District--"
        dt.Rows.InsertAt(newCustomersRow, 0)

        With cmbdistrict
            .DisplayMember = "district"
            .ValueMember = "district"
            .DataSource = dt
        End With
        cmbdistrict.Refresh()
    End Function
0
searchsanjaysharma
Asked:
searchsanjaysharma
  • 2
1 Solution
 
appariCommented:
why are you setting selectedindex to -1 before the check?

remove this line and check
cmbstate.SelectedIndex = -1
0
 
searchsanjaysharmaAuthor Commented:
But i want to reset cmbdistrict, if cmbstates has been changed.
0
 
searchsanjaysharmaAuthor Commented:
ok
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now