Friday, December 16, 2011

Get weeks in a month with start and end date of each week using C#.NET

This article explains how to get all weeks in a month with start and end date.

public ArrayList GetWeeks(int _year, int _month)
{
int countDays = DateTime.DaysInMonth(_year, _month);
ArrayList _weeksList = new ArrayList();

for (int i = 1; i <= countDays; i = i + 7)
{
DateTime _startDate = new DateTime(_year, _month, i);
DateTime _finishDate = _startDate.AddDays(6);
string sReturn = _startDate.ToShortDateString()
+ " - " + _finishDate.ToShortDateString();
_weeksList.Add(sReturn);
}

//zero-based array
return _weeksList;
}

Output Sample:

If _year = 2012 and _month = 04, the output would be

01-04-2012 - 07-04-2012
08-04-2012 - 14-04-2012
15-04-2012 - 21-04-2012
22-04-2012 - 28-04-2012
29-04-2012 - 05-05-2012

Get Month Names using C#.NET

This article explains how to get all month names of the year using C#.NET. First create an instance of the class DateTimeFormatInfo and get its CurrentInfo. There is property called MonthNames in the DateTimeFormatInfo which returs an array of month names.

using System.Globalization;
using System.Collections.Generic;

public
ListItem[] GetMonthNamesList()
{
DateTimeFormatInfo dfi = DateTimeFormatInfo.CurrentInfo;
string[] _months = dfi.MonthNames;
List<ListItem> _monthNamesList = new List<ListItem>();
for (int i = 0; i <= 11; i++)
{
_monthNamesList.Add(
new ListItem(_months[i], (i + 1).ToString()));
}
return _monthNamesList.ToArray();
}

Output sample:

January
February
March
April
May
June
July
August
September
October
November
December

Thursday, August 25, 2011

Pivot Table - Example #2

CREATE TABLE #tempRecords
(
Record int,
BookNo int,
BookValue varchar(50)
)

INSERT INTO #tempRecords VALUES (1,1,'One')
INSERT INTO #tempRecords VALUES (1,2,'Two')
INSERT INTO #tempRecords VALUES (1,3,'Three')
INSERT INTO #tempRecords VALUES (1,4,'Four')

INSERT INTO #tempRecords VALUES (2,1,'One')
INSERT INTO #tempRecords VALUES (2,2,'Two')
INSERT INTO #tempRecords VALUES (2,3,'Three')
INSERT INTO #tempRecords VALUES (2,7,'Seven')
INSERT INTO #tempRecords VALUES (2,8,'Eight')

INSERT INTO #tempRecords VALUES (3,9,'Nine')

INSERT INTO #tempRecords VALUES (4,2,'Two')
INSERT INTO #tempRecords VALUES (4,5,'Five')
INSERT INTO #tempRecords VALUES (4,10,'Ten')

/** Create crosstab using PIVOT **/
SELECT *
FROM #tempRecords
PIVOT
(

MAX(BookValue)
FOR [BookNo]
IN ([1],[2],[3],[4],[5],[6],[7],[8])
)
AS p

DROP TABLE #tempRecords


Pivot Table - Example #1

CREATE TABLE #tempProducts
(
Supplier varchar(15),
Product varchar(20),
Product_Quantity integer
)
INSERT INTO #tempProducts VALUES ('HP','Software',300000)
INSERT INTO #tempProducts VALUES ('HP','Hardware',150000)
INSERT INTO #tempProducts VALUES ('HP','Monitor',250000)
INSERT INTO #tempProducts VALUES ('HP','Keyboard',500000)
INSERT INTO #tempProducts VALUES ('Apple','Software',20000)
INSERT INTO #tempProducts VALUES ('Apple','Hardware',125000)
INSERT INTO #tempProducts VALUES ('Apple','Monitor',800000)
INSERT INTO #tempProducts VALUES ('Apple','Keyboard',60000)
INSERT INTO #tempProducts VALUES ('Dell','Software',120000)
INSERT INTO #tempProducts VALUES ('Dell','Hardware',250000)
INSERT INTO #tempProducts VALUES ('Sony','Software',900000)
INSERT INTO #tempProducts VALUES ('Sony','Hardware',450000)
INSERT INTO #tempProducts VALUES ('Sony','Monitor',100000)
/** Create crosstab using PIVOT **/
SELECT *
FROM #tempProducts
PIVOT
(
SUM(Product_Quantity)
FOR [Product]
IN ([Software],[Hardware],[Monitor],[Keyboard])
)
AS p
DROP TABLE #tempProducts


Codeigniter Shield Authorization

Codeigniter Shield Authorization CodeIgniter Shield is the official authentication and authorization framework for CodeIgniter 4. It provide...