Crystal Reports.NET. The mere mention of the word “Crystal” makes most .NET developers shudder. I hope this post can help others with one of the many pitfalls I’ve experienced while working with Crystal Reports.NET.
This post covers Crystal Reports.NET v11 (the version that comes with Visual Studio.NET 2008), and ASP.NET. The Crystal Report Viewer control in this post is manually databound to a strongly typed DataSet in the code behind.
An issue I ran into recently was the problem of navigating through the Crystal Report pages with the viewer's built-in paging controls. When I would land on report page 2, and then attempted to sequence forward to report page 3, the report would go to page 1. This problem was caused by improper databinding of the Crystal Report Viewer control.
I first attempted to handle the server side paging event in the Crystal Report Viewer control, but to no surprise… that event is not exposed by the control. After some further investigation, I discovered that I needed to do my report binding through the Init event of the ASP.NET page.
My report needed to accept a custom parameter, supplied by a DropDownList in the page.
<asp:DropDownList ID="objDropDown" runat="server">
<asp:ListItem Value="0" Text="Parameter 0"></asp:ListItem>
<asp:ListItem Value="1" Text="Parameter 1"></asp:ListItem>
<asp:ListItem Value="2" Text="Parameter 2"></asp:ListItem>
</asp:DropDownList>
<asp:Button runat="server" Text="Run Report" />
<CR:CrystalReportViewer id="objCrystalReportsViewer" runat="server"/>
The problem I ran into was that I could not get the value from the DropDownList on PostBack because I could not access its ViewState. In an ASP.NET’s Init Event, ViewState is not accessible. Here’s how I solved my problem…
Instead of trying to get my report parameters from ViewState on PostBack, I used the Request object (brought back memories of the old classic ASP days). My OnInit code ended up looking like this:
Protected Overrides Sub OnInit(ByVal e As System.EventArgs)
MyBase.OnInit(e)
If (Request.Form.Keys.Count > 0) Then
Dim oFormValues As NameValueCollection = Request.Form
Dim iParameter As Integer = -1
If (oFormValues(oDropDown.UniqueID) IsNot Nothing) Then
Integer.TryParse(oFormValues(oDropDown.UniqueID), _
iParameter)
End If
Dim oReport As ReportDocument = New ReportDocument()
oReport.Load(Server.MapPath("MyReport.rpt"))
oReport.SetDataSource(New MyDataSet(iParameter))
objCrystalReportsViewer.ReportSource = oReport
objCrystalReportsViewer.DataBind()
End If
End Sub
Not only did that solution solve that problem, but it also gave me a great method for future development for manually databinding Crystal Reports.NET viewer controls to custom parameters.
Tags: asp.net,
crystal reports.net
Categories: Crystal Reports.NET
a8cf7996-b082-4b4b-838f-e20db44dd081|0|.0