"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
searchsanjaysharmaAsked:
Who is Participating?
 
appariConnect With a Mentor Commented:
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
All Courses

From novice to tech pro — start learning today.