473,748 Members | 4,067 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

can the datasource of a drop down list be a datareader?

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
Nov 17 '05 #1
2 8194
Hi,

You have to set the DataTextField and DataValueField .

Natty Gur, CTO
Dao2Com Ltd.
28th Baruch Hirsch st. Bnei-Brak
Israel , 51114

Phone Numbers:
Office: +972-(0)3-5786668
Fax: +972-(0)3-5703475
Mobile: +972-(0)58-888377

Know the overall picture
*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 17 '05 #2
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

Nov 17 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
6160
by: Don Wash | last post by:
Hi There! I have a Server-side Drop-down box in ASP.NET (VB) page. What do I do to widen the Drop down box's Pull-Down list's width? I'm not talking about the Drop-down box's width but the box that appear when user clicks the drop-down arrow button. Because items in the Drop down list are a little long, user can't see the full thing when they pull down the items from the drop down list.
2
12622
by: Yoshitha | last post by:
hi I have 2 drop down lists in my application.1st list ontains itmes like java,jsp,swings,vb.net etc.2nd list contains percentage i.e it conatains the items like 50,60,70,80,90,100. i will select any skill in 1st drop down list then i'll select % of this skill in the 2nd list box , based on the percentage i've selected in the 2nd list box it has to display 2 sets of drop down list boxes at run time one for selecting skill and
0
2632
by: David | last post by:
In ASP.NET 2.0, is it possible to use a control that's on a Master Page as a select parameter in a content page's datasource control? For example, the Master Page has a drop down list of Departments. Content pages are various Department reports listed in a TreeView control on the Master Page. From the content page in the VWD Configure Data Source wizard (which I love!), I was disappointed when adding a WHERE clause that the Control ID list...
1
2502
by: pmelanso | last post by:
Hello, I have a drop down list which is dynatically loaded from a database and I have a second drop down list that is also dynatically loaded depending on what is selected in the first drop down box. This all works fine ... what isn't working is when I selected a different item in the first drop down list I want it to automatically reload the items in the second drop down list. I have a funcation called loadNames that laod the second...
3
1157
by: Tumurbaatar S. | last post by:
My page contains 2..3 DropDownList's with identical content. For example, country names. Is there a way to bound these lists to one data source? I think DataReader cannot act as shared source due to its forward-only nature. But can I do as shown below? SqlDataAdapter cmd = new SqlDataAdapter(sql, conn); DataSet ds = new DataSet(); cmd.Fill(ds); DataView dv = ds.Tables.DefaultView; list1.DataSource = dv;
6
1510
by: WebBuilder451 | last post by:
I have a drop down list of saved searches for a user and i'm trying to use the datasource control to populate the ddl. The select value in the storedproc is the user.Identity.name. How can i use this parameter? it's not a form field, nor a session, but it's there. Any idea how i can get this? -- thanks (as always) some day i''m gona pay this forum back for all the help i''m getting kes
8
7573
by: Ed Dror | last post by:
Hi there ASP.NET 2.0 VB & SQL Express Lest take Northwind Categories Products as example I create a table that hold these two together and I create a stored procedure like select ProductID, ProductName, CategoryID, from tblCategoryProducts Where (CategoryID = @CategoryID)
4
9296
by: TycoonUK | last post by:
Hi, As I do not have IE7 on my computer, I was wondering if there is a fault in my CSS Menu when using IE7. Please can someone look at my site - http://www.worldofmonopoly.co.uk and tell me if it works, and if it does not, tell me why it does not work. Thanks.
3
7359
by: penny111 | last post by:
Hi there, For my application, i need to have 3 drop down lists 1. drop down list of folder names 2. drop down list of documents in the folder selected 3. drop down list of instances of the document selected (my application uses the BusinessObjects Java Web Services SDK) The 2nd list is dependent on the 1st, while the 3rd list is dependent on the 2nd. In other words, this is what i want my application to do -select a folder from the...
0
9544
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9372
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8243
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6796
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6074
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4606
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4874
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3313
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2783
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.