populate DropDownList from DB

V

Viktor Popov

Hi,
Could someone tell me how can be populated DropdownList from Data Base. I
use ASP.NET/C#/MS SQL SERVER.

Thanks!

Viktor
 
E

Eliyahu Goldin

Victor,

You need to get the database data into a dataset, set the DropdownList
properties DataSource to the dataset name and DataMember to the table name
and call DataBind method.

Eliyahu
 
V

Viktor Popov

Thanks for the reply!

Could you add the other code?

SqlConnection conn = new SqlConnection("Data Source=BLEK;Initial
Catalog=Estate; User ID=blek; Password=banderas");

SqlDataAdapter da = new SqlDataAdapter ("Select * FROM blek.TypeOffer ",
conn);


DataSet ds = new DataSet();

da.Fill (ds,"Table");

DropDownList2.DataSource = ds;
 
V

Viktor Popov

Hi, Thanks again for the reply! I wrote that but it doesn't work. Could you
check for an error?

Thanks

private void Page_Load(object sender, System.EventArgs e)

{

SqlConnection conn = new SqlConnection("Data Source=BLEK;Initial
Catalog=Estate; User ID=blek; Password=banderas");

SqlDataAdapter da = new SqlDataAdapter ("Select * FROM blek.TypeOffer ",
conn);


DataSet ds = new DataSet();

da.Fill (ds,"Table");

DropDownList2.DataSource = ds;

DropDownList2.DataMember = "Table";

DropDownList2.DataBind () ;

}
 
E

Eliyahu Goldin

DropDownList2.DataMember = "Table";
DataBind (); // or DropDownList2.DataBind ();
 
H

Hans Kesting

see inline

Viktor Popov said:
Hi, Thanks again for the reply! I wrote that but it doesn't work. Could you
check for an error?

Thanks

private void Page_Load(object sender, System.EventArgs e)

{

SqlConnection conn = new SqlConnection("Data Source=BLEK;Initial
Catalog=Estate; User ID=blek; Password=banderas");

SqlDataAdapter da = new SqlDataAdapter ("Select * FROM blek.TypeOffer ",
conn);


DataSet ds = new DataSet();

da.Fill (ds,"Table");

DropDownList2.DataSource = ds;

DropDownList2.DataMember = "Table";

DropDownList2.DataBind () ;

add the code (with the correct values):
DropDownList2.DataTextField = "<name of column for visible text>";
 
V

Viktor Popov

I tryed to do that:
private void Page_Load(object sender, System.EventArgs e)
{
// Put user code to initialize the page here

SqlConnection conn = new SqlConnection("Data Source=BLEK;Initial
Catalog=Estate; User ID=blek; Password=banderas");
SqlDataAdapter da = new SqlDataAdapter ("Select * FROM blek.TypeOffer ",
conn);
conn.Open();
da.SelectCommand.ExecuteReader();
DataSet ds = new DataSet();
da.Fill (ds,"Table");
DropDownList2.DataSource = ds;
DropDownList2.DataMember = "Table";
DropDownList2.DataTextField = "TypeOffer";
DropDownList2.DataValueField = "OfferID";
DropDownList2.DataBind () ;]
}

but it doesn't work.
What could I do?

Thanks!
 
V

Viktor Popov

There is no error. When I start the application the DropDownList is empty.
There are no values, no items. My table is full with data
TypeOffer
---------------------
OfferID TypeOffer
1 rent
2 sell
3 change

Do you know what's wrong?
Eliyahu Goldin said:
what's the error?

Viktor Popov said:
I tryed to do that:
private void Page_Load(object sender, System.EventArgs e)
{
// Put user code to initialize the page here

SqlConnection conn = new SqlConnection("Data Source=BLEK;Initial
Catalog=Estate; User ID=blek; Password=banderas");
SqlDataAdapter da = new SqlDataAdapter ("Select * FROM
blek.TypeOffer
",
conn);
conn.Open();
da.SelectCommand.ExecuteReader();
DataSet ds = new DataSet();
da.Fill (ds,"Table");
DropDownList2.DataSource = ds;
DropDownList2.DataMember = "Table";
DropDownList2.DataTextField = "TypeOffer";
DropDownList2.DataValueField = "OfferID";
DropDownList2.DataBind () ;]
}

but it doesn't work.
What could I do?

Thanks!
 
E

Eliyahu Goldin

Can set a breakpoint after the line with da.Fill (ds,"Table"); and check if
the dataset actually gets any data?

Viktor Popov said:
There is no error. When I start the application the DropDownList is empty.
There are no values, no items. My table is full with data
TypeOffer
---------------------
OfferID TypeOffer
1 rent
2 sell
3 change

Do you know what's wrong?
Eliyahu Goldin said:
what's the error?

Viktor Popov said:
I tryed to do that:
private void Page_Load(object sender, System.EventArgs e)
{
// Put user code to initialize the page here

SqlConnection conn = new SqlConnection("Data Source=BLEK;Initial
Catalog=Estate; User ID=blek; Password=banderas");
SqlDataAdapter da = new SqlDataAdapter ("Select * FROM
blek.TypeOffer
",
conn);
conn.Open();
da.SelectCommand.ExecuteReader();
DataSet ds = new DataSet();
da.Fill (ds,"Table");
DropDownList2.DataSource = ds;
DropDownList2.DataMember = "Table";
DropDownList2.DataTextField = "TypeOffer";
DropDownList2.DataValueField = "OfferID";
DropDownList2.DataBind () ;]
}

but it doesn't work.
What could I do?

Thanks!
 
V

Viktor Popov

It doesn't work....I'm stucked.....
My last code is this. When I RUN it there are no errors but the DropDownList
is not populated and the msg.Text=""


private void Page_Load(object sender, System.EventArgs e)
{
// Put user code to initialize the page here

SqlConnection conn = new SqlConnection("Data Source=BLEK;Initial
Catalog=Estate; User ID=blek; Password=banderas");
SqlDataAdapter dad = new SqlDataAdapter ("SELECT blek.TypeOffer.OfferID,
blek.TypeOffer.TypeOffer FROM blek.TypeOffer", conn);


DataTable table=new DataTable();
conn.Open();
dad.Fill(table);
conn.Close();
DropDownList2.DataSource=table;
DropDownList2.DataValueField = "OfferID";
DropDownList2.DataTextField = "TypeOffer";
DropDownList2.DataBind ();
msg.Text = table.Rows.Count.ToString();
}
 
Joined
Feb 19, 2010
Messages
1
Reaction score
0
populate dropdown after entering the value in the textbox and clicking the button

i have a textbox and button in asp frontend.
if i enter a value in the textbox and click thebutton..the value should be stored in a table named "usertable"(say).i have a dropdown box also.
if i click the button after enterng a value in the textbox,tat value should be displayed in the dropdown.code plz????????????
 

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

Members online

Forum statistics

Threads
473,767
Messages
2,569,572
Members
45,046
Latest member
Gavizuho

Latest Threads

Top