473,396 Members | 2,004 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

DropDownList and SqlDataReader

TOm
How do I bind a SqlDataReader to a dropdownlist box?

I know how to construct a SqlDataReader dr and get the
values. Buit how do I bind it to a DropDownList box in C#?

This does not work

cboUsers.DataSource = dr;
cboUsers.Items.Clear();
cboUsers.DataTextField = dr.GetValue(1).ToString();
cboUsers.DataValueField = dr.GetValue(0).ToString();
cboUsers.DataBind();

Please advise.

Thanks

Tom
Nov 15 '05 #1
5 14248
Hi TOm,

Try setting the DataTextField and the DateValueField properties of the
dropdownlist.

Cheers,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation

"TOm" <an*******@discussions.microsoft.com> wrote in message
news:08****************************@phx.gbl...
How do I bind a SqlDataReader to a dropdownlist box?

I know how to construct a SqlDataReader dr and get the
values. Buit how do I bind it to a DropDownList box in C#?

This does not work

cboUsers.DataSource = dr;
cboUsers.Items.Clear();
cboUsers.DataTextField = dr.GetValue(1).ToString();
cboUsers.DataValueField = dr.GetValue(0).ToString();
cboUsers.DataBind();

Please advise.

Thanks

Tom

Nov 15 '05 #2
Tom
Yip! Setting the properties in the HTML workd but not in
the code behind. Thanks for that!

Now that I know how to add items to a dropdown, do you
what I'd need to do to set a specific item in the as the
default one selected?

Tom
-----Original Message-----
Hi TOm,

Try setting the DataTextField and the DateValueField properties of thedropdownlist.

Cheers,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation

"TOm" <an*******@discussions.microsoft.com> wrote in messagenews:08****************************@phx.gbl...
How do I bind a SqlDataReader to a dropdownlist box?

I know how to construct a SqlDataReader dr and get the
values. Buit how do I bind it to a DropDownList box in C#?
This does not work

cboUsers.DataSource = dr;
cboUsers.Items.Clear();
cboUsers.DataTextField = dr.GetValue(1).ToString();
cboUsers.DataValueField = dr.GetValue(0).ToString();
cboUsers.DataBind();

Please advise.

Thanks

Tom

.

Nov 15 '05 #3
Hi Tom,

Just set the desired SelectedIndex property in the control, now if you don't
know the index of the elem you need then you have to iterate on the
collection and set the correct index, see that you have a SelectedItem and
SelectedValue but they are read only.

Cheers,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation

"Tom" <an*******@discussions.microsoft.com> wrote in message
news:08****************************@phx.gbl...
Yip! Setting the properties in the HTML workd but not in
the code behind. Thanks for that!

Now that I know how to add items to a dropdown, do you
what I'd need to do to set a specific item in the as the
default one selected?

Tom
-----Original Message-----
Hi TOm,

Try setting the DataTextField and the DateValueField

properties of the
dropdownlist.

Cheers,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation

"TOm" <an*******@discussions.microsoft.com> wrote in

message
news:08****************************@phx.gbl...
How do I bind a SqlDataReader to a dropdownlist box?

I know how to construct a SqlDataReader dr and get the
values. Buit how do I bind it to a DropDownList box in C#?
This does not work

cboUsers.DataSource = dr;
cboUsers.Items.Clear();
cboUsers.DataTextField = dr.GetValue(1).ToString();
cboUsers.DataValueField = dr.GetValue(0).ToString();
cboUsers.DataBind();

Please advise.

Thanks

Tom

.

Nov 15 '05 #4
Tom

This code will help you....

It gets a datareader then binds it to the dropdownlist using in quotes the

name of the columns for the datavaluefield and datatextfield.

Therefore you do not need to use

dr.GetValue(1).ToString();

instead use:

dr.DataValueField = "yourkeyfield - column name in the reader"

dr.DataTextField = "yourtextfield - column name in the reader"

Hope it helps

Kuv

------------

