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

  • Thread starter Thread starter GG
  • Start date Start date
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 ...
 
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>


-Brock
DevelopMentor
http://staff.develop.com/ballen
 
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();
}
}
 
Back
Top