populate DropDownList from DB

  • Thread starter Thread starter Viktor Popov
  • Start date Start date
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
 
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
 
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;
 
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 () ;

}
 
DropDownList2.DataMember = "Table";
DataBind (); // or DropDownList2.DataBind ();
 
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>";
 
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!
 
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!
 
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!
 
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();
}
 
Back
Top