System.FormatException: 'String was not recognized as a valid - vb.net-2010

Dim day As String = Label22.Text
Dim week As String = DateTime.ParseExact(day, "dd/MM/yyyy", CultureInfo.InvariantCulture).DayOfWeek.ToString
Label88.Text = week
more things but nothing

You forgot parenthesis after ToString method call :)
Each method call must contain the parameter list in parenthesis - even if the parameter list is empty.
Should be:
Dim day As String = Label22.Text
Dim week As String = DateTime.ParseExact(day, "dd/MM/yyyy", CultureInfo.InvariantCulture).DayOfWeek.ToString()
Rem --------------------------------------------------------------------------------------------------------^^ here!
Label88.Text = week

Related

How do I parse PDF Iso Date?

Using iText7 and documentinfo.GetMoreInfo("ModDate"), I get the following date string:
D:20220817113241+00'00'
How do I parse this in VB.NET?
With the help of the comments, I found a solution:
Initial date string:
D:20220817113241+00'00'
If string starts with "D:", remove it.
20220817113241+00'00'
If a string has a +, remove the part beginning with the +.
20220817113241
Use a full format string like this:
yyyyMMddHHmmsszzz
Now measure the length of the remaining date string.
For example:
Dim s As String = "D:20220817113241+00'00'"
Dim sFormat As String = "yyyyMMddHHmmsszzz"
Dim iLen As Integer = s.Length
sFormat = Mid(sFormat, 0, iLen)
This makes the format string as long as the date string and cuts off anything that is not present in the date string, like "zzz".
Now use that format string like this:
Dim yourDate = DateTime.ParseExact(s, sFormat, Nothing)

vb.net timeadding from two textbox

Please help me first before giving me a bad feedback.
I need to add two time from text box. But when I'm adding it only the minute was adding and not the hours.
Dim dt As DateTime = TxtDTEAP.Text
Dim wt As DateTime = TxtWTEAP.Text
Dim totalspan As New TimeSpan
Dim result As New DateTime
result = dt.AddMinutes(wt.Minute)
Me.TxtTRTEAP.Text = result
For example, the txtWTEAP.Text = 1:30 and txtDTEAP.Text = 2:50 the result should be 4:20 but the result showing on this code is 1:20 only.
Thank you so much!
Split the strings in the TextBoxes into an array. The first element will contain the hours and the second element will contain the minutes. The c following the split character tells the compiler this is a char not a string.
Use the constructor of the TimeSpan structure to create TimeSpans. The constructor takes 3 arguments, Hours as Integer, Minutes as Integer and Seconds as Integer.
Finally, we can do the addition.
Then display the result using .ToString with a format string. The \ is and escape character so it doesn't think the : is something else.
Private Sub GetTotalTime()
Dim parseTime1() As String = TxtDTEAP.Text.Split(":"c)
Dim parseTime2() As String = TxtWTEAP.Text.Split(":"c)
Dim dt As TimeSpan = New TimeSpan(CInt(parseTime1(0)), CInt(parseTime1(1)), 0)
Dim wt As TimeSpan = New TimeSpan(CInt(parseTime2(0)), CInt(parseTime2(1)), 0)
Dim result As TimeSpan = dt + wt
TxtTRTEAP.Text = result.ToString("h\:mm")
End Sub

How to delimiter text file

In my application, I need to Read delimited text file in vb.net
2016/05/15 21:59:13,739 [7] INFO - Login.User_Aut - o03dx1n Unknown -
Login: KST028
with:
Day 2016/05/15
Time 21:59:13,739
LogType [7] INFO
MethodName Login.User_Aut
SessionID o03dx1n Unknown
LoginID Login: KST028
Message
Here is my code so far.
OpenFileDialog1.ShowDialog()
Dim filepath As String = OpenFileDialog1.FileName
Dim inputstream As New IO.StreamReader(filepath)
Dim newstr() As String
Dim Day As String
Dim Time As String
Dim LogType As String
Dim MethodName As String
Dim SessionID As String
Dim LoginID As String
Dim Message As String
Do While inputstream.Peek <> -1
newstr = inputstream.ReadLine().Split(" ")
Day = newstr(0)
Time = newstr(1)
LogType = newstr(2)
MethodName = newstr(3)
SessionID = newstr(4)
LoginID = newstr(5)
Message = newstr(6)
Me.LogListView.Items.Add(Day)
Me.LogListView.Items.Item(LogListView.Items.Count - 1).SubItems.Add(Time)
Me.LogListView.Items.Item(LogListView.Items.Count - 1).SubItems.Add(LogType)
Me.LogListView.Items.Item(LogListView.Items.Count - 1).SubItems.Add(MethodName)
Me.LogListView.Items.Item(LogListView.Items.Count - 1).SubItems.Add(SessionID)
Me.LogListView.Items.Item(LogListView.Items.Count - 1).SubItems.Add(LoginID)
Me.LogListView.Items.Item(LogListView.Items.Count - 1).SubItems.Add(Message)
Loop
inputstream.Close()
End Sub
this code is wrong because :
day and time is correct but logtype,session id,loginid,message of columns is wrong
Here is how I would break this down using the hyphen as the delimiter. By doing this you recieve 4 parts (5 if message is included and separated by a hyphen)
the 0th element of the array contains everything up to the first hyphen and needs to be broken down to use each of its parts separately. I've commented the code which should help you understand better what each section is doing.
'these would need adjusted to fit your stream
Dim inputstream As String = "2016/05/15 21:59:13,739 [7] INFO - Login.User_Aut - o03dx1n Unknown - Login: KST028"
'this produces an array of 4 elements
Dim parts() As String = inputstream.Split("-"c)
'element 0 of the array needs to be broken down into its
'proper substrings
Day = parts(0).Substring(0, 10).Trim
Time = parts(0).Substring(12, parts(0).IndexOf("[") - 12).Trim
LogType = parts(0).Substring(parts(0).IndexOf("[") - 1).Trim
'these 3 elements of the array are retrieved fromt the split on the delimiter
MethodName = parts(1).Trim
SessionID = parts(2).Trim
LoginID = parts(3).Trim
'display in console window. Here you would assign these variables to
'your listview instead.
Debug.Print(String.Format("Day={0}{7}Time={1}{7}LogType={2}{7}Method={3}{7}Session={4}{7}Login={5}{7}Message={6}", Day, Time, LogType, MethodName, SessionID, LoginID, Message, Environment.NewLine))
End Sub
Again, this doesn't really handle Message as your string didn't contain one but from this you should be able to work out how to manage it. I wasn't sure if message was something from the string or something entered later

