Sunday, October 18, 2009

Passing Parameters to A Crystal Report from Code Behind ~ ASP.NET

In a resent project i had to pass parameters to a crystal report depending on a user selection of a dropdownlist, this is how i achieved it in VB.NET.

The server name and DBname are stored in the web.config

Protected Sub DropDownList1_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles DropDownList1.SelectedIndexChanged
ReportLocation = System.IO.Path.Combine(Server.MapPath("~"), "Admin\Report1.rpt")
ServerName = ConfigurationManager.AppSettings("ServerName")
DBName = ConfigurationManager.AppSettings("DBName")
End Sub

Sub SetSource(ByVal params As String)
Dim cryRpt As New ReportDocument()
Dim crtableLogoninfos As New TableLogOnInfos()
Dim crtableLogoninfo As New TableLogOnInfo()
Dim crConnectionInfo As New ConnectionInfo()
Dim CrTables As Tables
crConnectionInfo.ServerName = ServerName
crConnectionInfo.DatabaseName = DBName
crConnectionInfo.IntegratedSecurity = True
CrTables = cryRpt.Database.Tables
For Each CrTable As CrystalDecisions.CrystalReports.Engine.Table In CrTables
crtableLogoninfo = CrTable.LogOnInfo
crtableLogoninfo.ConnectionInfo = crConnectionInfo

Dim parameters As New ParameterFields
Dim param As ParameterField
Dim paramvalue As ParameterDiscreteValue
CrystalReportViewer1.ReportSource = cryRpt

param = New ParameterField
paramvalue = New ParameterDiscreteValue
param.Name = "Param1"
paramvalue.Value = params
param.HasCurrentValue = True
CrystalReportViewer1.ParameterFieldInfo = parameters
End Sub

Avoid Navigating to previuos pages in Firefox ~ ASP.NET

I got a problem in where a user after logging out can navigate back to the pages that he visited previously without again logging in. The problem comes because the pages are not retrieved from the server but stored in the browser cache.This is how i avoided it.

I included this code in the page load of each page

Response.Buffer = True
Response.ExpiresAbsolute = DateTime.Now.AddDays(-1D)
Response.Expires = 0
Response.AppendHeader("Pragma", "no-cache")
Response.CacheControl = "no-cache"
If Not Page.User.Identity.IsAuthenticated Then
Response.Redirect("~/login.aspx", False)
End If