Forum Moderators: open
I tried using the code in a book but can't achieve what the book stated. below is part of the code. Following the book, I drag the reportdocument icon from the toolbox to my window form and and name it rdViewer. But when i run the program no compile error but the program go from 'Try' to 'MessageBox.Show(excp.Message)'. in other word always go into exception. What went wrong here? i tried to troubleshoot and realize that the book has a step that 'Choose a ReportDocument' dialog box that will link to the report whereby i don't know how to set. in addition i found that the soft code they provide the ReportSource in the CRViewer is also different from mine. Their is 'rdHowTo10_4 [VB.Net___Chapter_10.rptHowTo10_1]' rdHowTo10_4 is the reportdocument they created and rptHowTo10_1 is the Crystal report that is in the solution explorer.
Private Sub btnExport_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnExport.Click
Dim strExt As String
Try
With Me.rdViewer.ExportOptions
Select Case lstExportType.SelectedItem
Case "Excel"
.ExportFormatType = _
CrystalDecisions.[Shared].ExportFormatType.Excel
strExt = ".xls"
Case "Word Document"
.ExportFormatType = _
CrystalDecisions.[Shared].ExportFormatType.WordForWindows
strExt = ".Doc"
End Select
.ExportDestinationType = _
CrystalDecisions.[Shared].ExportDestinationType.DiskFile
Dim ddo As New CrystalDecisions.Shared.DiskFileDestinationOptions()
ddo.DiskFileName = "c:\" & "Test" & strExt
.DestinationOptions = ddo
End With
Me.rdViewer.Export()
MessageBox.Show("Report Exported!")
Catch excp As Exception
MessageBox.Show(excp.Message)
End Try
End Sub
Dim oRpt As New ReportDocument
<WebMethod()> Public Function ExportReport(ByVal ReportFileName As String, _
ByVal ExportFileName As String, _
ByVal ReportTitle As String, _
ByVal ReportFormula As String, _
ByVal ReportLogon As String) As String
Dim sPath As String = Context.Request.PhysicalApplicationPath '"d:\inetpub\wwwroot\test"
Try
Dim arrLogonInfo As String() = Split(ReportLogon, ";")
'Loads report from repository location
oRpt.Load(sPath + "reports\" + ReportFileName)
'single table logon ; delimited string = Server;Database;UserID;Password
Dim exportOpts As New ExportOptions
Dim diskOpts As New DiskFileDestinationOptions
Dim crLogonInfo As CrystalDecisions.Shared.TableLogOnInfo
crLogonInfo = oRpt.Database.Tables(0).LogOnInfo
crLogonInfo.ConnectionInfo.ServerName = arrLogonInfo(0)
crLogonInfo.ConnectionInfo.DatabaseName = arrLogonInfo(1)
crLogonInfo.ConnectionInfo.UserID = arrLogonInfo(2)
crLogonInfo.ConnectionInfo.Password = arrLogonInfo(3)
oRpt.Database.Tables(0).ApplyLogOnInfo(crLogonInfo)
exportOpts = oRpt.ExportOptions
exportOpts.ExportDestinationType = ExportDestinationType.DiskFile
diskOpts.DiskFileName = sPath + "temp\" + ExportFileName
exportOpts.DestinationOptions = diskOpts
Dim sExt As String = UCase(Right(ExportFileName, 3))
Select Case sExt
Case Is = "PDF"
exportOpts.ExportFormatType = ExportFormatType.PortableDocFormat
Case Is = "DOC"
exportOpts.ExportFormatType = ExportFormatType.WordForWindows
Case Is = "XLS"
exportOpts.ExportFormatType = ExportFormatType.Excel
Case Else
exportOpts.ExportFormatType = ExportFormatType.PortableDocFormat
End Select
Dim ReportFields As FormulaFieldDefinitions = oRpt.DataDefinition.FormulaFields()
oRpt.DataDefinition.FormulaFields("ReportTitle").Text = ReportTitle '"'" & Replace(ReportTitle, vbCrLf, "' + chr(13) + chr(10) + '") & "'" 'ReportTitle
Dim sExistingFormula As String = oRpt.RecordSelectionFormula
'check for existing formula
If InStr(sExistingFormula, "{") > 0 And ReportFormula <> "" Then
oRpt.RecordSelectionFormula = oRpt.RecordSelectionFormula + " and " + ReportFormula
Else
oRpt.RecordSelectionFormula = ReportFormula
End If
oRpt.Export()
ExportReport = "OK"
Catch e As Exception
Dim sMes As String = "Error: " + e.Message
ExportReport = sMes
End Try
oRpt.Close()
oRpt = Nothing
End Function
MyCrystalReportViewer1.ReportSource = Application.StartupPath & "\..\rptTestA.rpt"
Dim exportFileName As String = "exportedReportK.rpt" Dim exportPath As String = Application.StartupPath & "\" & exportFileName
Dim crReportDocument As New ReportDocument()
crReportDocument.Load("C:\Documents and Settings\kimcjw\Desktop\SleepMonitorSystem3CRDone\KKSSreport.doc")
Dim crExportOptions As ExportOptions
Dim crDestOptions As New DiskFileDestinationOptions()
crDestOptions.DiskFileName = exportPath
crExportOptions = crReportDocument.ExportOptions
crExportOptions.DestinationOptions = crDestOptions
crExportOptions.ExportDestinationType = ExportDestinationType.DiskFile
crExportOptions.ExportFormatType = ExportFormatType. WordForWindows
crReportDocument.Export()
End Sub
[asp.net...]
I could be wrong but you should not be using viewer see if this will work ..
Private Sub btnExport_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnExport.Click
MyCrystalReportViewer1.ReportSource = Nothing
Dim exportFileName As String = "exportedReportK.rpt" Dim exportPath As String = Application.StartupPath & "\" & exportFileName
Dim crReportDocument As New ReportDocument()
crReportDocument.Load("C:\Documents and Settings\kimcjw\Desktop\SleepMonitorSystem3CRDone\KKSSreport.doc")
Dim PatID As New ParameterDiscreteValue()
Dim PatDate As New ParameterDiscreteValue()
PatID.Value = strPatID
PatDate.Value = strPatDate
Dim paramList As New ParameterFields()
Dim paramTemp As New ParameterField()
paramTemp = New ParameterField()
paramTemp.ParameterFieldName = "@PatID"
paramTemp.CurrentValues.Add(PatID)
paramList.Add(paramTemp)
paramTemp = New ParameterField()
paramTemp.ParameterFieldName = "@PatDate"
paramTemp.CurrentValues.Add(PatDate)
paramList.Add(paramTemp)
crReportDocument.ParameterFieldInfo = paramList
'MyCrystalReportViewer1.ReportSource = Application.StartupPath & "\..\rptTestA.rpt"
Dim crExportOptions As ExportOptions
Dim crDestOptions As New DiskFileDestinationOptions()
crDestOptions.DiskFileName = exportPath
crExportOptions = crReportDocument.ExportOptions
crExportOptions.DestinationOptions = crDestOptions
crExportOptions.ExportDestinationType = ExportDestinationType.DiskFile
crExportOptions.ExportFormatType = ExportFormatType. WordForWindows
crReportDocument.Export()