Click here to Skip to main content
15,890,741 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Can a dropdownlist or combobox be directly bound to a database field? For a text box I can use the
ASP.NET
<asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("Reviewer1Title") %>' />



I tried to use the field bindings from the databindings popup but it is greyed out.
Posted
Updated 28-Nov-11 16:49pm
v2
Comments
D K N T H 28-Nov-11 23:06pm    
you have to provide source for dropdownlist
srinivas vadepally 29-Nov-11 0:07am    
Andy,
have you get the solution?
if not, send us the code and we do give the better solution.
Andy Morris 29-Nov-11 22:17pm    
Here is a copy of my current code:
<asp:DropDownList ID="DropDownList3" runat="server"
DataSourceID="SqlDataSource1" DataTextField="Reviewer1Title"
DataValueField="Reviewer1Title" Height="25px" style="margin-left: 0px"
Width="162px">

<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:PKLConnectionString %>" SelectCommand="SELECT tblEvaluationListing.Reviewer1Title
FROM tblEvaluationListing
GROUP BY tblEvaluationListing.Reviewer1Title
HAVING ((Not (tblEvaluationListing.Reviewer1Title) Is Null));">

using data source and data source id property you can directly bind your dropdown list to your database.

like you have to declare on sqldatasource control to your page and use the id of that sqldatasource control to directly bind data to your drop down list

C#
<asp:sqldatasource id="CostGroupDataSource" runat="server" selectcommand="SELECT tblCostGroup.costGroupID, tblCostGroup.costGroup FROM tblCostGroup ORDER BY tblCostGroup.costGroup">




<asp:dropdownlist id="ddltest" width="152px" runat="server" datasourceid="CostGroupDataSource" datatextfield="COSTGROUP" datavaluefield="COSTGROUPID" appenddatabounditems="true">
<asp:listitem text="" value="">
 
Share this answer
 
v2
C#
string strConn = "Data Source=localhost;uid=sa;pwd=password;Initial Catalog=northwind";
                 SqlConnection mycn = new SqlConnection(strConn);
                 DataSet ds = new DataSet();
                 SqlDataAdapter myda = new SqlDataAdapter("Select * FROM CategoryTable ", mycn);
                 myda.Fill(ds);
                 MyDropDownList.DataSource = ds;
                 MyDropDownList.DataTextField = "CategoryName";
                 MyDropDownList.DataValueField = "CategoryId";
                 MyDropDownList.DataBind();
           }
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900