Filling a combobox in 2005

G

Guest

I used the following two routines to fil a drop-down box in a VS 2003 web
project
What do I need to do to make these work with list controls in 2005 Windows
Projects?

========================================

Private Sub FillWithDepts(ByVal strStartDate As String)

Dim drDepts As SqlClient.SqlDataReader
drDepts = AppointmentsBLL.GetDeptNames(strStartDate)
Me.cmbSourceList.Items.Clear()
Me.cmbSourceList.Items.Add(New ListItem("", ""))
Do While drDepts.Read
Me.cmbSourceList.Items.Add(New ListItem(drDepts("Dept")))
Loop


End Sub



Public Shared Function GetDeptNames(ByVal strStartDate As String) As
SqlDataReader
'This function gets the Dept names from the most recent appointment
table
'with related date
'functions as a data reader
Dim drDepts As SqlDataReader
Dim conMembers As SqlConnection = GetMembershipConnection()
Dim strSQL As String

strSQL = "SELECT DISTINCT Dept " _
& "FROM tbl_Appt WHERE ApptDate =@StartDate Order by Dept "

Dim cmdGetDepts As New SqlCommand(strSQL, conMembers)
cmdGetDepts.CommandType = CommandType.Text

Dim pStartDt As New SqlParameter("@StartDate", SqlDbType.VarChar)
With cmdGetDepts
pStartDt.Direction = ParameterDirection.Input
pStartDt.Value = strStartDate
.Parameters.Add(pStartDt)
End With



conMembers.Open()
drDepts = cmdGetDepts.ExecuteReader(CommandBehavior.CloseConnection)
Return drDepts

End Function
 
O

Otis Mukinfus

I used the following two routines to fil a drop-down box in a VS 2003 web
project
What do I need to do to make these work with list controls in 2005 Windows
Projects?

========================================

Private Sub FillWithDepts(ByVal strStartDate As String)

Dim drDepts As SqlClient.SqlDataReader
drDepts = AppointmentsBLL.GetDeptNames(strStartDate)
Me.cmbSourceList.Items.Clear()
Me.cmbSourceList.Items.Add(New ListItem("", ""))
Do While drDepts.Read
Me.cmbSourceList.Items.Add(New ListItem(drDepts("Dept")))
Loop


End Sub



Public Shared Function GetDeptNames(ByVal strStartDate As String) As
SqlDataReader
'This function gets the Dept names from the most recent appointment
table
'with related date
'functions as a data reader
Dim drDepts As SqlDataReader
Dim conMembers As SqlConnection = GetMembershipConnection()
Dim strSQL As String

strSQL = "SELECT DISTINCT Dept " _
& "FROM tbl_Appt WHERE ApptDate =@StartDate Order by Dept "

Dim cmdGetDepts As New SqlCommand(strSQL, conMembers)
cmdGetDepts.CommandType = CommandType.Text

Dim pStartDt As New SqlParameter("@StartDate", SqlDbType.VarChar)
With cmdGetDepts
pStartDt.Direction = ParameterDirection.Input
pStartDt.Value = strStartDate
.Parameters.Add(pStartDt)
End With



conMembers.Open()
drDepts = cmdGetDepts.ExecuteReader(CommandBehavior.CloseConnection)
Return drDepts

End Function

Instead of returning a DataReader from your function, return a List or DataSet
then bind it to the combobox using the comboboxes dataSource property.

search for ComboBox.DataSource in the help files. Simple programming practice;
first look in the help files and search Google.
Good luck with your project,

Otis Mukinfus
http://www.arltex.com
http://www.tomchilders.com
 
G

Guest

That's why I wrote in this newsgroup, I tried what you said first - and it
didn't work.
Almost every demo I find in google shows how to use the graphical tools to
bind to the new combobox, but I want to be able to several different
resultsets to the combobox based on different criteria, so the graphical tool
doesn't help.

I prefer not to use a tableAdapter, but if I can,

I know it isn't as simple as

MyComboBox.DataSource = dtMyDataTable

or is it?
 
K

Klaas

Yes, it is that simple.
Also provide the MyComboBox.datamemeber and value and you are good to go.
 
Top