Calendar - Repeating Events

R

Rob Johnson

I have an ASP.Net calendar feature which allows users to add events and
configure whether or not they repeat at various frequencies (i.e, daily,
weekly, monthly, Sat/Sun, etc.). What I'm looking for is some C# code that
will calculate a date of "Every Other Week" based on a starting date and
ending date.

Example: If a user enters an event dated October 3, 2005 and would like it
to repeat every other week until October 31, 2005, the code will return the
following dates:

October 3, 2005
October 17, 2005
October 31, 2005

Any help would be greatly appreciated.

Rob
 
R

Rob Johnson

It's a bit more complex than that Ab. In order to determine how many dates
there are representing "Every other Week" starting on October 3, 2005 and
ending on October 31, 2005, you first have to figure out how many entries
there should be. In this case, there should be 3. A "for loop" is required
to loop through Oct 3 to Oct 31 and return either the number of days or
weeks. Example, the code below will insert the correct number of entries for
an event that repeated every day from Oct 3 to Oct 31 (intNumberofDays is
determined using TimeSpan):

if (EventFrequency == "Day")
{
for(int i = 0; i <= intNumberofDays; i++)
{
string strInsert = "INSERT INTO Events " +
"(EventStartDateTime, " +
"EventEndDateTime) VALUES " +
"('" + dtEventStartDateTime.AddDays(i) + "', " +
"'" + dtEventEndDateTime.AddDays(i) + "') ";

objConnection = new SqlConnection(dbPath);
objCommand = new SqlCommand(strInsert,objConnection);
objConnection.Open();
objCommand.ExecuteNonQuery();
objConnection.Close();
}
}

The problem I'm running into is substituting "intNumberofDays" with
"intNumberofWeeks" will return 5 weeks, where I really need it to return 3
weeks. "i" will then be 3 which I can plug into a revised SQL insert
statement and use "dtEventStartDateTime.AddWeeks(i)" to populate my database
with the 3 dates representing "Every Other Week" (Oct 3, Oct, 17, Oct 31).
Simply subtracting 2 weeks won't work since the duration of events entered
by users can change from entry to entry.
 
R

Rob Johnson

I think I've resolved my issue. The key was to divide "intNumberofWeeks" by
2. The following code seems to work fine...so far:

for(int i = 0; i <= intNumberofWeeks / 2; i++)
{
string strInsert = "INSERT INTO Events " +
"(EventStartDateTime, " +
"EventEndDateTime) VALUES " +
"('" + dtEventStartDateTime.AddDays(i * 14) + "', " +
"'" + dtEventEndDateTime.AddDays(i * 14) + "') ";

objConnection = new SqlConnection(dbPath);
objCommand = new SqlCommand(strInsert,objConnection);
objConnection.Open();
objCommand.ExecuteNonQuery();
objConnection.Close();
}
 
S

Sal

There's no need to care about the number of inserts you're doing at the time
you're calculating it (unless of course you have this constraint).

DateTime dt = startDate;
int frequency = 14;
int insertCount = 0;
int maxInserts = 100; //may not be necessary for you...

//begin tran

while( dt <= endDate && insertCount < maxInserts )
{
//insert dt into your date table

dt.AddDays( frequency );
insertCount++;
}

if( insertCount >= maxInserts )
{rollback tran}
else
{commit tran}
 
A

Abubakar

I think its as simple as adding number of days to the datetime type as:

DateTime dt = new DateTime(2005, 10, 3);
dt=dt.AddDays(14);

now dt has 17th oct. You keep on adding days like that until the the end
date comes, like in a loop.

Hope that helps.

Ab.
http://joehacker.blogspot.com
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top