Bind a SQL table to a dropdownlist at design time? Surely it can be done?

G

GG

I have a typical ASP page with a dropdownlist web form control. I want
this to auto-populate from a column in a sql table.

I've tried dragging a sql connection data control onto the form, played
with data binding, everything! I'm sure this is easy, can anyone point
out the steps I need to take?

Thanks ...
 
B

Brock Allen

Here's an example:

<script runat="server">
protected void Page_Load(object sender, EventArgs e)
{
using (SqlConnection cn = new SqlConnection("server=localhost;database=pubs;trusted_connection=yes;"))
{
using (SqlCommand cmd = cn.CreateCommand())
{
cmd.CommandText = "select * from authors";
cn.Open();
using (SqlDataReader rdr = cmd.ExecuteReader())
{
_ddl1.DataSource = rdr;
_ddl1.DataTextField = "au_fname";
_ddl1.DataBind();
}
}
}
}
</script>
<body>
<form id="form1" runat="server" >
<asp:DropDownList runat="server" ID="_ddl1"></asp:DropDownList>
</form>
</body>
</html>
 
G

Guest

Hi!

Dude, hope that u can get the values in a DataSet from the DataBase.Now
things are pretty starightfwd from here
just put in this code


if(objDs.Tables.Count != 0) //objDs is the dataset which has the table
Employee
{
if(objDs.Tables[0].Rows.Count != 0)
{
ddlEmployeeId.DataSource = objDs;
ddlEmployeeId.DataValueField = "EmployeeID";
ddlEmployeeId.DataTextField = "EmpName";
ddlEmployeeId.DataBind();
}
}
 
Joined
May 14, 2009
Messages
2
Reaction score
0
Hello readers today i found a helpful post on how you can optimize your code to bind with Dropdown List:

shawpnendu.blogspot.com/2009/05/how-to-bind-or-populate-data-into.html

HOPE IT WILL HELP READERS.
 

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,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top