Few years back I have to convert a FoxPro script that generate a custom yearly calendar to asp.net using c#. In a year it consists of 13 Periods, 4 weeks per period and 7 days a week. The concept behind is that the days per period is evenly divided into 28 days. The Logic is very simple yet it is worth taking a look. For documentation I just post here the code that generates the calendar.Below is the code. Happy coding every one.
protected void GenerateCalendar()
{
DateTime dtdateStart = DateTime.Now ;
if (textboxtransdate.Text != "")
{
dtdateStart = Convert.ToDateTime(this.textboxtransdate.Text);
}
int _intend = System.Data.Linq.SqlClient.SqlMethods.DateDiffDay(dtdateStart, Convert.ToDateTime(textboxDateEnd.Text));
int _pdcldr = 1;
int _wkcldr = 1;
int _dayCal = 1;
int _wkcounter = 0; //week counter
int _periodcount = 0; // period counter
int _yrcldr = Convert.ToInt32(this.txtYear.Text);
// loop thru until we reached the number of days specified
for (int i = 0; i <= _intend; i++)
{
if (i > 0)
{
dtdateStart = dtdateStart.AddDays(1);
}
switch (Convert.ToString(dtdateStart.DayOfWeek).ToLower())
{
case "sunday":
_dayCal = 1;
break;
case "monday":
_dayCal = 2;
break;
case "tuesday":
_dayCal = 3;
break;
case "wednesday":
_dayCal = 4;
break;
case "thursday":
_dayCal = 5;
break;
case "friday":
_dayCal = 6;
break;
case "saturday":
_dayCal = 7;
break;
}
_wkcounter = ++_wkcounter;
//counter for the days
_periodcount = ++_periodcount;
//increase _wkcldr by 1
if (_wkcounter == 8)
{
_wkcldr = ++_wkcldr;
_wkcounter = 1;
}
//1 period = 28 days if _periodcount ==28 increase the _pdcldr by 1 and reset the _periodcount
if (_periodcount == 29)
{
//reset the _wkcldr and increase _pdcldr
_wkcldr = 1;
_pdcldr = ++_pdcldr;
if (_pdcldr >= 14)
{
_wkcldr = 5;
_pdcldr = 13;
}
//reset the _periodcount which serves as the counter for the period
_periodcount = 1;
}
//prepare to append the data to the database
using (SqlConnection myConnection = new SqlConnection(AppConfiguration.ConnectionString3))
{
SqlCommand myCommand = new SqlCommand("Downtime_sp_CreateUpDateDoleCalendar", myConnection);
myCommand.CommandType = CommandType.StoredProcedure;
myCommand.Parameters.AddWithValue("@transdate",dtdateStart);
myCommand.Parameters.AddWithValue("@yrcldr", _yrcldr);
myCommand.Parameters.AddWithValue("@pdcldr", _pdcldr);
myCommand.Parameters.AddWithValue("@wkcldr", _wkcldr);
myCommand.Parameters.AddWithValue("@daycal", _dayCal);
DbParameter returnValue;
returnValue = myCommand.CreateParameter();
returnValue.Direction = ParameterDirection.ReturnValue;
myCommand.Parameters.Add(returnValue);
myConnection.Open();
myCommand.ExecuteNonQuery();
myConnection.Close();
}
}
}
No comments:
Post a Comment