我在下面的代码中为一个用户获得了一个null错误

时间:2017-10-25 11:15:19

标签: sql-server vb.net

以下功能试图从包含3个日期的表中获取最早的日期,每个类型的用户,护理,销售和经理一个。这是通过首先在日记日期中找到第一个日期来建立日记系统。它适用于某些用户,但在一种情况下,值不会返回,并且它为null。

私人功能GetEarliestDate()作为日期

    Dim strSQL As String
    Dim cmd As New SqlCommand
    Dim dDate As Date

    Try
        strSQL = "Select dT.RecordID, MIN(dT.inDate) As [EarliestDate]
FROM (
    Select RecordID, SentDate As [inDate]
        From tblOrderDetails Where Flagged = 0 AND SalesID = '" & gUserID & "'

         UNION ALL

     Select RecordID, DiaryDate AS [inDate]
     From tblOrderDetails
     Where Flagged=0 And ManID ='" & gUserID & "'

         UNION ALL

     Select RecordID,  CareDate As [inDate]
     From tblOrderDetails 
     Where Flagged = 0 And CareID ='" & gUserID & "'
     ) As dT Group By RecordID"

        cmd.CommandText = strSQL
        cmd.Connection = CnMaster
        cmd.CommandType = CommandType.Text

        Dim RecordCount As Integer
        RecordCount = 0
        dDate = DateTime.MaxValue
        Using reader As SqlDataReader = cmd.ExecuteReader()
            While (reader.Read())
                RecordCount += 1
                Dim dt As DateTime = reader.GetDateTime(1)
                If dt < dDate Then
                    dDate = dt

                End If
            End While
            If dDate = DateTime.MaxValue Then
                dDate = DateTime.MinValue
            End If
        End Using


        cmd.Dispose()

        Return dDate


    Catch ex As Exception

        Error_Handler(ex, "GetEarliestDate", "Unable to Get The Earliest Date!")

    End Try

1 个答案:

答案 0 :(得分:0)

将所有3个查询添加到以下位置:

where SentDate is not null ...
where DiaryDate is not null ...
where CareDate  is not null ...