Click here to Skip to main content
15,901,035 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I want to bind month list from database into DropDownList in asp.net.
My data in database is like this
Month
------
24
36
60

here i want to bind month list like
"24 Months"
"36 Months"
"60 Months"

So can any one help me please?
Posted

take month data in datatable dt

then do the following:


for(int i=0;i<dt.rows.count;i++)>
{
dropdown.Items.Add(dt.Rows[i]["Months"].ToString()+" Months");
}
 
Share this answer
 
The below snippet might help you:

C#
DataTable dt=GetMonthsFromDB();//Some method you have written to get the data from the DB
dropdownlist1.DataSource=dt;
dropdownlist1.DataTextField=dt.Columns[0].ToString() + "Months";
dropdownlist1.DataValueField=dt.COlumns[0].ToString();
dropdownlist1.DataBind();


The above snippet assumes that you have only one column coming in your datatable dt, if no, then you can change the column index accordingly..This should work..try it..

-Anurag
 
Share this answer
 
Make changes in the sql query for displaying values as required by you... if you can post the SQL Query you are using for populating drop down, i will be able to help you with Query also
 
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