我在网络表单上收到以下错误消息:
Cast from string "" to type 'Date' is not valid.
我假设这是因为这条线(但我的假设可能不正确):
command.Parameters.Add(New SqlParameter("col3", SqlDbType.DateTime, 8)).Value = some_date_parameter
这是一个功能:
Function ProcessAction(ByVal col1 As Integer, ByVal col2 As String, ByVal col3 As Date, ByVal col4 As Integer, ByVal col5 As String) as something
'...
command.Parameters.Add(New SqlParameter("@col1", SqlDbType.Int, 4)).Value = col1
command.Parameters.Add(New SqlParameter("@col2", SqlDbType.VarChar, 255)).Value = col2
command.Parameters.Add(New SqlParameter("@col3", SqlDbType.DateTime, 8)).Value = col3
command.Parameters.Add(New SqlParameter("@col4", SqlDbType.Int, 4)).Value = col4
command.Parameters.Add(New SqlParameter("@col5", SqlDbType.VarChar, 255)).Value = col5
'...
Return something
End Function
通过按钮调用:
Sub ButtonClick(ByVal Source As Object, ByVal E As EventArgs)
'...
structRecord = ProcessAction(TextBox1.Text, TextBox2.Text, TextBox3.Text, TextBox4.Text, TextBox5.Text)
'...
End Sub
structRecord
的位置:
Public Structure structRecord
Public col1 As Integer
Public col2 As String
Public col3 As Date
Public col4 As Integer
Public col5 As String
End Structure
实际错误在运行时显示在此行上,位于ButtonClick
:
structRecord = ProcessAction(TextBox1.Text, TextBox2.Text, TextBox3.Text, TextBox4.Text, TextBox5.Text)
如何纠正这个问题?我基本上希望允许用户继续使用其余的webapp,即使TextBox3.Text
为空。
答案 0 :(得分:0)
通常,您将测试输入以确保它们在有效或可接受的范围内。
如果是日期,您需要验证该值是否会转换为日期,如果是,请将其作为参数传递。如果它不是有效日期,那么您可以将空值传递给查询。
查询应足够智能以接受日期或null;但是,如果没有这个问题,我无法帮助你。
答案 1 :(得分:0)
您可以尝试使用dateTime.TryParse方法验证传入的字符串是否为有效的日期/时间值。
http://msdn.microsoft.com/en-us/library/system.datetime.tryparse.aspx