-
Jun 7th, 2016, 03:07 PM
#1
Thread Starter
Junior Member
[RESOLVED] Comparing DateTimePicker to text file data?
Hello All:
I am trying to compare the selected date (short type) in a DateTimePicker to a line from a text file. The text file contains this information:
3/21/2016 7:57:00 AM
3/21/2016 4:30:09 PM
Total Time: 8:33:09
What I need is to know if the current date (from the DateTimePicker) is after the dates listed in the text file. I have the entire program working except for this piece, and I cannot figure out how to check it line by line from the text file. This is what I have so far for this sub:
Code:
Sub GetRecords()
Dim startDate as String = DateTimePicker1.Value.ToString("M/d/yyyy")
Dim endDate as String = DateTimePicker2.Value.ToString("M/d/yyyy")
Dim EMP_NAME As String = "C:\mydata.txt"
Dim strReadFile As New System.IO.StreamReader(EMP_NAME)
Dim line As String
Do While Not strReadFile.EndOfStream
line = strReadFile.ReadLine
If line. ---STUCK HERE--- Then
---I need to compare the datetimepickers to the values here from the text file---
---If the text file lines are between these dates, then add them to a text box---
Exit Do
End If
Loop
strReadFile.Close()
End Sub
Any help with this would be greatly appreciated.
DB
Last edited by davebowlin; Jun 7th, 2016 at 03:16 PM.
-
Jun 7th, 2016, 03:35 PM
#2
Re: Comparing DateTimePicker to text file data?
Your first step is to load all the lines, except for the last line, into a collection. Next you will want to query the collection to return any DateTime values are less than the desired date from your DateTimePicker. Here is one such implementation:
Code:
Dim lines() As String = IO.File.ReadAllLines("dates.txt")
Dim dt As DateTime
Dim priorDates As IEnumerable(Of DateTime) = From line As String In lines Where DateTime.TryParse(line, dt) AndAlso dt < DateTimePicker1.Value Select Convert.ToDateTime(line)
-
Jun 8th, 2016, 11:11 AM
#3
Thread Starter
Junior Member
Re: Comparing DateTimePicker to text file data?
Thank you! I haven't had time to try this out yet as I'm on the road. I appreciate your help!
DB
-
Jun 8th, 2016, 11:33 AM
#4
Thread Starter
Junior Member
Re: Comparing DateTimePicker to text file data?
Hello again - sorry, I am getting an error this time: "Range variable 'line' hides a variable in an enclosing block or a range variable previously defined in the query expression." I have a Visual Basic 2010 Programmer's Reference book, but I don't see anything about this error listed. Here is the code. Any help figuring this out would be greatly appreciated.
Code:
Sub GetRecords()
Dim startDate As String = DateTimePicker1.Value.ToString("M/d/yyyy")
Dim endDate as String = DateTimePicker2.Value.ToString("M/d/yyyy")
Dim EMP_NAME As String = FileStorage + LCase(cboEmployees.SelectedItem) + ".emp"
Dim strReadFile As New System.IO.StreamReader(EMP_NAME)
Do While Not strReadFile.EndOfStream
Dim line As String = strReadFile.ReadLine
Dim dt As DateTime
Dim priorDates As IEnumerable(Of DateTime) = From line As String In line Where DateTime.TryParse(line, dt) AndAlso dt < DateTimePicker1.Value Select Convert.ToDateTime(line)
rtbReport.Text = rtbReport.Text + priorDates.ToString
Loop
strReadFile.Close()
End Sub
I'm sorry for being so new at this. Your help is appreciated very much.
-
Jun 8th, 2016, 11:40 AM
#5
Lively Member
Re: Comparing DateTimePicker to text file data?
Here you are declaring "line":
Code:
Dim line As String = strReadFile.ReadLine
Here you are also declaring "line" and checking for "line":
Code:
...(Of DateTime) = From line As String In line...
You may want to change one or the other
Code:
Sub GetRecords()
Dim startDate As String = DateTimePicker1.Value.ToString("M/d/yyyy")
Dim endDate as String = DateTimePicker2.Value.ToString("M/d/yyyy")
Dim EMP_NAME As String = FileStorage + LCase(cboEmployees.SelectedItem) + ".emp"
Dim strReadFile As New System.IO.StreamReader(EMP_NAME)
Do While Not strReadFile.EndOfStream
Dim strline As String = strReadFile.ReadLine
Dim dt As DateTime
Dim priorDates As IEnumerable(Of DateTime) = From line As String In strline Where DateTime.TryParse(line, dt) AndAlso dt < DateTimePicker1.Value Select Convert.ToDateTime(line)
rtbReport.Text = rtbReport.Text + priorDates.ToString
Loop
strReadFile.Close()
End Sub
-
Jun 8th, 2016, 12:06 PM
#6
Thread Starter
Junior Member
Re: Comparing DateTimePicker to text file data?
Thank you! I am a bit unclear on the "Dim priorDates..." line, but when I run this, the returned value in the text box is: System.Linq.Enumerable+WhereSelectEnumerableIterator`2[System.String,System.DateTime]
This is listed for each entry in the actual text file. I'm very confused, and I apologize for the lack of knowledge concerning this. This project has turned into a bit more than I anticipated. I am learning, albeit slowly. Am I returning the wrong data, perhaps, with this line:
Code:
rtbReport.Text = rtbReport.Text + priorDates.ToString
? Thank you!
-
Jun 8th, 2016, 12:24 PM
#7
Lively Member
Re: Comparing DateTimePicker to text file data?
I am unclear on how to proceed because you are working on stuff I do not have access to, and can't really makeup things to throw in...I might confuse it even more.
I will have to pass this to someone else.
-
Jun 8th, 2016, 12:26 PM
#8
Thread Starter
Junior Member
Re: Comparing DateTimePicker to text file data?
Originally Posted by Recoil1980
I am unclear on how to proceed because you are working on stuff I do not have access to, and can't really makeup things to throw in...I might confuse it even more.
I will have to pass this to someone else.
Thank you for your help, and no problem. Basically, I'm trying to check the date inside of a text file line against the date in the current DateTimePicker1 value. If the date in the text is equal to or older than the DateTimePicker1 value, then print that text file line to a textbox. I hope that makes sense.
DB
-
Jun 8th, 2016, 12:29 PM
#9
Thread Starter
Junior Member
Re: Comparing DateTimePicker to text file data?
Here's what I'm attempting to do:
1 - Open a text file, and read the lines one by one.
2 - If a line contains a date (ex: 6/1/2016), compare that date to the value of DateRangePicker1.
3 - If the date is equal to or greater than the date in DateRangePicker1, print that text file line into a textbox.
Thanks,
DB
-
Jun 8th, 2016, 01:23 PM
#10
Re: Comparing DateTimePicker to text file data?
Originally Posted by davebowlin
Here's what I'm attempting to do:
1 - Open a text file, and read the lines one by one.
2 - If a line contains a date (ex: 6/1/2016), compare that date to the value of DateRangePicker1.
3 - If the date is equal to or greater than the date in DateRangePicker1, print that text file line into a textbox.
Thanks,
DB
Hi Dave,
dday's code works by loading all the lines from the text file into an array and then working against the contents of the array using LINQ. However, your code is reading each line from the file one by one, and that's why you're having problems integrating dday's code into your own.
Sticking with your original approach for now:
Firstly, where ever possible, always work with Date values, not with String representaions of dates.
So change the first couple of lines of your code to
Code:
Dim startDate As DateTime = DateTimePicker1.Value
Dim endDate As DateTime = DateTimePicker2.Value
Next, as you read each line from the file, it will hopefully be a String representation of a date, so you have to try to convert it to an actual Date value. You can use Date.TryParse Method to accomplish this. You pass the Method a String representing a date, and a Date variable. If it can convert the string into a Date value then that value will be stored in the Date variable, and the method will return the Boolean value True. If it can't convert the String, then the method will return the Boolean value False.
However, there's a complication with this: string representation of dates can be formatted in various ways, so it's always best to specify what that format is. You do this by using an overload of the TryParse method that takes a format specifier. For example:
Code:
Dim theDateVariable as Date
Dim succeeded as Boolean = Date.TryParse(line, Globalization.CultureInfo.InvariantCulture, Globalization.DateTimeStyles.None, theDateVariable)
See https://msdn.microsoft.com/en-us/lib...vs.110%29.aspx
You have to be careful what format specifier you pick matches the format of the dates stored in your text file. I've used CultureInfo.InvariantCulture (which basically matches the USA Date format) based on the dates you showed in an earlier post.
Then, if that succeeds, you can compare the converted Date value against the Date values from your 2 DateTimePickers:
Code:
If theDateVariable >= startDate AndAlso theDate <= endDate Then
So your code becomes something like:
VB Code:
Sub GetRecords()
Dim startDate As DateTime = DateTimePicker1.Value
Dim endDate As DateTime = DateTimePicker2.Value
Dim EMP_NAME As String = "C:\Test\DaveB Dates.txt " '"C:\mydata.txt"
Dim strReadFile As New System.IO.StreamReader(EMP_NAME)
Dim theDate As DateTime
Dim line As String
Do While Not strReadFile.EndOfStream
line = strReadFile.ReadLine
If Date.TryParse(line, Globalization.CultureInfo.InvariantCulture, Globalization.DateTimeStyles.None, theDate) Then
If theDate >= startDate AndAlso theDate <= endDate Then
rtbReport.AppendText(line & vbLf)
End If
End If
Loop
strReadFile.Close()
End Sub
Note that the values from the DateTimePickers are not specifying time components, whereas the dates you've shown from your text file are. You may want to look into this.
Also note that the above is finding dates BETWEEN the values in the 2 DateTimePickers, not dates that are greater than or equal to just one of them.
-
Jun 8th, 2016, 01:34 PM
#11
Re: Comparing DateTimePicker to text file data?
I don't have access to Visual Studios half the time, so I rely on online .NET playgrounds. The advantage to this is that I can code online. The disadvantage is that I can only do console applications. So I'll provide a console application example of doing the same thing and try to make it as simple as possible to convert to a Windows Form Application.
Fiddle: https://dotnetfiddle.net/rT9z6Q
-
Jun 8th, 2016, 01:34 PM
#12
Thread Starter
Junior Member
Re: Comparing DateTimePicker to text file data?
AHA! Okay, I see now what I am doing wrong - thank you! I thought the dates had to be converted to string in order to compare them within a text file. I have tried this code and it works perfectly. Thank you so very much for your help - much appreciated!
This is exactly what I was trying to accomplish. I'm learning, a bit slowly I think, but learning. Thank you!
DB
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|