Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: ASP.NET
code>Hi Guys,
I have dropdownlist with static text.
 
<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.
 
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 19-Nov-12 19:46pm
Edited 19-Nov-12 19:58pm
v2
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

hi,
 
try this,
//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 Smile | :)
please mark this as solution if it helps you! thanks
  Permalink  
Comments
CH Guravaiah at 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();
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 4

Hi
 
Below I created a separate class where I select data from my database
 //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
 
N.B cboCourses //is a DropDownList name
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...
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 6

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
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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.. Smile | :)
  Permalink  

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



Advertise | Privacy | Mobile
Web03 | 2.8.141015.1 | Last Updated 10 Aug 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100