Writing Code
 Crystal Reports Forum : Crystal Reports .NET 2003 : Writing Code
Message Icon Topic: Problem in exporting crystal report. Post Reply Post New Topic
Author Message
yasinirshad
Newbie
Newbie
Avatar

Joined: 24 Sep 2007
Location: Saudi Arabia
Online Status: Offline
Posts: 39
Quote yasinirshad Replybullet Topic: Problem in exporting crystal report.
    Posted: 11 Jun 2008 at 12:05am

Hi,
Am getting this error
"System.NullReferenceException: Object reference not set to an instance of an object."

This error comes when i am trying to export crystal report in excel format.
Exporting to pdf works fine though.

On local development machine, exporting to pdf and excel works.
On server, only exporting to pdf works..exporting to excel throws this error.

can anyone pls help. Below is my code sample for export button click.

Private Sub Btn_Export_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Btn_Export.Click
        Try
            Dim oStream As New MemoryStream
            Me.Crystalreportviewer1.ParameterFieldInfo.Clear()
            Me.Crystalreportviewer1.ReportSource = Server.MapPath("Capex_Summary.rpt")
            Dim ParamFields As ParameterFields = Me.Crystalreportviewer1.ParameterFieldInfo
            Dim Per As New ParameterField
            Per.ParameterFieldName = "Period"
            Dim ParamCurrentValues As New ParameterValues
            Dim Period_Value As New ParameterRangeValue
            Period_Value.StartValue = Me.txtSTdate.Text
            Period_Value.EndValue = Me.txtCLdate.Text
            ParamCurrentValues.Add(Period_Value)
            ParamFields.Add(Per)
            crReportDocument.DataDefinition.ParameterFields(0).ApplyCurrentValues(ParamCurrentValues)

            'Set Division Paramter               
            Dim GetDivisionValues As String
            GetDivisionValues = Trim(Request.Form(ddl_division.UniqueID))
            Per = New ParameterField
            ParamCurrentValues = New ParameterValues
            Per.ParameterFieldName = "Division"
            Dim Division_Value As New ParameterDiscreteValue
            If GetDivisionValues = "Select All" Then
                Division_Value.Value = "*"
            Else
                Division_Value.Value = GetDivisionValues
            End If
            'Per.CurrentValues.Add(Division_Value)
            ParamCurrentValues.Add(Division_Value)
            ParamFields.Add(Per)
            crReportDocument.DataDefinition.ParameterFields(1).ApplyCurrentValues(ParamCurrentValues)

            'Set Department Parameter
            Dim GetDeptValues As String
            GetDeptValues = Trim(Request.Form(ddl_department.UniqueID))
            Per = New ParameterField
            ParamCurrentValues = New ParameterValues
            Per.ParameterFieldName = "Department"
            Dim Department_Value As New ParameterDiscreteValue
            If GetDeptValues = "Select All" Then
                Department_Value.Value = "*"
            Else
                Department_Value.Value = GetDeptValues
            End If
            'Per.CurrentValues.Add(Department_Value)
            ParamCurrentValues.Add(Department_Value)
            ParamFields.Add(Per)
            crReportDocument.DataDefinition.ParameterFields(2).ApplyCurrentValues(ParamCurrentValues)

            'Category
            Dim GetCategory As String
            GetCategory = Trim(Request.Form(ddl_Category.UniqueID))
            Per = New ParameterField
            ParamCurrentValues = New ParameterValues
            Per.ParameterFieldName = "Category"
            Dim Category_Value As New ParameterDiscreteValue
            If GetCategory = "Select All" Then
                Category_Value.Value = "*"
            Else
                Category_Value.Value = GetCategory
            End If
            'Per.CurrentValues.Add(Category_Value)
            ParamCurrentValues.Add(Category_Value)
            ParamFields.Add(Per)
            crReportDocument.DataDefinition.ParameterFields(3).ApplyCurrentValues(ParamCurrentValues)
            'crReportDocument.SetDatabaseLogon(gbVariables.strUsrName, gbVariables.strUsrPwd, gbVariables.strServerName, gbVariables.strDBName)
            Dim ExportFormatType As String
            ExportFormatType = Trim(Request.Form(ddl_export.UniqueID))
            Select Case ExportFormatType
                Case "Portable Document (PDF)"
                    crReportDocument.SetDatabaseLogon(gbVariables.strUsrName, gbVariables.strUsrPwd, gbVariables.strServerName, gbVariables.strDBName)
                    'oStream = crReportDocument.ExportToStream(CrystalDecisions.Shared.ExportFormatType.PortableDocFormat)
                    oStream = DirectCast(crReportDocument.ExportToStream(CrystalDecisions.[Shared].ExportFormatType.PortableDocFormat), MemoryStream)
                    Response.Clear()
                    Response.Buffer = True
                    Response.ContentType = "application/pdf"
                    Response.BinaryWrite(oStream.ToArray())
                    Response.End()
                Case "MS Excel (XLS)"
                    crReportDocument.SetDatabaseLogon(gbVariables.strUsrName, gbVariables.strUsrPwd, gbVariables.strServerName, gbVariables.strDBName)
                    'oStream = crReportDocument.ExportToStream(CrystalDecisions.Shared.ExportFormatType.Excel)
                    oStream = DirectCast(crReportDocument.ExportToStream(CrystalDecisions.[Shared].ExportFormatType.Excel), MemoryStream)
                    Response.Clear()
                    Response.Buffer = True
                    Response.ContentType = "application/vnd.ms-excel"
                    Response.BinaryWrite(oStream.ToArray())
                    Response.End()                   
            End Select           
        Catch ex As Exception
            lblError.Text = ex.Message.ToString
        End Try

Thanks to help.

Thanks,
Yasin.
IP IP Logged
zaroblan
Newbie
Newbie
Avatar

Joined: 04 Nov 2008
Location: South Africa
Online Status: Offline
Posts: 5
Quote zaroblan Replybullet Posted: 04 Nov 2008 at 4:28am
Are you sure Excel has been installed on the server?
If there is no Excel the export will not work.
IP IP Logged
Post Reply Post New Topic
Printable version Printable version

Forum Jump
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot delete your posts in this forum
You cannot edit your posts in this forum
You cannot create polls in this forum
You cannot vote in polls in this forum



This page was generated in 0.016 seconds.