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.