Re: tricky TimeSpan editing
- From: "Bruce Wood" <brucewood@xxxxxxxxxx>
- Date: 22 Apr 2005 11:10:07 -0700
A couple of notes on your code.
First and most important, I want to make sure that the SQL you use to
fetch your TimeEntries includes an ORDER BY on ths StartTime. If you
just do a simple SELECT then ADO.NET does not make _any_ guarantees
about the order in which the rows will be returned.
Another way to solve this problem is to use a DataView and tell the
DataView to sort the rows. That way the DataView will always maintain
the rows correctly sorted, no matter what you do to them. This is what
I would do.
Finally, I would change the structure of your code: make a method
called FindTimeEntry(DataView view, int timeEntryId) that returns an
index. That will get rid of the main loop in your method, and change it
to be simply:
private bool CheckOverlap(DateTime ts, DateTime te) {
bool hasChanged = false;
// Change to return DataView... why do you want the whole DataSet?
DataView view = this.GetTimeEntryByDay(DayID);
int rowIndex = FindTimeEntry(view, TimeEntryID);
DataRowView drv = view[rowIndex];
if ((DateTime)drv["TimeEnd"] != te && rowIndex != view.Count - 1)
{
DataRowView nextDR = view[rowIndex +1];
// Note that this may change the order of the rows in the data
view
// (as it's sorted by TimeStart), so we must assume that the
// rowIndex is invalid after we do this.
nextDR["TimeStart"] = te;
// Save the next TimeEntry back to the database
hasChanged = true;
this.UpdateTimeEntry((int)nextDR["TimeEntryID"],
(int)nextDR["DayID"], (DateTime)nextDR["TimeStart"],
(DateTime)nextDR["TimeEnd"], (int)nextDR["EntryTypeID"]);
}
// Now fix all end times
for (int i = 0; i < view.Count - 1; i++)
{
DataRowView thisRow = view[i];
DataRowView nextRow = view[i + 1];
if ((DateTime)thisRow["TimeEnd"] !=
(DateTime)nextRow["TimeStart"])
{
thisRow["TimeEnd"] = nextRow["TimeStart"];
this.UpdateTimeEntry((int)thisRow["TimeEntryID"],
(int)thisRow["DayID"], (DateTime)thisRow["TimeStart"],
(DateTime)thisRow["TimeEnd"], (int)thisRow["EntryTypeID"]);
hasChanged = true;
}
}
return hasChanged;
}
.
- Follow-Ups:
- Re: tricky TimeSpan editing
- From: DKode
- Re: tricky TimeSpan editing
- References:
- tricky TimeSpan editing
- From: DKode
- Re: tricky TimeSpan editing
- From: Bruce Wood
- Re: tricky TimeSpan editing
- From: DKode
- tricky TimeSpan editing
- Prev by Date: Memory usage anomaly with server application
- Next by Date: Re: When i want to use a static method?
- Previous by thread: Re: tricky TimeSpan editing
- Next by thread: Re: tricky TimeSpan editing
- Index(es):
Loading