Introduction
The main purpose of this document is to display the report without any error. I was bugged by the "Logon Failed Error" for several days, and now I finally have a code that displays report without any error. The code also Exports the report into .pdf, .xls, .rtf and .doc formats. It also prints the report directly to the printer.
Using the code
Unzip the crCodes.Zip and then run the crCode.vbproj project file.
--- OR ----
Just insert the Webform1.aspx file into your existing project, then copy the crystalreport2.rpt file into your project, and start using the code.
The entire source code of Webform1.aspx.vb is as follows. Simply design the form as shown in the image and place the Crystal Report Viewer control, and leave the name of controls to default:
Imports CrystalDecisions.Shared
Imports System.IO
Public Class WebForm1
Inherits System.Web.UI.Page
Dim crReportDocument As CrystalReport2 = New CrystalReport2
Protected WithEvents DropDownList1 As System.Web.UI.WebControls.DropDownList
Protected WithEvents Button1 As System.Web.UI.WebControls.Button
Protected WithEvents Label1 As System.Web.UI.WebControls.Label
Protected WithEvents CrystalReportViewer1 As _
CrystalDecisions.Web.CrystalReportViewer
Protected WithEvents Button2 As System.Web.UI.WebControls.Button
#Region " Web Form Designer Generated Code "
<System.Diagnostics.DebuggerStepThrough()>
Private Sub InitializeComponent()
End Sub
Private designerPlaceholderDeclaration As System.Object
Private Sub Page_Init(ByVal sender As System.Object,_
ByVal e As System.EventArgs) Handles MyBase.Init
InitializeComponent()
crReportDocument.SetDatabaseLogon("username",_
"password", "sql-server", "database")
"password")
CrystalReportViewer1.DisplayGroupTree = False
CrystalReportViewer1.ReportSource = crReportDocument
crReportDocument.SetParameterValue("city", "Mumbai")
With DropDownList1.Items
.Add("Rich Text (RTF)")
.Add("Portable Document (PDF)")
.Add("MS Word (DOC)")
.Add("MS Excel (XLS)")
End With
End Sub
#End Region
Private Sub Page_Load(ByVal sender As System.Object,_
ByVal e As System.EventArgs) Handles MyBase.Load
End Sub
Sub ExportReport()
Dim oStream As New MemoryStream
Select Case DropDownList1.SelectedItem.Text
Case "Rich Text (RTF)"
oStream = crReportDocument.ExportToStream(_
CrystalDecisions.Shared.ExportFormatType.WordForWindows)
Response.Clear()
Response.Buffer = True
Response.ContentType = "application/rtf"
Case "Portable Document (PDF)"
oStream = crReportDocument.ExportToStream(_
CrystalDecisions.Shared.ExportFormatType.PortableDocFormat)
Response.Clear()
Response.Buffer = True
Response.ContentType = "application/pdf"
Case "MS Word (DOC)"
oStream = crReportDocument.ExportToStream(_
CrystalDecisions.Shared.ExportFormatType.WordForWindows)
Response.Clear()
Response.Buffer = True
Response.ContentType = "application/doc"
Case "MS Excel (XLS)"
oStream = crReportDocument.ExportToStream(_
CrystalDecisions.Shared.ExportFormatType.Excel)
Response.Clear()
Response.Buffer = True
Response.ContentType = "application/vnd.ms-excel"
End Select
Try
Response.BinaryWrite(oStream.ToArray())
Response.End()
Catch err As Exception
Response.Write("< BR >")
Response.Write(err.Message.ToString)
End Try
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles Button2.Click
ExportReport()
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles Button1.Click
crReportDocument.SetDatabaseLogon("USER", _
"PASSWORD", "SQL-SERVER", "DATABASE")
crReportDocument.PrintToPrinter(1, False, 0, 0)
End Sub
End Class
Why is the "Logon Failed Error" generated
The "Logon Failed" error is generated basically because, when the report is being displayed it tries to log on to the database server. Even though you have selected the server while designing, the report still needs the server name while displaying or exporting or printing.
The code line that removes the error is:
crReportDocument.SetDatabaseLogon("USER", "PASSWORD", "SQL-SERVER", "DATABASE")
Or can also be used as:
crReportDocument.SetDatabaseLogon("USER", "PASSWORD")