如果没有选定日期,显示接下来30天的结果?

时间:2014-12-01 16:09:17

标签: sql asp.net sql-server vb.net

我们向用户提供搜索特定日期的记录或按日期范围搜索的选项。

但是,如果用户选择的日期不可用,而不是显示未找到用户选择的消息,则显示接下来30天的记录。

到目前为止,我的查询显示的是日期早于当天的日期记录。

这是错误的。

任何想法下面的代码有什么问题?

If ViewSelect = "Range" Then
    sb.Append(If(startedWhere, andors, " WHERE "))
    sb.Append("(CONVERT(VARCHAR(10), facilityAvailabilityDate, 101) >= @FromDate And CONVERT(VARCHAR(10), facilityAvailabilityDate, 101) <= @EndDate)")
    cmd.Parameters.AddWithValue("@FromDate", txtFromDate.Text)
    cmd.Parameters.AddWithValue("@EndDate", txtToDate.Text)
    startedWhere = True

ElseIf ViewSelect = "Specific" Then
    sb.Append(If(startedWhere, andors, " WHERE "))
    sb.Append("(CONVERT(VARCHAR(10), facilityAvailabilityDate, 101) = @SpecificDate)")
    cmd.Parameters.AddWithValue("@SpecificDate", txtSpecificDate.Text)
    startedWhere = True
End If

cmd.CommandText = sb.ToString()
'Response.Write(sb)
'Response.End()
' Dim cmd As New SqlCommand(strQuery) 
Dim dt As DataTable = GetData(cmd)

'No records found, so display records for next 30 days. 
If dt.Rows.Count = 0 Then

    cmd.Parameters.Clear()
    cmd.Parameters.AddWithValue("@FromDate", Date.Today.AddMonths(-1))
    cmd.Parameters.AddWithValue("@EndDate", Date.Today.ToString("MM/dd/yyyy"))

    cmd.CommandText = "Select siteID,siteName,bldgName,Capacity,AvailableFacilities,facilityAvailabilityDate,Status " & _
",amenities,[Rental Fees] RentalFees,[Extra Hour Fees] ExtraHourFees,[Deposit Fees] DepositFees,[Cancellation Fees] CancelFees,[Key Deposit Fees] KeyDepFees, Duration " & _
" FROM ParksNRecsData" & _
" where facilityAvailabilityDate Between @FromDate And @EndDate"
    dt = GetData(cmd)
End If

GridView1.DataSource = dt
GridView1.DataBind()

SQL Server是首选的数据库。

提前致谢

1 个答案:

答案 0 :(得分:1)

这段代码是选择上个月的日期,而不是下个月:

If dt.Rows.Count = 0 Then

    cmd.Parameters.Clear()
    cmd.Parameters.AddWithValue("@FromDate", Date.Today.AddMonths(-1))
    cmd.Parameters.AddWithValue("@EndDate", Date.Today.ToString("MM/dd/yyyy"))

将其更改为以下内容应达到您的目的:

If dt.Rows.Count = 0 Then

    cmd.Parameters.Clear()
    cmd.Parameters.AddWithValue("@FromDate", Date.Today)
    cmd.Parameters.AddWithValue("@EndDate", Date.Today.AddMonths(1))