检查字符串值是否在 VB.net 中有效
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22498118/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Check String Value If Valid Date in VB.net
提问by Denmark
My problem is when i receive a string and that is not in date format. And put the error in textbox.
我的问题是当我收到一个不是日期格式的字符串时。并将错误放入文本框中。
Example:
例子:
Public Sub get_error(ByVal sheet_name As String, ByVal column_name As String, ByVal row_count As String, ByVal row_value As String)
Dim dt_row_service_provider_code As String = row_value
If dt_row_service_provider_code Is Nothing Or dt_row_service_provider_code = "" Then
dt_row_service_provider_code = "Empty Value"
End If
If dt_row_service_provider_code.Length > 50 Then
dt_row_service_provider_code = "Characters is greater than 50"
End If
txtError.Text += "sheet: " & sheet_name & " | column: " & column_name & " | row: " & (row_count + 2) & " | row value is: '" & dt_row_service_provider_code & "'" & Environment.NewLine
End Sub
'dr(2).Tostring contains word "test"
For Each dr As DataRow In dt.Rows
Dim holiday_code As String = dr(0).ToString
Dim holiday_desc As String = dr(1).ToString
Dim holiday_date As String = dr(2).ToString
If holiday_date Is Nothing Or holiday_date = "" Then
get_error(dt.TableName, dt.Columns(2).ToString, dt_row.ToString, "Empty Value")
End If
If isDate(holiday_date) = False Then
'show error message in textbox
get_error(dt.TableName, dt.Columns(2).ToString, dt_row.ToString, "Value is not a valid Date")
End If
Next
This is just example of mine because my program is importing excel to database and i'm just taking some scenario that if the user will accidentally add a word in date format in excel.now how can i put the error in textbox?. i don't want to just show an exception but i want to put the error in textbox
这只是我的一个例子,因为我的程序正在将 excel 导入数据库,我只是在考虑一些场景,如果用户不小心在 excel 中添加了日期格式的单词。现在我该如何将错误放入文本框中?。我不想只显示异常,但我想将错误放入文本框中
回答by Codemunkeee
Updated my answer.
更新了我的答案。
Public Sub get_error(ByVal sheet_name As String, ByVal column_name As String, ByVal row_count As Integer, ByVal row_value As String)
Dim dt_row_service_provider_code As String = row_value
txtError.Text += "sheet: " & sheet_name & " | column: " & column_name & " | row: " & (row_count + 2) & " | row value is: '" & dt_row_service_provider_code & "'" & Environment.NewLine
End Sub
For Each dr As DataRow In dt.Rows
Dim holiday_code As String = dr(0).ToString
Dim holiday_desc As String = dr(1).ToString
Dim holiday_date As String = dr(2).ToString
Dim newDate As DateTime
Try
newDate = DateTime.Parse(holiday_date)
Catch ex As Exception
If holiday_date = "" Or holiday_date Is Nothing Then
get_error(dt.TableName, dt.Columns(2).ToString, dt.Rows.IndexOf(dr), "Empty Value")
Else
get_error(dt.TableName, dt.Columns(2).ToString, dt.Rows.IndexOf(dr), "Value is not a valid Date")
End If
End Try
Next

