When using maskedtextbox in my code, it returns an exception:
Conversion from string "" to type 'Date' is not valid
My code is:
Dim msg, first, second As String
Dim firstdate, seconddate As Date
first = MaskedTextBox1.Text
second = MaskedTextBox2.Text
firstdate = CDate(first)
seconddate = CDate(second)
msg = "Days from today: " & DateDiff(DateInterval.Month, firstdate, seconddate)
MsgBox(msg)
But my code works fine if a textbox is used in place of a maskedtextbox like:
Dim msg, first, second As String
Dim firstdate, seconddate As Date
first = TextBox3.Text
second = TextBox4.Text
firstdate = CDate(first)
seconddate = CDate(second)
msg = "Days from today: " & DateDiff(DateInterval.Month, firstdate, seconddate)
MsgBox(msg)
It is better to use one of the parsing methods to validate the date information:
If DateTime.TryParse(first, firstdate) AndAlso _
DateTime.TryParse(second, seconddate) Then
msg = "Days from today: " & DateDiff(DateInterval.Day, firstdate, seconddate)
MessageBox.Show(msg)
Else
MessageBox.Show("Invalid dates entered.")
End If
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.