Hi Jake, I'm new to this to, but I spent a lot of time figuring this
out (with help from group postings, etc.)
Here are two subs, both use datareader (which IS better than dataset)
if you just want to populate lists. The first one, simply populates a
list from a db table (please note the Not IsPostBack statement,
otherwise you will loose the user selection). The second one uses the
first box as a filter to populate the second listbox. I used
OleDbConnection , but you obviously can easily make this
SQLDbConnection , etc.
Hope these examples help you!
Also, I included a third example to fill just a textbox with a
datareader field from a db. You must use the Read method to select the
first record and test for false().
Kathy
******POPULATE DROPDOWN DROM DB WITH DATAREADER***** **
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArg s) Handles MyBase.Load
'Put user code to initialize the page here
If Not IsPostBack Then
Dim Conn1 As OleDbConnection
Dim Rdr1 As OleDbDataReader
Dim Cmd1 As OleDbCommand
Dim strSQL As String
Conn1 = New OleDbConnection (strConn)
strSQL = "SELECT DISTINCT Station FROM tblStationUsers ORDER BY
Station"
Cmd1 = New OleDbCommand(st rSQL, Conn1)
Conn1.Open()
Rdr1 = Cmd1.ExecuteRea der()
cboStation.Data Source = Rdr1
cboStation.Data Bind()
cboStation.Item s.Insert(0, "Select Station")
cboStation.Sele ctedIndex = 0
Rdr1.Close()
Conn1.Close()
End If
End Sub
*****POPULATE DROPDOWN FROM DB WITH DATAREADER USING FIRST LIST
SELECTION AS QUERY PARAMETER******
Private Sub cboStation_Sele ctedIndexChange d(ByVal sender As
System.Object, ByVal e As System.EventArg s) Handles
cboStation.Sele ctedIndexChange d
Dim Conn2 As New OleDbConnection ()
Dim Rdr2 As OleDbDataReader
Dim strSQL2 As String = "SELECT DISTINCT UserName FROM tblStationUsers
WHERE
([Station] = @Station) ORDER BY UserName"
Dim Cmd2 As New OleDbCommand(st rSQL2, Conn2)
Conn2 = New OleDbConnection (strConn)
Dim prmStation As OleDbParameter = New OleDbParameter( "@Station",
OleDbType.VarCh ar, 50)
prmStation.Valu e = cboStation.Sele ctedItem.Value
Cmd2.Parameters .Add(prmStation )
Cmd2.Connection = Conn2
Conn2.Open()
Rdr2 = Cmd2.ExecuteRea der()
cboUser.DataSou rce = Rdr2
cboUser.DataBin d()
cboUser.Items.I nsert(0, "Select User")
cboUser.Selecte dIndex = 0
Rdr2.Close()
Conn2.Close()
End Sub
*****FILL TEXTBOX WITH DATAREADER RESULT******
Private Sub cboUser_Selecte dIndexChanged(B yVal sender As
System.Object, ByVal e As System.EventArg s) Handles
cboUser.Selecte dIndexChanged
'Get password from db to use in validation of txtpass field
Dim Conn3 As New OleDbConnection ()
Dim Rdr3 As OleDbDataReader
Dim strSQL3 As String = "SELECT Password FROM tblUsers WHERE
([UserName] = @UserName)"
Dim Cmd3 As New OleDbCommand(st rSQL3, Conn3)
Conn3 = New OleDbConnection (strConn)
Dim prmUserName As OleDbParameter = New OleDbParameter( "@UserName" ,
OleDbType.VarCh ar, 50)
prmUserName.Val ue = cboUser.Selecte dItem.Value
Cmd3.Parameters .Add(prmUserNam e)
Cmd3.Connection = Conn3
Conn3.Open()
Rdr3 = Cmd3.ExecuteRea der()
If Rdr3.Read() Then
dbPass.Text = Rdr3("Password" ) 'used to compare to tblUsers
End If
Rdr3.Close()
Conn3.Close()
End Sub
"Jake S" <re*******@hotm ail.com> wrote in message news:<#V******* *******@TK2MSFT NGP12.phx.gbl>. ..
Hi all,
Is it possibele to set the datasource of a dropdown list to a datareader?
When I try to the only column I receive is a column populated by the
datasource name repeated the amount of times equivilant to the number of
records returned, instead of the two columns(value and text) I have
populated the datareader with.
Any ideas?
Thanks in advance
Cheers Jake