Click here to Skip to main content
15,885,546 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
XML
<tr>
                <td align="center" class="style1">
                    <asp:Label runat="server" ID="Role" Text="Role">
                    </asp:Label>
                </td>
                <td>
                    <asp:DropDownList ID="DropDownList1" runat="server" DataTextField="Role"
                        DataValueField="ID" onselectedindexchanged="DropDownList1_SelectedIndexChanged">

                    </asp:DropDownList>
                </td>
            </tr>




In my deisgn i kept one drop down for role and i m maintaining a database table as ROLEID in that table contains two columns ID, ROLE. by default i gaved some roles as 1 Admin, 2 Leader 3 Manager so what is my question is how to bind the table to drop down list
Posted
Updated 9-Jan-13 20:36pm
v5
Comments
[no name] 10-Jan-13 2:12am    
Your question is not clear, elaborate it :)
MT_ 10-Jan-13 2:20am    
Write some proper subject and whatever written in subject, move to the question. Use "Improve Question" widget.

C#
protected void Page_Load(object sender, EventArgs e)
{
if(!IsPostBack)
bindDropDownList();
}
protected void bindDropDownList()
{
string constr = ConfigurationManager.ConnectionStrings["yourConnectionString"].ToString();         
SqlConnection con =new SqlConnection (constr);
con.Open();
SqlCommand com=new SqlCommand ("select * from ROLEID",con); 
SqlDataAdapter da=new SqlDataAdapter(com);
DataSet ds = new DataSet (); 
da.Fill(ds);  
DropDownList1.DataTextField = ds.Tables[0].Columns["ROLE"].ToString();         DropDownList1.DataValueField=ds.Tables[0].Columns["ID"].ToString();
DropDownList1.DataSource=ds.Tables[0];
DropDownList1.DataBind();      
}


Remember that you should use stored procedure in place of direct query.
 
Share this answer
 
I think you are a beginner and need ADO.Net knowledge first.
Please go through the basics of .Net development.
 
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