Conversion from string "M" to type 'Integer' is not valid

I am having an issues with conversion of values that I am trying to reference via a field that I want to format from an ERP System. I am unable to convert all of my values because they are being pulled out as strings, no matter if variables are set to integer or string. What am I doing that would cause this error, should variables be defined a different way?
Public Class Class1
Inherits erp.Rule
Public Overrides Function Execute() As erp.RuleResult
Dim Result As New RuleResult
Try
Dim date_recieved As Date
Dim month As String
Dim period As String
Dim Year1 As String
Dim Year As String
date_recieved = Data.Fields.GetFieldByAlias("date_received").FieldValue
month = Format(date_recieved, "M").ToString
Year = Data.Fields.GetFieldByAlias("yearAR").FieldValue
period = Data.Fields.GetFieldByAlias("periodAR").FieldValue
If period = month Then
If Year = Year1 Then
Exit Function
Else
MessageBox.Show("Date received does not match year", "Invalid Input")
End If
Else
MessageBox.Show("Date received does not match period", "Invalid Input")
End If
Catch ex As Exception
Result.Message = ex.Message
End Try
Result.Success = True
Return Result
End Function
Format does not accept a string parameter, by you passing "M" it is trying to convert the datatype you supply to the datatype the function accepts and since a string does not implicitly cast to an integer an error occurs
To format a Date type to various formats of string you just use your date variable and its subsequent .ToString() method with your formatting rules as an argument of .ToString()
Here is a link to the msdn explaining all the possible formatting options: https://msdn.microsoft.com/en-us/library/8kb3ddd4(v=vs.110).aspx?cs-save-lang=1&cs-lang=vb#code-snippet-1

How to get substring date (year) using vb.net?

I've been doing this, is it possible to get the middle string in datetime.
Example :
Basically my string return this '12/23/2015 12:00:00 AM'.
However, what i want is actually '15' from the year 2015.
I've been using something like this 'strDate = strDate.Substring(0, 9)',
which sometimes return me wrong when the date is something like this '1/2/2015 12:00:00 AM'
Can someone help me on this, i am not sure if we can get the middle.
Don't use string methods for this, use DateTime.TryParseExact:
Dim str = "12/23/2015 12:00:00 AM"
Dim dt As DateTime
If DateTime.TryParseExact(str, "MM'/'dd'/'yyyy hh:mm:ss tt", Globalization.CultureInfo.InvariantCulture, Globalization.DateTimeStyles.None, dt) Then
Dim yearOnly As String = dt.ToString("yy") ' 15
End If
Update: with CultureInfo.InvariantCulture and this format you don't even need to use TryParseExact, you can use Parse/TryParse immediately:
DateTime.TryParse(str, CultureInfo.InvariantCulture, DateTimeStyles.None, dt)
MSDN: Custom Date and Time Format Strings, The "yy" Custom Format Specifier
If you really want to treat it as just strings, you can get it by splitting twice. First on the space, then on the slashes:
Dim justDate as String = strDate.Split(" ")(0) 'contains "12/23/2015"
Dim justYear as String = justDate.Split("/")(2) 'contains "2015"
Dim partYear as String = justYear.Substring(2,2) 'contains "15"
or all in one line:
Dim partYear as String = strDate.Split(" ")(0).Split("/")(2).Substring(2,2)
Try this
dim dateStr as string
dateStr = "12/23/2015 12:00:00 AM"
dim dateobj as new DateTime
dateobj = DateTime.Parse(dateStr)
dim year as integer
year = dateobj.Year
dim youWantStr = year.ToString().Substring(2)
Console.WriteLine(youWantStr)