SqlConnection conn = new SqlConnection("database=northwind; server=(local);

user=sa; pwd=;");

conn.Open();

SqlDataReader dr;

SqlCommand cmd = new SqlCommand("Select * from

customers",conn);

dr = cmd.ExecuteReader();

dr1.DataSource = dr;

dr1.DataTextField = "contactname";

dr1.DataValueField = "customerid";

dr1.DataBind();

"TOm" <an*******@discussions.microsoft.com> wrote in message
news:08****************************@phx.gbl...
How do I bind a SqlDataReader to a dropdownlist box?

I know how to construct a SqlDataReader dr and get the
values. Buit how do I bind it to a DropDownList box in C#?

This does not work

cboUsers.DataSource = dr;
cboUsers.Items.Clear();
cboUsers.DataTextField = dr.GetValue(1).ToString();
cboUsers.DataValueField = dr.GetValue(0).ToString();
cboUsers.DataBind();

Please advise.

Thanks

Tom

Nov 15 '05 #5
Tom
Kuv:

Many thanks

Tom
-----Original Message-----
Tom

This code will help you....

It gets a datareader then binds it to the dropdownlist using in quotes the
name of the columns for the datavaluefield and datatextfield.
Therefore you do not need to use

dr.GetValue(1).ToString();

instead use:

dr.DataValueField = "yourkeyfield - column name in the reader"
dr.DataTextField = "yourtextfield - column name in the reader"
Hope it helps

Kuv

------------

SqlConnection conn = new SqlConnection ("database=northwind; server=(local);
user=sa; pwd=;");

conn.Open();

SqlDataReader dr;

SqlCommand cmd = new SqlCommand("Select * from

customers",conn);

dr = cmd.ExecuteReader();

dr1.DataSource = dr;

dr1.DataTextField = "contactname";

dr1.DataValueField = "customerid";

dr1.DataBind();

"TOm" <an*******@discussions.microsoft.com> wrote in messagenews:08****************************@phx.gbl...
How do I bind a SqlDataReader to a dropdownlist box?

I know how to construct a SqlDataReader dr and get the
values. Buit how do I bind it to a DropDownList box in C#?
This does not work

cboUsers.DataSource = dr;
cboUsers.Items.Clear();
cboUsers.DataTextField = dr.GetValue(1).ToString();
cboUsers.DataValueField = dr.GetValue(0).ToString();
cboUsers.DataBind();

Please advise.

Thanks

Tom

.

Nov 15 '05 #6

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

Similar topics

12
by: Stanley J Mroczek | last post by:
How do you load a dropdownlist when edit is clicked in a datagrid ? <Columns> <asp:BoundColumn DataField="OptionDescription" ItemStyle-Wrap="True" HeaderText="Option...
1
by: djozy | last post by:
Please, I am using SqlDatareader in C# and when I want to put int value ,that is get by datareader, to dropdown list I get this error: An invalid data source is being used for idlist. A valid...
2
by: rmorvay | last post by:
I am trying to dynamically build a dropdownlist and bind it to a cell in a grid. I tried to utilize the following code but I am stuck at the point where I bind the dropdownlist to the grid cell. ...
2
by: William Mild | last post by:
When I databind a value/text combination from an sqldatareader to a dropdownlist, the trailing spaces are getting included in the dropdownlist. I can RTRIM from within the SQL to fix this...
3
by: wei7028 | last post by:
i have create a webform that contain a dropdownlist that will show data in my database. when i try to select a value in the dropdownlist and display its value in a label but an error occur. my...
0
by: Daniel Doyle | last post by:
Hello and apologies in advance for the amount of code in this post. I've also sent this message to the Sharepoint group, but thought that ASP.NET developers may also be able to help, even though...
4
by: jjack100 | last post by:
I have a DropDownList that is nested inside a Repeater. The datasource of the DropDownList is declared in the aspx, not the codebehind. So we have this: <asp:Repeater ID="rptOptions"...
1
by: Nir | last post by:
hello all! i really need a help of a friend... i am working on a self project. and i am realy lost here... i have to Dropdown list ,the first one gives me the name of the hospital and the other...
1
by: Brent White | last post by:
I can't figure out what I'm doing differently with this one drop-down list control from the other two that are working just fine. Background: I am constructing a page that will allow a user to...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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,...
0
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...
0
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,...
0
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.