VB.NET: How to populate combo box with database item

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • nareshpulipati
    New Member
    • Jun 2007
    • 5

    VB.NET: How to populate combo box with database item

    Hi all,
    I am new to VB .net.
    Iam trying to populate the database item into combo box.
    Database Type:SQL(ODBC)
    My code retuns no value in combo box
    [CODE=vbnet]Public Class Form1
    Private Sub ComboBox1_Selec tedIndexChanged (ByVal sender As System.Object, ByVal e As System.EventArg s) Handles ComboBox1.Selec tedIndexChanged
    Dim Cmd As Odbc.OdbcComman d
    Dim Con As Odbc.OdbcConnec tion
    Dim Sql As String = Nothing
    Dim Reader As Odbc.OdbcDataRe ader
    Dim ComboRow As Integer = -1
    Dim Columns As Integer = 0
    Dim Category As String = Nothing
    Dim Cmbbox As ComboBox

    CmbboxMan.Items .Clear()

    Con = New Odbc.OdbcConnec tion("Provider= Microsoft.Jet.O dbc.4.0;data source=" & Database & "")

    Sql = "SELECT [Comp_ItemNumber] FROM [Components] ORDER BY [Comp_ItemNumber] Asc"
    Cmd = New Odbc.OdbcComman d(Sql, Con)
    Con.Open()

    Reader = Cmd.ExecuteRead er()
    While Reader.Read()
    For Columns = 0 To Reader.FieldCou nt - 1
    Category = Reader.Item(Col umns) 'READ COLUMN FROM DATABASE
    Next
    Cmbbox.Items.Ad d(Category)
    ComboRow += 1
    End While
    Con.Close()
    End Sub
    End Class[/CODE]
    Plz reply ASAP
    Last edited by Killer42; Jun 28 '07, 09:53 PM. Reason: Added [CODE=vnet] tag
  • danp129
    Recognized Expert Contributor
    • Jul 2006
    • 323

    #2
    Is category blank?
    [CODE=vb]For Columns = 0 To Reader.FieldCou nt - 1
    Category = Reader.Item(Col umns) 'READ COLUMN FROM DATABASE
    debug.print Category
    Next[/CODE]

    use [CODE=VB]code here[/CODE] tags please

    Comment

    • nareshpulipati
      New Member
      • Jun 2007
      • 5

      #3
      Originally posted by danp129
      Is category blank?
      [CODE=vb]For Columns = 0 To Reader.FieldCou nt - 1
      Category = Reader.Item(Col umns) 'READ COLUMN FROM DATABASE
      debug.print Category
      Next[/CODE]

      use [CODE=VB]code here[/CODE] tags please

      Thanks for replying

      When i debug these are the two errors i came across and also i used debug.print Category(Column s).Because its showing declaration error when i use debug.print Category

      Error 1 Name 'CmbBoxMan' is not declared. C:\Documents and Settings\naresh \Desktop\VB COMBO BOX WITH SQL DATA SOURCE\VB COMBO BOX WITH SQL DATA SOURCE\Form1.vb 13 9 VB COMBO BOX WITH SQL DATA SOURCE

      Error 2 Name 'Database' is not declared. C:\Documents and Settings\naresh \Desktop\VB COMBO BOX WITH SQL DATA SOURCE\VB COMBO BOX WITH SQL DATA SOURCE\Form1.vb 15 88 VB COMBO BOX WITH SQL DATA SOURCE


      Please help me

      Comment

      • nareshpulipati
        New Member
        • Jun 2007
        • 5

        #4
        Originally posted by nareshpulipati
        Thanks for replying

        When i debug these are the two errors i came across and also i used debug.print Category(Column s).Because its showing declaration error when i use debug.print Category

        Error 1 Name 'CmbBoxMan' is not declared. C:\Documents and Settings\naresh \Desktop\VB COMBO BOX WITH SQL DATA SOURCE\VB COMBO BOX WITH SQL DATA SOURCE\Form1.vb 13 9 VB COMBO BOX WITH SQL DATA SOURCE

        Error 2 Name 'Database' is not declared. C:\Documents and Settings\naresh \Desktop\VB COMBO BOX WITH SQL DATA SOURCE\VB COMBO BOX WITH SQL DATA SOURCE\Form1.vb 15 88 VB COMBO BOX WITH SQL DATA SOURCE


        Please help me



        IS THIS CODE CORRECT TO CONNECT TO SQL DATABASE


        Con = New Odbc.OdbcConnec tion("Provider= Microsoft.Jet.O dbc.4.0;data source=" & kpi_database & "")

        Sql = "SELECT [Comp_ItemNumber] FROM [Components] ORDER BY [Comp_ItemNumber] Asc"
        Cmd = New Odbc.OdbcComman d(Sql, Con)

        Comment

        • danp129
          Recognized Expert Contributor
          • Jul 2006
          • 323

          #5
          I haven't made a SQL app in vb.net only vb6. What DB are you connecting to?

          Comment

          • nareshpulipati
            New Member
            • Jun 2007
            • 5

            #6
            Originally posted by danp129
            I haven't made a SQL app in vb.net only vb6. What DB are you connecting to?

            IAm connecting to ODBC

            Comment

            • nareshpulipati
              New Member
              • Jun 2007
              • 5

              #7
              Originally posted by nareshpulipati
              IAm connecting to ODBC
              Oracle data base of the company

              Comment

              • danp129
                Recognized Expert Contributor
                • Jul 2006
                • 323

                #8
                One of these strings for Oracle should work.

                http://www.carlprothma n.net/Default.aspx?ta bid=90

                Comment

                Working...