Question

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 in between dates and write the data to a textbox or label control

for eg in log file, 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 up to now 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 in between date data.

The below are some of the coding I have tried:

try
{
    // ...

    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.

No correct solution

OTHER TIPS

I would recommend using DateTime.TryParse (doc here) instead of just converting. That will allow you to provide valid formatting, handle failures (convert will throw if it fails).

The part about using the Date property seems fine, and comparison operators will work as expected.

Try and deal with DateTime datatypes here rather than converting everything to string. After reading the line from the file, parse out the date. Assuming it is always going to be the first element in the file, and is followed by a space, you could do this by using DateTime lineDate = DateTime.Parse(line.Split(" ")[0]);

Once you have the date from the file in that format, and you have DateTime fromDate and DateTime toDate obtained from your date pickers, you can write if(lineDate >= fromDate && lineDate <= toDate) sb.AppendLine(line);

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top