Solved

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

Posted on 2012-03-21
3
188 Views
Last Modified: 2012-03-21
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
Comment
Question by:searchsanjaysharma
  • 2
3 Comments
 
LVL 39

Accepted Solution

by:
appari earned 500 total points
Comment Utility
why are you setting selectedindex to -1 before the check?

remove this line and check
cmbstate.SelectedIndex = -1
0
 

Author Comment

by:searchsanjaysharma
Comment Utility
But i want to reset cmbdistrict, if cmbstates has been changed.
0
 

Author Closing Comment

by:searchsanjaysharma
Comment Utility
ok
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

A while ago, I was working on a Windows Forms application and I needed a special label control with reflection (glass) effect to show some titles in a stylish way. I've always enjoyed working with graphics, but it's never too clever to re-invent …
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now