473,597 Members | 2,113 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Poulating a DropDownList with a database query


Hi everyone,
Can anyone see a problem with this code. I'm kinda new to asp.net and
I'm having a problem populating a drop down list with a database query.
The results of my query populate the list with the words
"System.Data.Co mmon.DbDataReco rd"

This is my code:

If Not Page.IsPostBack Then
Dim cn As New
SqlConnection(C onfigurationSet tings.AppSettin gs("CONN_STRING "))
Dim sql As String = "SELECT * FROM teams "
Dim dr As SqlDataReader
Dim cmd As New SqlCommand(sql, cn)
cn.Open()
dr = cmd.ExecuteRead er(CommandBehav ior.CloseConnec tion)
ddlTeam.DataSou rce = dr
ddlTeam.DataBin d()

cn.Close()
End If

Any help would be greatly appreciated.

Thanks
Rob
*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 18 '05 #1
3 1211
You need to set the DataTextField property for the
DropDownList control.

Switch to the HTML view of your web form and set the
DataTextField property to the name of the column you want
the DropDownList to display.

So, if you want to display a list of say, offices, it
would look something like this:
(assumming i selected the office_name and office_id
columns from my database into my DataReader)

<asp:dropdownli st id="ddlOffices " runat="server"
datatextfield=" office_name"
datavaluefield= "office_id" ></asp:dropdownlis t>

I've also set the DataValueField property above. This is
very handy cos it means when a user selects an office from
the drop down, you can easily find the corresponding id
for the selected office and pass it to your insert/update
statements.
-----Original Message-----

Hi everyone,
Can anyone see a problem with this code. I'm kinda new to asp.net andI'm having a problem populating a drop down list with a database query.The results of my query populate the list with the words
"System.Data.C ommon.DbDataRec ord"

This is my code:

If Not Page.IsPostBack Then
Dim cn As New
SqlConnection( ConfigurationSe ttings.AppSetti ngs ("CONN_STRING") ) Dim sql As String = "SELECT * FROM teams "
Dim dr As SqlDataReader
Dim cmd As New SqlCommand(sql, cn)
cn.Open()
dr = cmd.ExecuteRead er(CommandBehav ior.CloseConnec tion)
ddlTeam.DataSou rce = dr
ddlTeam.DataBin d()

cn.Close()
End If

Any help would be greatly appreciated.

Thanks
Rob
*** Sent via Developersdex http://www.developersdex.com ***Don't just participate in USENET...get rewarded for it!
.

Nov 18 '05 #2

Perfect,
Not only did you answer my question but you answered my next question as
to how can I get the ID to write it back to the database.

Thank you very much.

Rob
*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 18 '05 #3

Perfect,
Not only did you answer my question but you answered my next question as
to how can I get the ID to write it back to the database.

Thank you very much.

Rob
*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 18 '05 #4

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

Similar topics

2
1608
by: Stephan Bour | last post by:
Hi, I¹ve tried to implement a solution to populate a dropdownlist inside an editable datagrid. The code is simple enough and an example can be found here: <http://www.4guysfromrolla.com/webtech/050801-1.shtml> The problem I'm having is that the entire datagrid disappears after I click the Edit link. Could someone tell me what I'm doing wrong? Much appreciated, Stephan.
5
1799
by: DC Gringo | last post by:
I've got a command button to submit a value from a dropdown list that should then filter a SELECT query. I'm simply appending a WHERE colx = <variableSelectedFromDropdownList>. How do I pass this value into the event handler? -- MY EVENT HANDLER Sub RunReport_OnClick(sender As Object, e As System.EventArgs) _sqlStmt = _sqlStmt & " AND colx = '<variableSelectedFromDropdownList>'"
4
2096
by: wolfgang wagner | last post by:
hi all! after successfully integrating a dropdownlist in my datagrid i have another problem: i cannot set the selected index of the dropdownlistbox. here is my code: hardware.aspx -------------
3
2629
by: RFS666 | last post by:
Hello together, I tried to find out about populating an asp.net server control (a dropdownlist) from the clientside jscript, but I didn't find a solution up to now. I cannot use a html dropdownlist, because a database query in codebehind has to be done, when a new listItem is selected. So, I need to use a servercontrol dropdownlist. Now to my problem:
2
1505
by: fonnylasmana | last post by:
Hi, I have a dropdownlist that is basically pulled the data from database using sqldatasource. The dropdownlist will give years result. The results will be from 1967 to 2006. I have anoher query using sqldatasource that will give me the result of year 2004. How do I bind the second query so the dropdownlist will make 2004 as its default selected value? RIght now, 1967 is by default the selected value.
15
3091
by: glenn | last post by:
Hi folks, I have a DropDownList in a DataGrid that is populated from records in a database. I want to add a value that might be a string such as "Select a Company" for the first item since an OnSelectedIndex event is not fired if you select the first item. Does anyone know of an easy way to do this?
1
10383
by: Ben | last post by:
I have a formview with a few dropdownlists (software version, database version, etc). When a software version is selected, the database version dropdownlist updates itself accordingly. When in edit mode, everything works fine - the selected value in each list defaults to the value from the database record (selectedvalue=bind...). However, in insert mode (insertitem), it is not working. I pass in a value on the query string that I would...
4
4467
by: Sevu | last post by:
I am working with ASP.NET.I am using ReportViwer Control to show my report.I like to add dropdownlist with in the reportviewer control. ( Not top to the control some thing like that).I need to place dropdown near to Export link ( Excel and PDF Export).Is it possible to add at the dropdown list with in report viewer control?. I found the property as ReportViewer.Controls.Add.But its not working.Please help me. Regards, S.Sevugan.
0
1321
by: brian.newman | last post by:
I've created a dropdownlist whose values are drawn from an Access database. This list of items represents a user account. In the same record is a boolean field which records whether the user is an administrator. On my web form, I want a checkbox labelled "Is Admin" which will take the appropriate value from the Access database depending on which value is chosen in the dropdownlist. I'm using a checkboxlist in this case as it has...
0
7969
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
7886
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8381
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
6688
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
5847
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
3927
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2404
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
1
1494
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1238
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.