Click here to Skip to main content
15,881,588 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
code>Hi Guys,
I have dropdownlist with static text.

XML
<asp:DropDownList ID="ddlStatus" runat="server" Width="150px">
    <asp:ListItem Text="Select Item" Value="0"></asp:ListItem>
  <asp:ListItem Text="Accept" Value="1"></asp:ListItem>
  <asp:ListItem Text="Reject" Value="2"></asp:ListItem>
   </asp:DropDownList>


I am getting the data from database and bind the data to dropdownlist.

C#
dsEmpRequisition = new DataSet();

            dsEmpRequisition = g.Get_CFO_Requisition(hdnRequisitionNumber.Value);
            if (dsEmpRequisition.Tables[0].Rows.Count > 0)
            {   
                ddlApprove.SelectedItem.Text= dsEmpRequisition.Tables[0].Rows[0]["APPROVED_HR_STATUS"].ToString();
// here
}


how can bind to the dropdownlist.

Please any one help me.
Posted
Updated 19-Nov-12 19:58pm
v2

hi,

try this,
C#
//put your datasource here
ddlApprove.DataSource  = dsEmpRequisition.Tables[0];
//data from specific column that you want to display
ddlApprove.DataTextField="TitleColumn"; 
//value of the data that you want to get when the text is selected
ddlApprove.DataValueField="ValueColumn";
//lastly, bind it
ddlApprove.DataBind();


hope you get it!! Happy coding :)
please mark this as solution if it helps you! thanks
 
Share this answer
 
Comments
CH Guravaiah 20-Nov-12 2:02am    
thanks for reply. Already bind the data to dropdownlist statically. But based on application getting the data from database and put into the dropdownlist dynamically. no need all values from DB. Need only perticular value.
Like this:
ddlApprove .Text= dsEmpRequisition.Tables[0].Rows[0]["APPROVED_HR_STATUS"].ToString();
Member 13636078 23-Jan-18 4:19am    
but it only show dsEmpRequisition value in dropdownlist no other items on the bottom
Hi
Below I created a separate class where I select data from my database
C#
//Metod to populate combobox for cognos courses
   public ArrayList CognosComboBox()
   {
       dbConnection();

       string sqlCBO = "SELECT DISTINCT  courseCode FROM Courses;";
       dbCmd = new OleDbCommand(sqlCBO, dbConn);

       OleDbDataReader reader = dbCmd.ExecuteReader();

       ArrayList myList = new ArrayList();
       myList.Add("Choose course");
       while (reader.Read())
       {
           myList.Add(reader[0].ToString());
       }
       reader.Close();


       dbConn.Close();

       return myList;

   }


Below is the back code

C#
N.B cboCourses //is a DropDownList name

C#
public void populateCombo()
    {
        DataCls dtClass = new DataCls();

        cboCourses.DataSource = dtClass.CognosComboBox();
        cboCourses.DataBind();


    }


Then you can call populateCombo() any where you want even on Page_Load()


Hope it helps...
 
Share this answer
 
For example you want the employee names into a drop down list.empname is
thecolumn in Employee Table.then stored procedure to get the list of employee names.
select empname from EmpTable;

Then create a employee class with attributes empname,empid etc.
UI:

asp page code:
<asp:label id="lblEmpName" runat="server" text="Employee Name" xmlns:asp="#unknown">


<asp:dropdownlist id="ddlEmpName" runat="server" xmlns:asp="#unknown">

asp.cs code


protected voir Page_Load(object sender,EventsArgs e)
{
if(!ISPostBack)
{
List(Employee) emp=new List(Employee)();
BLL objUserBLL = new BLL();
emp=objUserBLL.GetEmployeeNames;
foreach(string item in emp)
{
ddlEmpName.Items.Add(new ListItem(emp[item].ToString()));
}
}
}
Busines logic layer(BLL-a class library):


filename:empbll.cs
code:
public List(Employee) GetEmployeeNames
{
DAl objUserDAL=new DAL;
List(employee) ls=new List(Employee)();
ls=objUSerDAL.RetrieveEmployeeNames;
return ls;
}

Data Access Layer(DAL-a class library):
filename:empdal.cs
code:
public List(Employee) RetrieveEmployeeNames
{
SqlConnection con=new SqlConnection("Database=;inital catalog=;username=;passowrd;")
SqlCommand cmd=SqlCommand("Stored procedure name",con);
cmd = new SqlCommand(StoredProcedure);
string name;
cmd.open();
SqlDataReader rdr=cmd.ExecuteReader();
while(rdr.Read())
{
List(employee) ls=new List(Employee)();
name=Convert.Tostring(rdr["EmpName"]);
}
ls.add(name);
return ls;
}

Hope this example will clear you doubt....If are satisfies with this solution mark this as solution if it helps you! thanks
 
Share this answer
 
Use Smart Tag of DropDownList, where you will find Add New DataSoure go through the steps...And then If you find any difficulty, let us know.. :)
 
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