Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/vb.net/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Asp.net 比较两个日期并有条件地设置格式_Asp.net_Vb.net - Fatal编程技术网

Asp.net 比较两个日期并有条件地设置格式

Asp.net 比较两个日期并有条件地设置格式,asp.net,vb.net,Asp.net,Vb.net,我在aspx页面中有一个GridView,我正在比较两个日期,并根据结果格式化它们。这似乎可以正常工作,但有些情况下,我有一个空日期或一个空白单元格,这会导致页面崩溃,visual studio会返回此错误 mscorlib.dll中发生“System.FormatException”类型的异常 但未在用户代码中处理。附加信息:字符串为 无法识别为有效的日期时间 这就是我得到错误的地方: Dim startdate As DateTime = Convert.ToDateTime(e.Row.C

我在aspx页面中有一个GridView,我正在比较两个日期,并根据结果格式化它们。这似乎可以正常工作,但有些情况下,我有一个空日期或一个空白单元格,这会导致页面崩溃,visual studio会返回此错误

mscorlib.dll中发生“System.FormatException”类型的异常 但未在用户代码中处理。附加信息:字符串为 无法识别为有效的日期时间

这就是我得到错误的地方:

Dim startdate As DateTime = Convert.ToDateTime(e.Row.Cells(7).Text)
以下是我的VB.Net代码:

    Protected Sub GridView1_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GridView1.RowDataBound
    If e.Row.RowType = DataControlRowType.DataRow Then

        Dim duedate As DateTime = Convert.ToDateTime(e.Row.Cells(11).Text)
        Dim startdate As DateTime = Convert.ToDateTime(e.Row.Cells(7).Text) '*THIS IS WHERE I HAVE NULL OR BLANK DATES
        Dim today As DateTime = DateTime.Now


        If startdate > duedate Then
            e.Row.Cells(2).BackColor = System.Drawing.Color.Red
            e.Row.Cells(2).CssClass = "gvhlrow"
        ElseIf startdate.AddDays(7) < duedate Then
            e.Row.Cells(2).BackColor = System.Drawing.Color.Yellow
            e.Row.Cells(2).CssClass = "gvhlrow"
        ElseIf startdate.AddDays(14) < duedate Then
            e.Row.Cells(2).BackColor = System.Drawing.Color.Lime
            e.Row.Cells(2).CssClass = "gvhlrow"
        ElseIf startdate < duedate Then
            e.Row.Cells(2).BackColor = System.Drawing.Color.Orange
            e.Row.Cells(2).CssClass = "gvhlrow"
        End If

    End If
End Sub
Protected Sub-GridView1_RowDataBound(ByVal sender作为对象,ByVal e作为System.Web.UI.WebControls.GridViewRowEventArgs)处理GridView1.RowDataBound
如果e.Row.RowType=DataControlRowType.DataRow,则
Dim duedate As DateTime=Convert.ToDateTime(如Row.Cells(11).Text)
Dim startdate As DateTime=Convert.ToDateTime(例如,Row.Cells(7.Text))*这是我有空或空日期的地方
今天变暗为DateTime=DateTime.Now
如果startdate>duedate,则
e、 行.单元格(2).背景色=System.Drawing.Color.Red
e、 行。单元格(2)。CssClass=“gvhlrow”
其他开始日期。添加日期(7)<到期日期
e、 行.单元格(2).背景色=System.Drawing.Color.Yellow
e、 行。单元格(2)。CssClass=“gvhlrow”
其他开始日期。添加日期(14)<到期日期
e、 行.单元格(2).背景色=System.Drawing.Color.Lame
e、 行。单元格(2)。CssClass=“gvhlrow”
如果开始日期<则结束日期
e、 行.单元格(2).背景色=System.Drawing.Color.Orange
e、 行。单元格(2)。CssClass=“gvhlrow”
如果结束
如果结束
端接头

您必须使用以下方法检查输入:

Dim startdate As DateTime
If Not DateTime.TryParse(e.Row.Cells(7).Text, startdate) Then
    ' Display an error, set a meaningful default value or just return.
End If
可能您希望在无效日期的情况下向用户显示错误消息,可能您希望忽略它并使用一些默认值,或者如果没有有意义的默认值,那么您可能只希望返回而不进行任何计算

我建议以类似的方式检查
duedate
是否存在无效值