472,110 Members | 2,143 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,110 software developers and data experts.

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
Expand|Select|Wrap|Line Numbers
  1. Public Class Form1
  2.     Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged
  3.         Dim Cmd As Odbc.OdbcCommand
  4.         Dim Con As Odbc.OdbcConnection
  5.         Dim Sql As String = Nothing
  6.         Dim Reader As Odbc.OdbcDataReader
  7.         Dim ComboRow As Integer = -1
  8.         Dim Columns As Integer = 0
  9.         Dim Category As String = Nothing
  10.         Dim Cmbbox As ComboBox
  11.  
  12.         CmbboxMan.Items.Clear()
  13.  
  14.         Con = New Odbc.OdbcConnection("Provider=Microsoft.Jet.Odbc.4.0;data source=" & Database & "")
  15.  
  16.         Sql = "SELECT [Comp_ItemNumber] FROM [Components] ORDER BY [Comp_ItemNumber] Asc"
  17.         Cmd = New Odbc.OdbcCommand(Sql, Con)
  18.         Con.Open()
  19.  
  20.         Reader = Cmd.ExecuteReader()
  21.         While Reader.Read()
  22.             For Columns = 0 To Reader.FieldCount - 1
  23.                 Category = Reader.Item(Columns) 'READ COLUMN FROM DATABASE
  24.             Next
  25.             Cmbbox.Items.Add(Category)
  26.             ComboRow += 1
  27.         End While
  28.         Con.Close()
  29.     End Sub
  30. End Class
Plz reply ASAP
Jun 28 '07 #1
7 22446
danp129
321 Expert 100+
Is category blank?
Expand|Select|Wrap|Line Numbers
  1. For Columns = 0 To Reader.FieldCount - 1
  2.     Category = Reader.Item(Columns) 'READ COLUMN FROM DATABASE
  3.     debug.print Category
  4. Next
use [CODE=VB]code here[/CODE] tags please
Jun 28 '07 #2
Is category blank?
Expand|Select|Wrap|Line Numbers
  1. For Columns = 0 To Reader.FieldCount - 1
  2.     Category = Reader.Item(Columns) 'READ COLUMN FROM DATABASE
  3.     debug.print Category
  4. Next
use
Expand|Select|Wrap|Line Numbers
  1. code here
tags please

Thanks for replying

When i debug these are the two errors i came across and also i used debug.print Category(Columns).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
Jun 29 '07 #3
Thanks for replying

When i debug these are the two errors i came across and also i used debug.print Category(Columns).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.OdbcConnection("Provider=Microsoft.Jet.Odbc.4 .0;data source=" & kpi_database & "")

Sql = "SELECT [Comp_ItemNumber] FROM [Components] ORDER BY [Comp_ItemNumber] Asc"
Cmd = New Odbc.OdbcCommand(Sql, Con)
Jun 29 '07 #4
danp129
321 Expert 100+
I haven't made a SQL app in vb.net only vb6. What DB are you connecting to?
Jun 29 '07 #5
I haven't made a SQL app in vb.net only vb6. What DB are you connecting to?

IAm connecting to ODBC
Jun 29 '07 #6
IAm connecting to ODBC
Oracle data base of the company
Jun 29 '07 #7
danp129
321 Expert 100+
One of these strings for Oracle should work.

http://www.carlprothman.net/Default.aspx?tabid=90
Jun 29 '07 #8

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

1 post views Thread by Lance | last post: by
4 posts views Thread by Mike L | last post: by
4 posts views Thread by Ziv Riezman | last post: by
reply views Thread by shamirza | last post: by
reply views Thread by leo001 | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.