Friday, March 9, 2012

How to set ReportViewer DataSource to a Custom Collection in Another Project

I have a web site that uses a business logic layer (BLL) and a data access layer (DAL). The BLL is in a separate project. The DAL is in a separate project. The web site has a reference to the BLL project and the BLLproject has a reference to the DAL project. Just to learn how to use the reportviewer control I used the server explorer to create a web datasource based directly on a stored procedure and everything works just fine. However, now I want to reportviewer to be based on a method in my BLL project. My problem is that the web data source window only shows those methods that have not paramenters. I want it to show CovAdm.BLL.InitialInspectionMgr.LoadDataTableForParcelID which has one parameter for ParcelID and returns a datatable. How do I get this method to show up under the CovAdm.BLL item in the web data sources window?

I found what I think is a simpler and better solution, especially because the reportviewer control seems be a long way from WYSIWYG. My solution doesn't use a report viewer control at all. Here's what I did.

My web page has a formview control with a "Report" button. The formview control itemcommand event checks for which button has been clicked and executes the following code behind for the Report button:
' get the data
Dim bll As New CovAdm.BLL.InitialInspectionMgr
Dim col As CovAdm.BLL.InitialInspectionCollection
Dim lngParcelID As Long = MySession.ParcelSearchResultDetailParcelID(Session)
col = bll.LoadForParcelID(lngParcelID, "ParcelID", "ASC")
' use the default file name for cases that have no file name
' use a physical path because local reports don't work with virtual paths
For Each ii As CovAdm.BLL.InitialInspection In col
If String.IsNullOrEmpty(ii.FQPhotoFileName) Then
Dim str As String = AppConfigurationMgr.PhotoNotAvailableRelativeFile()
str = Server.MapPath(str)
ii.FQPhotoFileName = str
End If
Next
' set up the report
Dim lr As New Microsoft.Reporting.WebForms.LocalReport
lr.ReportPath = Server.MapPath("~/Home/Report/rptInitialInspection.rdlc")
lr.EnableExternalImages = True
lr.DataSources.Clear()
' make sure we use the correct data source name
Dim rds As New Microsoft.Reporting.WebForms.ReportDataSource("dsrptInitialInspection_dtrptInitialInspection", col)
lr.DataSources.Add(rds)
' show the file dialog for opening or saving the pdf verion of the report
Utility.ShowLocalReport(Response, lr)

The code from my Utility class is shown below:
Public Shared Sub ShowLocalReport _
( _
ByVal vResponse As System.Web.HttpResponse, _
ByVal vlr As Microsoft.Reporting.WebForms.LocalReport _
)
'
' Aruments
' ========
' vResponse - HttpResponse object from calling page
' vlr - LocalReport object from calling page
'
' the procedure is currently limited to PDF format
Dim strFormat As String = "PDF"
Dim strMimeType As String = Nothing
Dim strEncoding As String = Nothing
Dim strFileExt As String = Nothing
' the default filename for saving will be the name if the rdlc file
Dim strFileName As String = System.IO.Path.GetFileNameWithoutExtension(vlr.ReportPath)
Dim astrStream() As String
Dim aWarning() As Microsoft.Reporting.WebForms.Warning
Dim aBytes As Byte()
' render the report
aBytes = vlr.Render(strFormat, Nothing, strMimeType, strEncoding, strFileExt, astrStream, aWarning)
' show the open / save dialog
vResponse.Clear()
vResponse.ContentType = "application/" & strFormat
vResponse.AddHeader("content-disposition", "attachment; filename=" & strFileName & "." & strFileExt)
vResponse.BinaryWrite(aBytes)
vResponse.End()
End Sub

No comments:

Post a Comment