Re: Iterate through 2 dates
I'm not entirely sure, that I understand completely, what you need. But here is my guess at something you might be able to use:
The function getemptyattendance below will create an empty attendance-list (SortedList<DateTime, Int32>) in the specified date-interval. This attendance-list can subsequently be used to update attendances for specific dates. The button_click event will create an empty attendance-list in the date-interval specified by the DateTimePickers dtpStart and dtpEnd, and read all the lines of the attendance-file. The attendance is updated by one (didn't really know if attendance was the first or last element or possibly neither) for each line in the file, that has a date occuring in the attendance-list. The complete attendance list is finally written out to the console-window.
CSharp Code:
private void button3_Click(object sender, EventArgs e)
{
// This line reads all lines in the file at one time. There is nothing wrong with using a streamreader and reading
// one line at the time, but unless the file is really large, the code below is a bit more readable.
string[] datelines = File.ReadAllLines(Application.StartupPath + "\\Logs\\Sample Logs.txt");
string[] lineelements;
DateTime curdate;
SortedList<DateTime, Int32> attendancelist = getemptyattendancelist(dtpStart.Value, dtpEnd.Value);
foreach (string line in datelines)
{
lineelements = line.Split(',');
if (lineelements.Length == 4)
{
if (DateTime.TryParse(lineelements[1] + " " + lineelements[2], out curdate))
{
if (attendancelist.ContainsKey(curdate.Date))
{
// The current line in the file is in the required format, and the date in within the range
// of the attendancelist. The line is stored in the variable [line], and the 4 parts of the
// line, seperated by kommas, are stored in [lineelements].
//
// The first and last elements can be found by using Int32.TryParse(lineelements[0], out ivar)
// and Int32.TryParse(lineelements[3], out ivar), where ivar is declared as an int32 variable.
// Or you can use Convert.ToInt32(lineelements[0 or 3 respectively]), if you are sure that no
// errors can occur.
//
// The current attendance entry can be updated with the following line:
attendancelist[curdate.Date] += 1;
}
}
else
{
// The second and third elements combined dosen't constitute a correctly formatted datetime.
}
}
else
{
// The line dosen't have the correct number of parts seperated by komma.
}
}
Console.WriteLine("The file provided the following attendance:");
Console.WriteLine("-------------------------------------------");
foreach (DateTime loopdate in attendancelist.Keys)
Console.WriteLine(string.Format("Date: {0}, attendance: {1}", loopdate.ToShortDateString(), attendancelist[loopdate]));
}
private SortedList<DateTime, Int32> getemptyattendancelist(DateTime startdate, DateTime enddate)
{
if (startdate.Date > enddate.Date) return null;
int days = enddate.Subtract(startdate).Days;
SortedList<DateTime, Int32> rv = new SortedList<DateTime,int>();
foreach (DateTime loopdate in Enumerable.Range(0, days + 1).Select(i => startdate.Date.AddDays(i)))
rv.Add(loopdate, 0);
return rv;
}
Regards Tom
Re: Iterate through 2 dates
THanks TOm.
Or is there any best way to manage logs of attendance?
The way that all log with same date will be save as single line.
Something like this
1,2011/12/1,7:31,0
2,2011/12/1,7:32,0
3,2011/12/1,7:33,0
4,2011/12/1,7:34,0
1,2011/12/1,11:50,0
2,2011/12/1,11:51,0
3,2011/12/1,11:52,0
4,2011/12/1,11:53,0
1,2011/12/1,13:01,0
2,2011/12/1,13:02,0
3,2011/12/1,13:03,0
4,2011/12/1,13:04,0
1,2011/12/1,17:31,0
2,2011/12/1,17:32,0
3,2011/12/1,17:33,0
4,2011/12/1,17:34,0
I want to save that on my database as
My table format.
ID------Date--------AMin--------AMout-------PMin-------PMout
1----2011/12/1----7:31--------11:50--------13:01------17:31
2----2011/12/1----7:32--------11:51--------13:02------17:32
so on....
Re: Iterate through 2 dates
In my opinion, you should go ahead and create a database. Making example code based on a text-file is all good and well, but pretty much all of that code will be useless once you get a database to store the data in. You will most likely rely on a strongly typed dataset and use SQL to retrieve:
* Logins for a specified person (possibly over a given period in time).
* Logins for a specified period.
* Missing logins for a specified person (possibly over a given period).
* etc.
eliminating the need to store text-files and/or declare classes or variables to maintain the data.
I implemented a similar system for a youth-centre a couple of years ago. The data basically consisted of a table of persons, each uniquely defined by an id, and a table of logins using inner join on the person-table on the id and keeping a login-datetime (in your case also a logout-datetime or possibly login-duration). Each young person had a magnetic card, and when they scanned it in a given club, the information was sent to a central server storing the information for all 21 clubs.
The system allowed for a wide range of statistics to be drawn, cards being banned etc., but basically it is similar in nature to your project.
Start out with a very simple database consisting of just 2 tables with as few columns as possible - for example:
Person_Table:
* Id (integer, primary key)
* Name (string)
Attendance_Table:
(* Possible autonumber for primary key or no key at all - depending on whether you plan on deleting/editing rows or permanently keep them as is).
* Person_Id (integer - refers to the person-table)
* Login (DateTime - the date and time when the student arrives).
* Additional columns can be added as needed, but no more columns are required to make a working example.
Once you have the database created with the 2 tables, you will find that using databases is pretty well-supported and not that difficult in the .Net framework. The most difficult task (IMO) will be creating SQL-sentences to extract the required information - ie. attendance in professor X's classes in May, number of days Bryan was absent in any class in 2012 etc. etc.
Regards Tom
Re: Iterate through 2 dates
I'll take your advice sir Thom.
Maybe I need to recode the previous system(made by other coder) that generate a logs from biometric device.
I will make save directly to database instead of making it in text file format.
I'll update you later about the progress.