Click here to Skip to main content
15,120,268 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i am doing windows project, in which i have two datetimepicker controls, one for startdate and other for enddate

In runtime, when user selects the startdate and enddate from that controls, it should read the textfile(i.e) log.txt & search line by line get the matching of both these dates and as well as inbetween dates and write the data to a textbox or label control

for eg in log file the some data is like below

3/12/2013 2:51:47 PM - ASDASDASD.D20131203145019
4/12/2013 2:52:23 PM - ASDDFSDSA.C20131203145019
5/12/2013 2:52:37 PM - SDASAFAS_20131203182101.D
6/12/2013 3:17:11 PM - RRRTWEWA_20131203184602.D00
7/12/2013 3:35:32 PM - XBCNXCXCXC.D0120131203153408

if i search from 5 dec 2013 to 7 dec 2013

it should retrieve

5/12/2013 2:52:37 PM - SDASAFAS_20131203182101.D
6/12/2013 3:17:11 PM - RRRTWEWA_20131203184602.D00
7/12/2013 3:35:32 PM - XBCNXCXCXC.D0120131203153408

but uptonow i am getting

5/12/2013 2:52:37 PM - SDASAFAS_20131203182101.D
7/12/2013 3:35:32 PM - XBCNXCXCXC.D0120131203153408

i am retriving only the startdate & enddate matching data not inbetween date data

The below are some of the coding i have tried

string FDDate = FD.Date.ToString("M/d/yyyy");
string TDDate = TD.Date.ToString("M/d/yyyy");

string searchstring = EnterFileNameTextbox.Text.ToString();
string searchfromdate = FromDateTimePicker.Value.ToShortDateString();
string searchtodate = ToDateTimePicker.Value.ToShortDateString();

string line;
StringBuilder sb = new StringBuilder();
int counter = 0;
using (StreamReader file = new StreamReader(@"D:\log.txt"))
{
while ((line = file.ReadLine()) != null)
{
if (line.Contains(searchstring) && line.Contains(FDDate))
{
sb.AppendLine(line.ToString());
counter++;
}

else if (line.Contains(searchstring) && !line.Contains(FDDate))
{
if (FD.Date < TD.Date)
{
sb.AppendLine(line.ToString());
counter++;
FDDate = FD.Date.AddDays(1).ToShortDateString();
}
else if (FD.Date == TD.Date)
{
FDDate = FD.Date.ToShortDateString();
sb.AppendLine(line.ToString());
counter++;
}
}
}
}

ResultTextBox.Text = sb.ToString();
CountLabel.Text = counter.ToString();
}

catch (Exception ex)
{
MessageBox.Show("The file could not be read");
MessageBox.Show(ex.Message);
}


i am not getting idea to do the loop
Posted
Updated 12-Feb-14 0:37am
v3

1 solution

Assuming you mean to select the data between the two datetime values from your text file, we can't help you that much: we have no idea how data is organised in your text file.
If it is CSV data, or XML, then we can suggest some shortcuts, but if it isn't, then you will have to process each "row" in the text file, find the date field you are interested in, convert it to a DateTime and then see if it lies between the two values.

But without much better information, we really can't give you anything else.
   
Comments
Kumartyr 8-Feb-14 0:07am
   
@OriginalGriff

i have two datetimepicker control in windows form in which if user selects startdate & enddate in two controls, then the data between those 2 date should be retrived

1st i want only the date to be stored in datetime object instead of both date and time
2nd i need to compare two dates
OriginalGriff 8-Feb-14 5:55am
   
Try the Date property - it strips the time off:
DateTime dt = MyDateTimePicker.Value;
Console.WriteLine(dt.Date);

We still can't help you with the actual comparison, given we still know nothing about your data!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900