Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load Dim rpt As New ReportDocument rpt.Load(Server.MapPath("CrystalReport1.rpt")) rpt.SetDatabaseLogon("sa1", "sa1") Me.CrystalReportViewer1.ReportSource = rpt End Sub Protected Sub btnReload_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnReload.Click ' Create filter expression, based on the filter input fields. ' If an input field is left blank, it isn't used in the filter expression. Dim filterString As String = Nothing AddFilter(filterString, "EmpNo", TextBox1, "'") AddFilter(filterString, "DeptmtID", TextBox2, "'") Dim rpt As New ReportDocument CrystalReportViewer1.ReportSource = rpt End Sub Private Sub AddFilter(ByRef filterString As String, ByVal databaseFieldName As String, ByVal fieldInput As TextBox, ByVal valueQuoteChar As String) Dim fieldValue As String = fieldInput.Text.Trim() ' If input field left blank, don't filter on it. If fieldValue = "" Then Return End If ' Escape for quotes if we're going to quote the value Dim escapedFieldValue As String = fieldValue If valueQuoteChar = "'" Then escapedFieldValue = fieldValue.Replace("'", "''") End If ' Add boolean clause to the complete filter expression ' This method always uses operator = Dim booleanSqlExpression As String = "{" & databaseFieldName & "} = " & valueQuoteChar & escapedFieldValue & valueQuoteChar ' separate boolean clauses with AND operator If Not String.IsNullOrEmpty(filterString) Then filterString += " AND " End If filterString += booleanSqlExpression End Sub Private Function CreateCrystalReportDocument(ByVal filterString As String) As ReportDocument Dim rpt As New ReportDocument() Dim report As String = Server.MapPath("CrystalReport1.rpt") rpt.Load(report) ' ---- Assign connection information for each table in the database 'Build connection info Dim crConnectionInfo As New ConnectionInfo crConnectionInfo.ServerName = "10.1.2.215\BOSS_NET_DB" crConnectionInfo.DatabaseName = "eExitPass" crConnectionInfo.UserID = "sa1" crConnectionInfo.Password = "sa1" ' Assign to all tables used by the report Dim tables As Tables = rpt.Database.Tables For Each table As CrystalDecisions.CrystalReports.Engine.Table In tables Dim crtableLogoninfo As TableLogOnInfo = table.LogOnInfo crtableLogoninfo.ConnectionInfo = crConnectionInfo table.ApplyLogOnInfo(crtableLogoninfo) Next rpt.DataDefinition.RecordSelectionFormula = filterString Return rpt End Function
var
This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)