Programming Crystal Reports with ASP.NET 2.0 - Enhancing the report with run-time binding along with session handling
(Page 6 of 6 )
In all of the above sections, the CrystalReportViewer worked with CrystalReportSource. Now, let us dynamically add a report source to the CrystalReportViewer and bind it at runtime. This gives us the flexibility to use the same viewer for different reports (showing one at a time).
Add a new web page to the project, add the four buttons (First, Previous, Next and Last) and a CrystalReportViewer control. Modify the CrystalReportViewer control, so that it looks like the following:
<CR:CrystalReportViewer ID="CrystalReportViewer1" runat="server" AutoDataBind="True" ReuseParameterValuesOnRefresh="True" DisplayToolbar="False" EnableDatabaseLogonPrompt="False" EnableParameterPrompt="False" DisplayGroupTree="False"
Height="1064px" Width="928px" />
In the code-behind, add the following at the top:
Imports CrystalDecisions.Shared
Imports CrystalDecisions.CrystalReports.Engine
Add a new method as follows:
Private Sub BindReport()
If Session("Rep") Is Nothing Then
Dim ConnInfo As New ConnectionInfo
With ConnInfo
.ServerName = ".sqlexpress"
.DatabaseName = "Northwind"
.UserID = "sa"
.Password = "eXpress2005"
End With
Dim rep As New ReportDocument
rep.Load(Server.MapPath("SampleRpt01.rpt"))
Me.CrystalReportViewer1.ReportSource = rep
Dim RepTbls As Tables = rep.Database.Tables
For Each RepTbl As Table In RepTbls
Dim RepTblLogonInfo As TableLogOnInfo = RepTbl.LogOnInfo
RepTblLogonInfo.ConnectionInfo = ConnInfo
RepTbl.ApplyLogOnInfo(RepTblLogonInfo)
Next
Session("Rep") = rep
End If
Me.CrystalReportViewer1.ReportSource = Session("Rep")
Me.CrystalReportViewer1.DataBind()
End Sub
Add the following code to bind the report for every button click and also during the page load event:
Protected Sub btnFirst_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnFirst.Click
BindReport()
Me.CrystalReportViewer1.ShowFirstPage()
End Sub
Protected Sub btnLast_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnLast.Click
BindReport()
Me.CrystalReportViewer1.ShowLastPage()
End Sub
Protected Sub btnNext_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnNext.Click
BindReport()
Me.CrystalReportViewer1.ShowNextPage()
End Sub
Protected Sub btnPrev_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnPrev.Click
BindReport()
Me.CrystalReportViewer1.ShowPreviousPage()
End Sub
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Not IsPostBack Then
BindReport()
End If
End Sub
I hope you enjoyed the article and any suggestions, bugs, errors, enhancements etc. are highly appreciated at http://jagchat.spaces.live.com
| DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware. |
|
| · | | | · | | | · | | | · | | | · | | | · | | | · | | | · | | | · | | | · | | | · | | | · | | | · | | | · | | | · | | | · | | | · | | | · | | | · | | | | |
|