Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

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=visualbasic]
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

[/code]

 

Thanks In Advance

 

-=Simcoder=-

Whatever thy hand findest to do, do it with all thy heart - Jesus
Posted (edited)

[code=visualbasic]
Private Sub PopulateBoxes()

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

           cmd.Connection = SQLConn

           'If you're 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 - don't 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()   'can't 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

[/code]

 

Thanks In Advance

 

-=Simcoder=-

Edited by bri189a
Posted

Thanks for the reply. I'll give it a try as soon as I get to work Monday Morning.

 

 

-=Simcoder=-

Whatever thy hand findest to do, do it with all thy heart - Jesus
Posted

I'm Having Some Difficulties Getting This Thing To Work. This Is The

Statement I'm Having Problems With.

 

cboClass.Items.Add(New ListItem(SqlReader.Item(0), SqlReader.Item(1)))

 

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

Whatever thy hand findest to do, do it with all thy heart - Jesus

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...