Drop Down List for SQL Database

D

Dharmen

Hey Guys,
I have a SQL database called price and a table called TB1, in it i have a
column called "loc"

I want to display the tiems in "loc" in a drop down list. I am able to do
this in a datagrid.
This is the code im using to display in datagrid:
SqlConnection Conn=new SqlConnection("Data Source=localhost;Database=price;
Integrated Security=SSPI;" +

"Initial Catalog=price");

Conn.Open();

string sqlString="SELECT Loc FROM TB1";

SqlCommand Comm = new SqlCommand(sqlString,Conn);

ddList1.DataSource=Comm.ExecuteReader();

ddList1.DataBind();

ddList1.

Conn.Close();



Can someone Please shwo me how to display this column in a DropDownList???
Thanks
 
A

Ann Marinas

Hi!

If you are using a drop-down list on your program, you need to bind that
list using the BindingContext.

for example, if you have a drop-down box named comboSample:

//Get the data from the database
//Place the data from the dataset

comboSample.DataSource = yourDataSet.Tables["TableName"];

//This column would be displayed
comboSample.DisplayMember = "ColumnName";

//This statement sets the value of your selected item, say if you're listing
a specific item, you want to know the primary key for these items in the
database
comboSample.ValueMember = "ColumnName

Hope this helps!

-A
 
J

James Morton

The simpliest way is to set the combobox datasource to the table in a
dataset. Set the datafield to the column you want to display. And set the
datavalue to the unique id on the table. If you want to create a subset of
values in this particular column (i.e. to get a distinct list) then the
proper way would be to create a view on the server instead of writing the
sql into the front end code.
 
A

Ann Marinas

Let me just correct my reply....

You connect your dropdown box by using the DataSource property, not the
BindingContext.

Sorry about that.

Ann

Ann Marinas said:
Hi!

If you are using a drop-down list on your program, you need to bind that
list using the BindingContext.

for example, if you have a drop-down box named comboSample:

//Get the data from the database
//Place the data from the dataset

comboSample.DataSource = yourDataSet.Tables["TableName"];

//This column would be displayed
comboSample.DisplayMember = "ColumnName";

//This statement sets the value of your selected item, say if you're listing
a specific item, you want to know the primary key for these items in the
database
comboSample.ValueMember = "ColumnName

Hope this helps!

-A

Dharmen said:
Hey Guys,
I have a SQL database called price and a table called TB1, in it i
have
a
column called "loc"

I want to display the tiems in "loc" in a drop down list. I am able to do
this in a datagrid.
This is the code im using to display in datagrid:
SqlConnection Conn=new SqlConnection("Data Source=localhost;Database=price;
Integrated Security=SSPI;" +

"Initial Catalog=price");

Conn.Open();

string sqlString="SELECT Loc FROM TB1";

SqlCommand Comm = new SqlCommand(sqlString,Conn);

ddList1.DataSource=Comm.ExecuteReader();

ddList1.DataBind();

ddList1.

Conn.Close();



Can someone Please shwo me how to display this column in a DropDownList???
Thanks
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

Error while running asp.net 3
SqlConnection 2
Drop SQL Database 2
How can I do this 2
C# database access 5
How to execute DROP/CREATE TABLE from c#? 0
Cannot drop database 4
Problems accessing SQL Server 2

Top