Click here to Skip to main content
15,892,839 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,
I need to bind data to datset as Years and Months in Below foramt from current month and year through C# code or Sql Server Fucntion or Procedure.
if any one know colud you tell me the answer.

VB
Year    Month
2013    May
2013    June
2013    July
2013    August
2013    September
2013    October
2013    November
2013    December
2014    January
2014    February
2014    March
2014    April


Regards
Nanda Kishore.CH
Posted
Updated 17-May-13 20:52pm
v2
Comments
Mayur Panchal 18-May-13 2:52am    
Do you want any specific range or only current year and month.
nandkishorre 18-May-13 2:56am    
Specific Range.
For example i give value in textbox as 20
based on textbox value, current month and year
20 rows bind to dataset like wise as above.
Mayur Panchal 18-May-13 2:58am    
You mean previous twenty months? Right?
nandkishorre 18-May-13 3:03am    
No. Present to Future Years and Months
nandkishorre 18-May-13 3:30am    
Thank you. Its Working.

1 solution

Below method will return DataSet with data what you exactly want.

C#
private DataSet GetListOfMonths(int range)
{
    DataSet ds = new DataSet();
    ds.Tables.Add("Months");
    ds.Tables["Months"].Columns.Add("Year");
    ds.Tables["Months"].Columns.Add("Month");
    DateTime dtCurrent = DateTime.Now;
    for (int i = 0; i < range; i++)
    {
        DataRow dr = ds.Tables["Months"].NewRow();
        dr["Year"] = dtCurrent.Year;
        dr["Month"] =System.Globalization.CultureInfo.CurrentCulture.DateTimeFormat.GetMonthName(dtCurrent.Month);
        ds.Tables["Months"].Rows.Add(dr);
        dtCurrent = dtCurrent.AddMonths(1);
    }

    return ds;
}
 
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