Link to home
Start Free TrialLog in
Avatar of Kerau
KerauFlag for United States of America

asked on

Add a day to the date range

I am working on Date Range Picker where a user can pick specific date and according to the date range (parameter passed) he will be displayed the report. Right now everything works fine untill a user chooses his start date and end date as the same day. eg start date 10/06/2008 and enddate 10/06/2008, the report then errors out.

I want to insert a logic below, if the SelectedBeginDate = SelectedEndDate. I want SelectedEndDate added by a day. Say a user chooses start date 10/06/2008 and enddate 10/06/2008 ..the end date should be converted as 10/07/2008..I have been trying this for couple of hours but in vain...How do i do this?..please advice
Private Function DateRangeIsValid(ByVal dateRange As DateRangePicker) As Boolean
        If (IsNothing(dateRange.SelectedBeginDate) And IsNothing(dateRange.SelectedEndDate)) Then
            Return True
        Else
            If (IsNothing(dateRange.SelectedBeginDate) And Not IsNothing(dateRange.SelectedEndDate)) Or _
               (Not IsNothing(dateRange.SelectedBeginDate) And IsNothing(dateRange.SelectedEndDate)) Then
                Return False
            End If
        End If
        Return dateRange.SelectedEndDate.Value.Date >= dateRange.SelectedBeginDate.Value.Date
    End Function

Open in new window

Avatar of Kevin Cross
Kevin Cross
Flag of United States of America image

Try something like this:
(since that is a custom component I don't have -- I couldn't test, but this should work for date objects)
Private Function DateRangeIsValid(ByVal dateRange As DateRangePicker) As Boolean
        If (IsNothing(dateRange.SelectedBeginDate) And IsNothing(dateRange.SelectedEndDate)) Then
            Return True
        ElseIf IsNothing(dateRange.SelectedBeginDate) Or IsNothing(dateRange.SelectedEndDate) Then '<<< changed
            Return False
        ElseIf dateRange.SelectedEndDate.Value.Date = dateRange.SelectedBeginDate.Value.Date Then '<<< added
            'May need to use this in above IF
            'DateDiff(DateInterval.Day, dateRange.SelectedEndDate.Value.Date, dateRange.SelectedBeginDate.Value.Date) = 0
            dateRange.SelectedEndDate.Value.Date = DateAdd(DateInterval.Day, 1, dateRange.SelectedBeginDate.Value.Date)
        End If
 
        Return dateRange.SelectedEndDate.Value.Date > dateRange.SelectedBeginDate.Value.Date '<<< changed
    End Function

Open in new window

use the AddDays function
dateRange.SelectedEndDate.Value.Date.AddDays(1)

Great suggestion, davrob60.

Again this works if these are ultimately Date objects.
Private Function DateRangeIsValid(ByVal dateRange As DateRangePicker) As Boolean
        If (IsNothing(dateRange.SelectedBeginDate) And IsNothing(dateRange.SelectedEndDate)) Then
            Return True
        ElseIf IsNothing(dateRange.SelectedBeginDate) Or IsNothing(dateRange.SelectedEndDate) Then '<<< changed
            Return False
        ElseIf dateRange.SelectedEndDate.Value.Date = dateRange.SelectedBeginDate.Value.Date Then '<<< added
            'May need to use this in above IF
            'DateDiff(DateInterval.Day, dateRange.SelectedEndDate.Value.Date, dateRange.SelectedBeginDate.Value.Date) = 0
            dateRange.SelectedEndDate.Value.Date.AddDays(1)
        End If
 
        Return dateRange.SelectedEndDate.Value.Date > dateRange.SelectedBeginDate.Value.Date '<<< changed
    End Function

Open in new window

Avatar of Kerau

ASKER

Thanks for a quick response. But it says "Expression is a value and therefore can't be the target of an assignment."
SOLUTION
Avatar of JohnP_Realini
JohnP_Realini
Flag of Argentina image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Forgot to mention something... forget the "daterange".. use two datepicker controls... one for starting date, one for ending date, and assign values to separate variables... that will make it much easier to handle
See my comments in code.  You may want this:

...
ElseIf DateDiff(DateInterval.Day, dateRange.SelectedEndDate.Value.Date, dateRange.SelectedBeginDate.Value.Date) = 0 Then
...
Avatar of Kerau

ASKER

