Hello.. I want to populate my combobox with the list of banknames available in my database.. I tried doing so but i am getting a blank list.. ie.. shows no value in combobox !! Here is what i tried.. Please help !!

Private Sub ComboBox4_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox4.SelectedIndexChanged

        Try
            'MsgBox("Open")
            cn = New OleDbConnection("Provider=microsoft.jet.oledb.4.0;Data Source=E:\Project-Hemtech\HemDatabase1.mdb;")
            cn.Open()
            cmd = New OleDbCommand("select * from contactinfo", cn)
            dr = cmd.ExecuteReader()

            While dr.Read()

                If Not ComboBox4.Items.Contains(dr("bankname")) Then
                    ComboBox4.Items.Add(dr("bankname"))
                End If

            End While

            MsgBox("Your Account Created Successfully ")
            'MsgBox("Done !!")

        Catch myException As Exception

            MsgBox("No Record Inserted" + myException.ToString())

        Finally
            'MsgBox("Closing Connection")
            dr.Close()
            cn.Close()

        End Try


    End Sub

Recommended Answers

All 11 Replies

Try using dataset and set display member and value member to combo. in Select statement itself get the distinct bank names.

First timer.. Could you please explain a bit in detail ?? Or try illustrating via code snippet or something as that ??

Thank You !! :)

Is this what you meant ?? Just tried to implement what i could follow..
However, the output is still blank ..

Private Sub ComboBox4_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox4.SelectedIndexChanged

        Try
            'MsgBox("Open")
            cn = New OleDbConnection("Provider=microsoft.jet.oledb.4.0;Data Source=E:\Project-Hemtech\HemDatabase1.mdb;")
            cn.Open()
            cmd = New OleDbCommand("select * from contactinfo", cn)
            da = New OleDbDataAdapter(cmd)

            dt = New DataTable
            da.Fill(dt)

            ComboBox4.DataSource = dt
            ComboBox4.DisplayMember = "bankname"
            ComboBox4.ValueMember = "bankname"

            MsgBox("Your Account Created Successfully ")
            'MsgBox("Done !!")

        Catch myException As Exception

            MsgBox("No Record Inserted" + myException.ToString())

        Finally
            'MsgBox("Closing Connection")
            da.Dispose()
            da = Nothing
            dr.Close()
            cn.Close()

        End Try

Yes this is the way. Is ds having data?
Can you check combobox4.items.count after MsgBox("Your Account Created Successfully ")??

I am not being able to debug.. may be its because i placed the code in SelectedIndexChanged property.. Am i making a mistake in this ?? If so, where should i place the code ?? I want the items to be listed when the user clicks on dropdown menu ..

Why binding in Selectedindexchanged event? Any reason? What exactly is the need? Cant u try in form load?

I tried in formload.. No effect..

Basically i want that went the user clicks on the drop down menu, he should be able to view that many list of banks, which are present in my database !!

Write the same code in form load event and check combobox4.items.count after MsgBox("Your Account Created Successfully "). hope u will be abale to debug in form load :)

hey.. i made some chganges.. i checked in he form load.. dint happen.. now added a button and trying on button click.. the rows on beeing counted give me proper output value in the msgbox !! But it leaves my drop down menu blank..
This is the modified code..

System.Object, ByVal e As System.EventArgs) Handles Button5.Click


        Try
            'MsgBox("Open")
            cn = New OleDbConnection("Provider=microsoft.jet.oledb.4.0;Data Source=E:\Project-Hemtech\HemDatabase1.mdb;")
            cn.Open()
            cmd = New OleDbCommand("select * from contactinfo", cn)
            da = New OleDbDataAdapter(cmd)

            dt = New DataTable
            da.Fill(dt)

            ComboBox4.DataSource = dt
            ComboBox4.DisplayMember = "bankname"
            ComboBox4.ValueMember = "bankname"


            ''dr = cmd.ExecuteReader()

            ''While dr.Read()

            ''    If Not ComboBox4.Items.Contains(dr("bankname")) Then
            ''        ComboBox4.Items.Add(dr("bankname"))
            ''    End If

            ''End While

            MsgBox("Your Account Created Successfully ")

            MsgBox(ComboBox4.Items.Count.ToString)
            'MsgBox("Done !!")

        Catch myException As Exception

            MsgBox("No Record Inserted" + myException.ToString())

        Finally
            'MsgBox("Closing Connection")
            da.Dispose()
            da = Nothing
            'dr.Close()
            cn.Close()

        End Try

    End Sub

Then somewhere else in ur code ComboBox4.datasource=nothing myt be setting. u need to check that.

Nope.. There is nothing like that in my entire code !! :(

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.