Tracy,
Below you will find some code to populate a combobox. I choose to use a Dataset instead of using Parameters.
--------------Code Below-------------------
----Declarations------------
'Connection String
Public connectionstring As String = "Data Source=FA2-0282\SQLEXPRESS;Initial Catalog=ComboBox;Integrated Security=True;Pooling=False"
----FormLoad----------------
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
populateStaffComboBox()
End Sub
----Seperate Subroutine-----
Public Sub populateStaffComboBox()
'Declared new SqlConnection and set it too
'public connection string which
'I declared above
Dim conn As New SqlConnection(connectionstring)
'Open connection
conn.Open()
'Connection String
Dim sql As String = "select staff_first_name from staff"
'Declare DataAdapter
Dim StaffAdapter As New SqlDataAdapter
'Declare SQL command and set it too sql string
'(sql) and connection (conn)
Dim cmd As New SqlCommand(sql, conn)
'Delcare Dataset and DataView --- notice
'i declared both of them as New.
Dim ds As New DataSet
Dim dv As New DataView
'Here I am setting my SqlCommand
'-- cmd -- Command type to a text value
'So that it will be accepted by the Adapter
cmd.CommandType = CommandType.Text
'Here I am declaring the select command for my Adapter
StaffAdapter.SelectCommand = cmd
'Try Catch Block for error checking
Try
'I fill my dataset using the Fill method
StaffAdapter.Fill(ds)
'Setting my Dataview equal to my dataset
'basically the default table
dv = ds.Tables(0).DefaultView
'I am populating my combo box here
With Me.cboTest
'I set the datasource of the combobox
'to my dataview
.DataSource = dv
'Here i tell the combobox what to display
.DisplayMember = "Staff_First_Name"
End With
Catch ex As Exception
'In case I have an errors --- ;)
MessageBox.Show(ex.Message.ToString)
End Try
'Close my connection
conn.Close()
End Sub
-----------End Code----------------
Everyone have a great day...
notrosh
www.nwadnug.org