4 Replies - 428 Views - Last Post: 07 November 2011 - 03:24 AM Rate Topic: -----

#1 Ruchi22  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 118
  • Joined: 23-September 11

combobox and database

Posted 07 November 2011 - 12:23 AM

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

This post has been edited by Ruchi22: 07 November 2011 - 12:27 AM


Is This A Good Question/Topic? 0
  • +

Replies To: combobox and database

#2 smohd  Icon User is offline

  • Critical Section
  • member icon


Reputation: 1819
  • View blog
  • Posts: 4,627
  • Joined: 14-March 10

Re: combobox and database

Posted 07 November 2011 - 03:00 AM

First of all I dont get the logic why that code should be in SelectedIndexChanged event of the same combobox, I was expecting on form load or another combobox event.


If you are loading the complete column, then why not just bind it? You can use the dataset and get the table from it, then use data source and display member properties to set your data to combobox.
Just after filling the dataset, you can say:
ComboBox4.DataSource = ds.Tables(0) 'or  the name of table in i
ComboBox4.DisplayMember = ds.Tables(0).Columns("bankname").ToString
'And if you need you can specify the ValueMember

Was This Post Helpful? 0
  • +
  • -

#3 Ruchi22  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 118
  • Joined: 23-September 11

Re: combobox and database

Posted 07 November 2011 - 03:10 AM

Yes.. After posting i thought i was wrong.. so added a button and posted the code in button click, after some modifications..

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("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

Was This Post Helpful? 0
  • +
  • -

#4 smohd  Icon User is offline

  • Critical Section
  • member icon


Reputation: 1819
  • View blog
  • Posts: 4,627
  • Joined: 14-March 10

Re: combobox and database

Posted 07 November 2011 - 03:18 AM

So it is working now?
Was This Post Helpful? 0
  • +
  • -

#5 Ruchi22  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 118
  • Joined: 23-September 11

Re: combobox and database

Posted 07 November 2011 - 03:24 AM

nope.. gives me blank output.. I tested the statement in access and also tried to debug the code..

No error.. But gives me blank output.. Here is screenshot of the output..

Attached image(s)

  • Attached Image

This post has been edited by Ruchi22: 07 November 2011 - 03:26 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1