Wrox Programmer Forums
Go Back   Wrox Programmer Forums > .NET > .NET 1.0 and Visual Studio.NET > .NET Framework 1.x
| Search | Today's Posts | Mark Forums Read
.NET Framework 1.x For discussing versions 1.0 and 1.1 of the Microsoft .NET Framework.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the .NET Framework 1.x section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
  #1 (permalink)  
Old June 11th, 2008, 02:20 AM
Registered User
 
Join Date: Aug 2007
Location: , , .
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Default Problem in exporting crystal report to excel.

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.
Code:
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.



Similar Threads
Thread Thread Starter Forum Replies Last Post
Exporting Crystal Report To Excel mohithmohith VB.NET 1 July 12th, 2008 12:36 AM
Code for Exporting Crystal Report to Excel or PDF shakti_2505270 ASP.NET 2.0 Professional 1 October 4th, 2007 01:00 AM
Crystal Report Export problem to excel spmano1983 VB.NET 0 August 17th, 2007 01:33 AM
Exporting Crystal Report Harinder.Dhamija ASP.NET 2.0 Professional 1 July 24th, 2007 02:45 AM
problem in Exporting Crystal report to PDF cat_net Crystal Reports 0 October 3rd, 2005 11:49 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.