Link to home
Start Free TrialLog in
Avatar of searchsanjaysharma
searchsanjaysharma

asked on

"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
ASKER CERTIFIED SOLUTION
Avatar of appari
appari
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of searchsanjaysharma
searchsanjaysharma

ASKER

But i want to reset cmbdistrict, if cmbstates has been changed.