doesnot work :(. The code works fine but the validation comes into picture when i enter same date range, which i don't want. So, we have to keep "=" sign without the quotes.

Return dateRange.SelectedEndDate.Value.Date >= dateRange.SelectedBeginDate.Value.Date

And if i keep the = sign it doesn't give me the report desired..

For your clarification i want to add a date to allow a user enter 10/07/2008 to 10/07/2008 to let him see the report from 12:00 AM to 11:59:59PM of the very day
 Private Function DateRangeIsValid(ByVal dateRange As DateRangePicker) As Boolean
        If (IsNothing(dateRange.SelectedBeginDate) And IsNothing(dateRange.SelectedEndDate)) Then
            Return True
        ElseIf IsNothing(dateRange.SelectedBeginDate) Or IsNothing(dateRange.SelectedEndDate) Then '<<< changed
            Return False
        ElseIf DateDiff(DateInterval.Day, dateRange.SelectedEndDate.Value.Date, dateRange.SelectedBeginDate.Value.Date) = 0 Then '<<< added
            'May need to use this in above IF
            'DateDiff(DateInterval.Day, dateRange.SelectedEndDate.Value.Date, dateRange.SelectedBeginDate.Value.Date) = 0
 
            dateRange.SelectedEndDate.Value.Date.AddDays(1)
        End If
 
        Return dateRange.SelectedEndDate.Value.Date > dateRange.SelectedBeginDate.Value.Date '<<< changed
    End Function

Open in new window

try this

dateRange.SelectedBeginDate.value.Date.AddDays(1).AddSeconds(-1)
sorry it was SelectedEndDate.
but you know what i meen. it add a day and substract a second so tou got 23:59:59 that very day
Avatar of Kerau

ASKER

Really appreciate your efforts, I selected my begin date 10/01/2008 and end date as 10/01/2008..Shouldn't the value shown in the screenshot would change to 10/02/2008. :(



hi.JPG
No because you subtracted a second to make it 10/1/2008 23:59:59
Avatar of Kerau

ASKER

okay then the issue would be with the report ... the report will not run...i am sorry if i sound same so how do i make it take param..if the user inputs start date 10/01/2008 to 10/01/2008...
this should be converted as10/01/2008 to 10/02/2008...
no it becaut it a function. so you should set it some were or call it directly, isnce it remove a second, you dont need the datediff thing
 

return dateRange.SelectedEndDate.Value.Date >= dateRange.SelectedEndDate.value.Date.AddDays(1).AddSeconds(-1)

Open in new window

something like this
Private Function DateRangeIsValid(ByVal dateRange As DateRangePicker) As Boolean
        If (IsNothing(dateRange.SelectedBeginDate) And IsNothing(dateRange.SelectedEndDate)) Then
            Return True
        ElseIf IsNothing(dateRange.SelectedBeginDate) Or IsNothing(dateRange.SelectedEndDate) Then '<<< changed
            Return False
        else
            Return dateRange.SelectedEndDate.Value.Date >= dateRange.SelectedEndDate.value.Date.AddDays(1).AddSeconds(-1)         End If
 
    End Function

Open in new window

Avatar of Kerau

ASKER

well it says ..invalid date range...wierd
my error, i missplaced the end and start


Private Function DateRangeIsValid(ByVal dateRange As DateRangePicker) As Boolean
        If (IsNothing(dateRange.SelectedBeginDate) And IsNothing(dateRange.SelectedEndDate)) Then
            Return True
        ElseIf IsNothing(dateRange.SelectedBeginDate) Or IsNothing(dateRange.SelectedEndDate) Then '<<< changed
            Return False
        else
            Return dateRange.SelectedEndDate.Value.Date.AddDays(1).AddSeconds(-1) >= dateRange.SelectedEndDate.value.Date
       End If
 
    End Function

Open in new window

Avatar of Kerau

ASKER

used the exact same code and no results. It doesn't add in a day to the selected date...when i started this, i thought it would have been much easier to achieve....:(
Avatar of Kerau

ASKER

this is the format i am looking for if the report param date is 10/01/2008 to 10/01/2008

i want to see the report of 10/1/2008 12:00:00AM To 10/2/2008 12:00:00AM
ok, it<s because your function just test the daye and return a boolean.
The Date Range Picker is not modified by the validation.
As you said, you got the error "Expression is a value and therefore can't be the target of an assignment." when you try to modify the dateRange s values...

you could try dateRange.SelectedEndDate = dateRange.SelectedEndDate.Value.Date.AddDays(1).AddSeconds(-1)
if it dont work,in the part where you made the select, you should try to add the dateRange.SelectedEndDate.Value.Date.AddDays(1).AddSeconds(-1)
For more help. please provide the part where you select the data
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Kerau

ASKER

So is this the place i need to make necessary changes



Protected Overridable Sub AddEndDateParameter(ByVal Report As CrystalDecisions.ReportAppServer.ClientDoc.ReportClientDocument)
        Report.DataDefController.ParameterFieldController.SetCurrentValues(String.Empty, Report.DataDefinition.ParameterFields(1).Name, GetSingleValueParameterFieldValues(HttpContext.Current.Request.Params("dteEnd")))
    End Sub

Open in new window

what is the data type of HttpContext.Current.Request.Params("dteEnd")?
cause you could try
ctype(HttpContext.Current.Request.Params("dteEnd"), date).AddDays(1).AddSeconds(-1)
Avatar of Kerau

ASKER

FYI : Public member 'AddDays' on type 'ValuesClass' not found.
Avatar of Kerau

ASKER

This might not be relevant at all but can we write something on button click :)
oops
ctype(GetSingleValueParameterFieldValues(HttpContext.Current.Request.Params("dteEnd"), date).AddDays(1).AddSeconds(-1).tostring()
?
Avatar of Kerau

ASKER

or on the validation page itself, something like this

  Public Overrides Sub Clear()
        'drpReportDates.SelectedBeginDate = DateTime.Now
        drpReportDates.SelectedEndDate = Add date here'DateTime.Now
       
    End Sub
here you go
drpReportDates.SelectedEndDate = DateTime.Now.Date.AddDays(1).AddSeconds(-1)
Avatar of Kerau

ASKER

okay tried you above method and says "Date is a type and can't be used as an expression.
Avatar of Kerau

ASKER

I have something like this

Protected Sub ValidateEndDate(ByVal sender As Object, ByVal e As ServerValidateEventArgs) Handles cvValidEndDate.ServerValidate
        e.IsValid = EndDateIsValid(drpReportDates)
    End Sub
but, what are you trying to do? why setting the drpReportDates.SelectedEndDate in the Sub Clear()???
you chould realy try to set it in the so it will be passed to you select function
Report.DataDefController.ParameterFieldController.SetCurrentValues(
 
It dont work this way???
Avatar of Kerau

ASKER

nope!!!

Here is the whole page ..if you r interested
Partial Class Secure
    Inherits BaseWebFormPage
 
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        rfvReport.Text = MessageRegistry.Message("FieldErrorIndicatorString").ToString + " " + _
                         MessageRegistry.Message("InputRequiredError").ToString
        csvRequiredEventDate.Text = MessageRegistry.Message("FieldErrorIndicatorString").ToString + " " + _
                                    MessageRegistry.Message("InputRequiredError").ToString
        cvValidDateRange.Text = MessageRegistry.Message("FieldErrorIndicatorString").ToString + " " + _
                                MessageRegistry.Message("InputInvalidDateRangeError")
        cvValidEndDate.Text = MessageRegistry.Message("FieldErrorIndicatorString").ToString + " " + _
                               MessageRegistry.Message("EndDateGreaterThanTodayError").ToString()
 
    End Sub
 
    Protected Sub btnClear_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnClear.Click
        Me.ddlReports.SelectedIndex = -1
        Me.drpReportDates.SelectedEndDate = Nothing
        Me.drpReportDates.SelectedBeginDate = Nothing
    End Sub
 
    Protected Sub btnSubmit_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSubmit.Click
        If Page.IsValid Then
            Dim urlString As String = ""
            Select Case Me.ddlReports.SelectedValue
                Case "abc.aspx"
                    urlString = String.Format("abc.aspx?dteBegin={0}&dteEnd={1}", Me.drpReportDates.SelectedBeginDate.Value.ToShortDateString, Me.drpReportDates.SelectedEndDate.Value.ToShortDateString)
                Case "bcd.aspx"
                    urlString = String.Format("bcd.aspx?dteBegin={0}&dteEnd={1}", Me.drpReportDates.SelectedBeginDate.Value.ToShortDateString, Me.drpReportDates.SelectedEndDate.Value.ToShortDateString)
            End Select
            Dim scriptString As New StringBuilder
            scriptString.Append("<script language=JavaScript>")
            scriptString.Append("window.open('" + urlString + "')")
            scriptString.Append("</script>")
            If (Not ClientScript.IsStartupScriptRegistered("Startup")) Then
                ClientScript.RegisterStartupScript(GetType(Page), "Startup", scriptString.ToString)
            End If
        End If
    End Sub
 
    Protected Sub ddlReports_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ddlReports.SelectedIndexChanged
        trDates.Visible = True
    End Sub
 
    Protected Sub ValidateEndDate(ByVal sender As Object, ByVal e As ServerValidateEventArgs) Handles cvValidEndDate.ServerValidate
        e.IsValid = EndDateIsValid(drpReportDates)
    End Sub
 
    Protected Overridable Function EndDateIsValid(ByVal dateRange As DateRangePicker) As Boolean
        If Not IsNothing(dateRange.SelectedEndDate) And Not IsNothing(dateRange.SelectedBeginDate) Then
            If dateRange.SelectedEndDate.Value.Date > DateTime.Now.Date Then
                Return False
            End If
        End If
 
        Return True
    End Function
 
    Protected Sub RequireEventDate(ByVal sender As Object, ByVal e As ServerValidateEventArgs) Handles csvRequiredEventDate.ServerValidate
        e.IsValid = DateRangeIsNotNull(drpReportDates)
    End Sub
 
    Protected Overridable Function DateRangeIsNotNull(ByVal dateRange As DateRangePicker) As Boolean
        Return _
         Not IsNothing(dateRange.SelectedBeginDate) AndAlso _
         Not IsNothing(dateRange.SelectedEndDate)
    End Function
 
    Protected Sub ValidateDateRange(ByVal sender As Object, ByVal e As ServerValidateEventArgs) Handles cvValidDateRange.ServerValidate
        e.IsValid = DateRangeIsValid(drpReportDates)
    End Sub
 
    Private Function DateRangeIsValid(ByVal dateRange As DateRangePicker) As Boolean
        If (IsNothing(dateRange.SelectedBeginDate) And IsNothing(dateRange.SelectedEndDate)) Then
            Return True
        Else
            If (IsNothing(dateRange.SelectedBeginDate) And Not IsNothing(dateRange.SelectedEndDate)) Or _
               (Not IsNothing(dateRange.SelectedBeginDate) And IsNothing(dateRange.SelectedEndDate)) Then
                Return False
            End If
        End If
        Return dateRange.SelectedEndDate.Value.Date >= dateRange.SelectedBeginDate.Value.Date
    End Function
 
End Class

Open in new window

Avatar of Kerau

ASKER

can i just add it here

urlString = String.Format("bcd.aspx?dteBegin={0}&dteEnd={1}", Me.drpReportDates.SelectedBeginDate.Value.ToShortDateString, Me.drpReportDates.SelectedEndDate.Value.ToShortDateString)
Avatar of Kerau

ASKER

So when i do something like the following i get the following error..Date is a type and can't be used as an expression...Please suggest





 Protected Overridable Sub AddEndDateParameter(ByVal Report As CrystalDecisions.ReportAppServer.ClientDoc.ReportClientDocument)
        Report.DataDefController.ParameterFieldController.SetCurrentValues(String.Empty, Report.DataDefinition.ParameterFields(1).Name, ctype(GetSingleValueParameterFieldValues(HttpContext.Current.Request.Params("dteEnd"),date).AddDays(1).AddSeconds(-1).tostring()
    End Sub

Open in new window

well we will try to do it on the btnSubmit_Click
 urlString = String.Format("bcd.aspx?dteBegin={0}&dteEnd={1}", Me.drpReportDates.SelectedBeginDate.Value.ToShortDateString, Me.drpReportDates.SelectedEndDate.Value.Date.AddDays(1).AddSeconds(-1).ToShortDateString)

Avatar of Kerau

ASKER

Pasted exact same code but in vain ...doesn't add a day to  the end date :(...what can i possibly do to make this work...
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Kerau

ASKER

Davrob60 and all experts who answered this, I really have no clue how you do this, without even having much information..but you are a saviour. This works and works like a charm.,,,,,,,,yhoooooo!!!!I am highly appreciate of you being taking out much of your precious time and trying to answer my question (at times stupid :)). Thanks a lot .. Thanks to mwvisa, JohnP_Realini

Davrob you deserve more than 350 points ..so please post a link of this question to one of my other post https://www.experts-exchange.com/questions/23791451/Issues-with-Date-Range-Picker.html so that i can grant you points as this answers my other question as well.