Drop Down List for SQL Database

  • Thread starter Thread starter Dharmen
  • Start date Start date
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
 
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
 
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.
 
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
 
Back
Top