By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
425,478 Members | 1,833 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 425,478 IT Pros & Developers. It's quick & easy.

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

P: 5
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
Share this Question
Share on Google+
7 Replies


danp129
Expert 100+
P: 321
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

P: 5
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

P: 5
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
Expert 100+
P: 321
I haven't made a SQL app in vb.net only vb6. What DB are you connecting to?
Jun 29 '07 #5

P: 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

P: 5
IAm connecting to ODBC
Oracle data base of the company
Jun 29 '07 #7

danp129
Expert 100+
P: 321
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.