Click here to Skip to main content
15,886,787 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,
How to add academic years in dropdownlist(Programatically).

Eg: 2010-2011,
2011-2012 etc.

Please solve my problem
Posted
Comments
thatraja 23-Dec-11 4:01am    
What's the problem?
What about dropdownlist.Items.Add?
Hari Krishna Prasad Inakoti 23-Dec-11 4:07am    
Adding years in dropdownlist (2010-2011) like
Hari Krishna Prasad Inakoti 23-Dec-11 4:12am    
Hi Thatraja
How to add years in dropdownlist?
thatraja 23-Dec-11 4:15am    
Check my answer. & go ahead.

 
Share this answer
 
v2
Comments
Hari Krishna Prasad Inakoti 23-Dec-11 4:05am    
i know how to add dynamically but i want how to add years?
Use DateTime[^] to get the years, do string concatenation(Ex. 2010-2011) & add it to dropdownlist.

DateTime.Now.Year() will return current year.

EDIT

Agree with Prasad. Store values in database & load it in dropdownlist. Consider this is your table(Academic_Years).
Years
--------------
2008-2009
2009-2010
2010-2011
2011-2012
--------------

Use below query to select the data
SQL
SELECT * FROM Academic_Years

Write code something like below
C#
DataTable dt = GetYears();//Custom function to load Academic years from database
dropdownlist.DataSource = dt;
dropdownlist.DataBind();

Finally I suggest you to learn everything ASP.NET Tutorial @ W3Schools[^]
 
Share this answer
 
v2
Comments
Hari Krishna Prasad Inakoti 23-Dec-11 4:23am    
how to apply to dropdownlist
thatraja 23-Dec-11 4:38am    
Check my updated answer. Particularly read the last line which is useful for you.
koolprasad2003 23-Dec-11 4:41am    
Exactly raja. OP has to put his input either in database or in file and then read it. instead of hardcode it on page.
5. for you.
Hari Krishna Prasad Inakoti 23-Dec-11 4:42am    
ok thanq for ur valuable suggestion ...
I know using database how to retrieve values.......
Hi Hari,

Please use this code in the Page_Load() event

C#
if (!Page.IsPostBack)
        {
            for (int i = 2000; i <= 2012; i++)
            {
                int next = i + 1;
                DropDownList1.Items.Add(i.ToString() + "-" + next.ToString());
            }
        }

C++




Please make as Resolved if this resolves your issue,

Please revert back to me in case of any cocnerns.

Thanks And Regards
Suman Zalodiya
 
Share this answer
 
Comments
Suman Zalodiya 28-Dec-11 0:11am    
Hi Hari,

Please make it resolved if your problem is resolved.
it is basic. i don't know, what you want exactly. but this may help you.

C#
dropdownlist.Items.Add("2010-2011");


There is no limit.
you can add items directly through code or your can take years in XML file and load XML in dropdowonlist
 
Share this answer
 
v2
Comments
Hari Krishna Prasad Inakoti 23-Dec-11 4:14am    
i know this code but i want some academic years in dropdownlist.How many items i can add?
koolprasad2003 23-Dec-11 4:16am    
check updated reply.
To fill the dropwdonlist from database use DataReader

C#
cmd = new SqlCommand("select * from AcademicYears", con);
       cmd.CommandType = SqlDataSourceCommandType.Text;
       dr = cmd.ExecuteReader();
       while (dr.Read())
       {
               drp1.Items.Add(new ListItem(dr[AcademicYearName], dr[AcademicYearId]));
           // This is your Columne Name - "AcademicYearName"
           // This is your Primary key column - "AcademicYearId"
       }


There are two properties of Dropdownlist
1. DataTextField
2. DataValueField

Hence i have retrieve two fields from database.


Thanks
 
Share this answer
 
v2

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