Click here to Skip to main content
15,881,381 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
I'm trying to populate a toolbar combobox with values from two fields in a database table. The table contains the following data:

DBID DB_Type
1 Oracle Database
2 Microsoft SQL Server
3 ODBC
4 Intrabase/Firebird
5 OleDB(ADO)
6 SQLite

I'm trying to populate the combobox from the Form_Load event.
VB
Private Sub Form1_Load(ByVal sender As System.Object, _
                           ByVal e As System.EventArgs) _
                           Handles MyBase.Load
        Dim cn As New OleDbConnection
        Dim cm As OleDbCommand
        Dim da As New OleDbDataAdapter
        Dim strCnn As String
        Dim ds As New DataSet
        Dim strSQL As String
        strSQL = "SELECT * FROM tblDbTypes;"
        'Function to build Connection string
        strCnn = CnnCtrlDB(strAppPath & "\CtrlDB.ACCDB")
        cn = New OleDbConnection(strCnn)
        Try
            'Function to check db connection status
            Dim bCnn As Boolean = DBConnectionStatus(strCnn)
            If bCnn Then
                cn.Open()
                cm = New OleDbCommand(strSQL, cn)
                da.SelectCommand = cm
                da.Fill(ds)
                da.Dispose()
                cm.Dispose()
                cn.Close()
            End If
            'This SHOULD set the combobox index to 
            'number in the DBID field in the DB - IT DON'T
            tsCboDBTypes.ComboBox.ValueMember = "DBID"
            'This sets the combobox's DisplayMember to the 
            'value contained in the Db_Type field
            tsCboDBTypes.ComboBox.DisplayMember = "Db_Type"
            'Binds the combobox to the DataSet
            tsCboDBTypes.ComboBox.DataSource = ds.Tables(0)
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try
    End Sub


When I open the form the routine works to the point where I can see all six items in the combobox. The problem I'm getting now is when I click on the Oracle item; the DBID being returned is zero(0), clicking on Microsoft SQL Server will result with a one(1), and so on.

I know all collections are zero-based. But what I want to know is why doesn't .NET assign the index to the DBID? Isn't the .NET way of doing things suppose to be better? What is the use of building a DataSet, but have to populate the Combobox using ADO techniques used in VB6? I have never seen a database table where a zero can be used as a identification number in a Primary Key field.

Thanks in advance,
MRM256
Posted
Comments
Sergey Alexandrovich Kryukov 28-Mar-13 16:33pm    
Please tag the UI library you use or application type. If could be ASP.NET or System.Windows.Forms...
—SA
Sergey Alexandrovich Kryukov 28-Mar-13 16:34pm    
Don't catch and handle exception locally. Let go. It should be handled way up the stack...
—SA

1 solution

I have found my own answer.
VB
'This sets the combobox.ValueMember to the
'number in the DBID column(field) of the dataset.
'I believe the ValueMember Method replaces the
'NewIndex method in VB6.
ComboBox.ValueMember = "DBID"

When I selected a database type from the combobox and looked at the ValueMember property I could then see the DBID number I needed.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900