Friday, February 24, 2012

Custom Code to Setup Parameter

I have a function that calcualte last_sunday date and return a datetime.

I have two Parameter in my report

start_date

end_date

and I put th following code in the report Parameer Properties

for Start date parameter

code.Last_Sunday

For End Date Parameter

code.Last_Sunday.adddays(6)

However when I ry to Preview I am getting this error

[rsReportParameterPropertyTypeMismatch] The property ‘DefaultValue’ of report parameter ‘Start_Date’ doesn't have the expected type.

[rsReportParameterPropertyTypeMismatch] The property ‘DefaultValue’ of report parameter ‘End_Date’ doesn't have the expected type.

Build complete -- 2 errors, 0 warnings

What I am doign Wrong both parameter are defined as datetime.

Thanks

Tanweer

Public Function Last_Sunday() As datetime

Dim Processed_date As DateTime
Dim start_date As DateTime
' Dim End_date As DateTime
Processed_date = DateTime.Today

'start_date = DateAdd(Processed_date - Weekday(Processed_date) + 1)
Select Case Processed_date.DayOfWeek.ToString
Case "Monday"
start_date = Processed_date.AddDays(-8)
Case "Tuesday"
start_date = Processed_date.AddDays(-9)
Case "Wednesday"
start_date = Processed_date.AddDays(-10)
Case "Thursday"
start_date = Processed_date.AddDays(-11)
Case "Friday"
start_date = Processed_date.AddDays(-12)
Case "Saturday"
start_date = Processed_date.AddDays(-13)
Case "Sunday"
start_date = Processed_date.AddDays(-14)
End Select
' End_date = format(start_date.AddDays(6),"mm/dd/yyyy")
console.writeline (start_date)
Return format(start_date,"MM/dd/yyyy")
End Function

You may want to try changing this line:

Return format(start_date,"MM/dd/yyyy")

to

CDate(Return format(start_date,"MM/dd/yyyy"))

The format function returns a string - not a DateTime...

Why are you formatting it here?

You mention a report...would it not be better to just pass the unformatted datetime to your report and handle the formatting in the report?

If you want create a re-usable function like this, it is better to return the raw data and handle formatting etc from where it is called. It is more generic and re-useable that way.

Steve

No comments:

Post a Comment