Valuemember For ComboBox

Simcoder

Well-known member
Joined
Apr 18, 2003
Messages
124
Location
Texas
Ok, Heres The Situation. I Have A Combo Box That Gets Populated With(String) Information From A Sql Query. However, The Information That Will Be Saved Back Into The Database Will Be In An Integer Form, Which Is The ID Number. How Do I Use Valuemember To Correspond Each Item In The Combobox With The Field ID. Heres Some Code, If Someone Could Show Me Where And How To Add In That Information, It Would Be Much Appreciated.
Code:
[VB]
 Private Sub PopulateBoxes()

        Try
            Dim strSelect As String
            Dim Command As New SqlCommand
            Dim SqlReader As SqlDataReader
          
            DefineControls()

            Command.Connection = SQLConn

            strSelect = "SELECT [fldClassName] FROM tblClasses"

            Command.CommandText = strSelect

             This Is Where I Tried To Use Valuemember
             cboClass.DataSource = SqlReader
             cboClass.ValueMember = "fldClassName"


            SqlReader = Command.ExecuteReader()

           
            Do While SqlReader.Read()

                If Not cboClass.Items.Contains(SqlReader.Item(0)) Then
                    If Not IsDBNull(SqlReader.Item(0)) Then
                        cboClass.Items.Add(SqlReader.Item(0))
                    End If
                End If
            Loop
            SqlReader.Close()
        Catch
            MessageBox.Show(Err.Description)
        End Try

    End Sub


   And When The Index Changes On The Combo Box, Change The Text
   To Say What Valuemember Is Selected
    Private Sub cboClass_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cboClass.SelectedIndexChanged
        Me.Text = cboClass.SelectedValue()
    End Sub
[/VB]

Thanks In Advance

-=Simcoder=-
 
Code:
[VB]
 Private Sub PopulateBoxes()

        Try
            Dim strSelect As String
            Dim cmd As New SqlCommand
            Dim rdr As SqlDataReader
          
            DefineControls()

            cmd.Connection = SQLConn

            If youre worried about duplicate records use the distinct keyword
            or use a constraint on the database.
            strSelect = "SELECT DISTINCT id, fldClassName FROM tblClasses"

            cmd.CommandText = strSelect

            Try
               rdr = Command.ExecuteReader()
               Do While rdr.Read()
                   I assume cboClass is a list box object
                      IsDBNull() is VB6 legacy function - dont use.
                      I assume id is an numeric type and fldClassName is a string
                      or character type and that either field has the possibility of
                      being null.
                      If Not rdr.Item(0) = DBNull.Value AndAlso Not rdr.Item(1) Is DBNull.Value Then
                           cboClass.Items.Add(new ListItem(rdr.Item(0), rdr.Item(1)))
                     End If
               Loop
            Catch ex As Exception
                Do whatever you need to
            Finally
                SqlReader.Close()
                SqlReader.Dispose()   cant remember if reader has dispose, think it does.
            End Try
        Catch
            MessageBox.Show(Err.Description)
        End Try

    End Sub


   And When The Index Changes On The Combo Box, Change The Text
   To Say What Valuemember Is Selected
    Private Sub cboClass_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cboClass.SelectedIndexChanged
        Me.Text = cboClass.SelectedValue()
    End Sub
[/VB]

Thanks In Advance

-=Simcoder=-[/QUOTE]
 
Last edited by a moderator:
Thanks for the reply. Ill give it a try as soon as I get to work Monday Morning.


-=Simcoder=-
 
Im Having Some Difficulties Getting This Thing To Work. This Is The
Statement Im Having Problems With.

[VB]
cboClass.Items.Add(New ListItem(SqlReader.Item(0), SqlReader.Item(1)))
[/VB]

I Changed The ListItem To ListViewItem Because ListItem Was A Type That Was Not Found. And Here Is The Problem I Ran Into
 
Back
Top