Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: VB.NET
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.
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 28-Mar-13 11:22am
MRM256498
Comments
Sergey Alexandrovich Kryukov at 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 at 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

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

I have found my own answer.
'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.
  Permalink  

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

  Print Answers RSS
0 Afzaal Ahmad Zeeshan 259
1 Maciej Los 240
2 BillWoodruff 240
3 OriginalGriff 220
4 Sergey Alexandrovich Kryukov 195
0 OriginalGriff 6,419
1 Sergey Alexandrovich Kryukov 6,008
2 DamithSL 5,183
3 Manas Bhardwaj 4,673
4 Maciej Los 4,070


Advertise | Privacy | Mobile
Web01 | 2.8.1411019.1 | Last Updated 12 Apr 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100