Click here to Skip to main content
14,097,639 members
Rate this:
 
Please Sign up or sign in to vote.
I have a table name updates property is Date,Jan,feb,march,april...dec

That means that i have the Date column and 12 months

The question is that i have to select 3 months on the bases of the date. Suppose the if the Date is 1 it should select the current month and last 2 months. That is for from date 1 to 31 it should display the results for the current months and last two months.
so question is how can the months could be selected automatically.

My current query is
SqlCommand command = new SqlCommand("SELECT Date_,Decm,Jan,Feb from updates order by Date_", con);
SqlDataAdapter da = new SqlDataAdapter(command);
DataTable dt = new DataTable();
da.Fill(dt);
GridView1.DataSource = dt;
GridView1.DataBind();
Posted
Updated 4-Feb-16 7:24am
v3

1 solution

Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

Declare a function which returns current month name with next two months:
public static string GetMonths(int month)
{
	string[] monthColumns = { "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" };

	string monthName = string.Empty;

	//// Get current month with next two months
	//monthName = monthColumns[month - 1] + ", "
	//        + monthColumns[(12 + (((month) > 11) ? 0 : month)) - 12] + ", "
	//        + monthColumns[(12 + (((month + 1) > 11) ? (month + 1) - 12 : month + 1)) - 12];

	// Get current month with previous month
	monthName = monthColumns[((month - 3) < 0) ? (12 + (month - 3)) : month - 3] + ", "
		+ monthColumns[((month - 2) < 0) ? (12 + (month - 2)) : month - 2] + ", "
		+ monthColumns[month-1];

	return monthName;
}

Next call the above method:
string sqlQuery = "SELECT Date_, "+ GetMonths(DateTime.Now.Month)+ " FROM updates order by Date_";

// Next execute above sqlQuery as per your code
SqlCommand command = new SqlCommand(sqlQuery, con);
SqlDataAdapter da = new SqlDataAdapter(command);
   
v3
Comments
Shantanu sinha 3-Feb-16 14:40pm
   
the solution is fine but its giving result for current month +next two months
where as i want last two month + current months.
Manas_Kumar 3-Feb-16 21:43pm
   
Can you give 2-3 cases here..
Shantanu sinha 4-Feb-16 11:37am
   
suppose you have to display today result then the display should be like:-
Date_ Dec Jan Fab
1 23 32 09
2 55 33 45
3 32 23 55
4 45 43 01
5 62 02
6 12 11
7 24 12
8 12 32
9 21 63
10 76 32
11 22 32
.
.
31 21 32
and if the month is march
then result should be like:-
Date_ Jan Feb Mar
1
2
3
4
5
6
7
Manas_Kumar 4-Feb-16 12:46pm
   
If it current month is Jan or Feb then what result do you expect?

Updated my answer. Please check..
Shantanu sinha 19-Feb-16 7:33am
   
the updated one is fine
but what i am looking is
if suppose current month is Feb
then result should be
Dec Jan Feb
if current month is march
then the result should be
jan feb March
if current month is December
then the result should be
Oct Nov Dec

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

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Cookies | Terms of Service
Web03 | 2.8.190518.1 | Last Updated 4 Feb 2016
Copyright © CodeProject, 1999-2019
All Rights Reserved.
Layout: fixed | fluid

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