Click here to Skip to main content
15,881,281 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Dear all experts: After struggling for almost 2 weeks I dare to post a question for help. I’m a newbie in Crystal Reports and I started to using Crystal Report for making a dynamic report. My problem is I am getting an Object reference not set to an instance of an object when Crystal Report View assigned as Report Document, but the report is shown successfully under Crystal Report Designer. I’m using VB.Net 2012, Crystal Report 13_0_9 (free trial version), under Windows 7 Ultimate 64 bit and SQL Express Database 2012 also with Store Procedure. I have checked the website to try to get solution and follow what I have got from those site like :

1. Reinstall CR with 32 bit after that install CR 64 bit
2. Change target CPU from any to 32 or 64
3. Install Service Pack CR_13_0_9
4. Make sure target framework to .Net Framework 4.5
5. Install service pack for CR
6. Change Copy to Output Directory on report.rpt to Copy if newer or Copy Always

But it still did not solve the problem. This is really making me frustrated. How can I solve this error? Below is my code.

--- subroutine from frmGoodsRcvd.frm


VB
Public Sub Print()  
    Dim frmCrystalViewer As New frmCrystalViewer  

    mdlPrint.SendParams("GoodsRcvdNote", lblRcvdNo.Text)  
    frmCrystalViewer.Show_Report()  
    frmCrystalViewer.CRV.PrintReport()  
End Sub  


--- Module Print -----


VB
Imports CrystalDecisions.CrystalReports.Engine  
Imports CrystalDecisions.Shared  
Imports System.IO  
Imports System.Data.SqlClient  

Module mdlPrint  
    Public Params() As String  
    Public Report As String  

    'Variabel Parameter  
    Public paramFields As ParameterFieldDefinitions  
    Public paramValue As ParameterValues  
    Public paramDiscreteValue As New ParameterDiscreteValue  
    Public crFormulas As FormulaFieldDefinitions  
    Public crFormulaTextField() As FormulaFieldDefinition  
    Public IParam As Integer  
    Public param As Object  

    Sub SendParams(ByVal RptType As String, ByVal ParamArray param() As String)  
        Report = UCase(RptType)  
        Params = param  
    End Sub  

    Sub SetParameter(ByVal paramDef As ParameterFieldDefinitions, ByVal paramName As String, ByVal 

paramValue As String)  
        Dim crParameterFieldDefinition As ParameterFieldDefinition = paramDef.Item(paramName)  
        Dim crParameterValues As ParameterValues = crParameterFieldDefinition.CurrentValues()  
        Dim crParameterDiscreteValue As New ParameterDiscreteValue  

        crParameterDiscreteValue.Value = paramValue  
        crParameterValues.Add(crParameterDiscreteValue)  
        crParameterFieldDefinition.ApplyCurrentValues(crParameterValues)  
    End Sub  

    Function PrintReport(ByRef _oRpt As ReportDocument) As Boolean
        Select Case frmMain.strModule.ToUpper  
            Case "PURCHASE"  
                 Call ListPurchaseRpt(_oRpt)  
        End Select  

        If Not RptDataConn(_oRpt) Then  
           PrintReport = False  
           Exit Function  
        End If  
        PrintReport = True  
    End Function  

    Public Function RptDataConn(ByRef _oRpt As ReportDocument) As Boolean  
        Dim myConnectionInfo As New ConnectionInfo  
        Dim myTable As CrystalDecisions.CrystalReports.Engine.Table  

 
        myConnectionInfo.ServerName = "Server"  
        myConnectionInfo.DatabaseName = "Goods"  
        myConnectionInfo.UserID = "sa"  
        myConnectionInfo.Password = "Test"  

        For Each myTable In _oRpt.Database.Tables  
            myTable.LogOnInfo.ConnectionInfo = myConnectionInfo  
            myTable.ApplyLogOnInfo(myTable.LogOnInfo)  
        Next  
        RptDataConn = True  
    End Function  

    Public Sub ListPurchaseRpt(ByRef _oRpt As ReportDocument)  
        Select Case Report  
            Case "GOODSRCVDNOTE"  
                 _oRpt.Load(Application.StartupPath & "\GoodsRcvdNote.rpt")  
                 paramFields = _oRpt.DataDefinition.ParameterFields  
                 IParam = 1  
                 For Each param As Object In Params  
                     Select Case IParam  
                         Case 1 : SetParameter(paramFields, "@RcvdNo", CStr(param))  
                     End Select  
                     IParam = IParam + 1  
                 Next param  
        End Select  
    End Sub  
End Module 



--- form frmCrystalViewer


VB
Imports System.Data.SqlClient  
Imports CrystalDecisions.CrystalReports.Engine  
Imports CrystalDecisions.Shared  
Imports System.Drawing.Printing  

Public Class frmCrystalViewer  
    Inherits System.Windows.Forms.Form  

    Dim lastPageNumber As Integer  
    Dim oRpt As ReportDocument  

    Public Sub Print()  
        Dim PrintSetup As New PrintDialog  
        Dim PrintDoc As New PrintDocument  

        If PrinterSettings.InstalledPrinters.Count = 0 Then  
            MsgBox("No Printer is installed")  
            PrintDoc.Dispose()  
            PrintSetup.Dispose()  
            Exit Sub  
        End If  

        PrintDoc.DocumentName = oRpt.FileName  

        If oRpt.PrintOptions.PrinterName = "" Then  
            oRpt.PrintOptions.PrinterName = PrintSetup.PrinterSettings.PrinterName  
        End If  

        PrintSetup.PrinterSettings.PrinterName = oRpt.PrintOptions.PrinterName  

        For i As Integer = 0 To PrintSetup.PrinterSettings.PaperSizes.Count - 1  
            If PrintSetup.PrinterSettings.PaperSizes.Item(i).Kind = oRpt.PrintOptions.PaperSize Then  
                PrintDoc.DefaultPageSettings.PaperSize = PrintSetup.PrinterSettings.PaperSizes.Item(i)  
            End If  
        Next  

        If oRpt.PrintOptions.PaperOrientation = PaperOrientation.Landscape Then  
            PrintSetup.PrinterSettings.DefaultPageSettings.Landscape = True  
        Else  
            PrintSetup.PrinterSettings.DefaultPageSettings.Landscape = False  
        End If  

        PrintSetup.AllowSomePages = True  

        PrintSetup.PrinterSettings.FromPage = 1  
        PrintSetup.PrinterSettings.ToPage = lastPageNumber  

        If (PrintSetup.ShowDialog = DialogResult.OK) Then
            If PrintDoc.DefaultPageSettings.PaperSize.PaperName <> "" Then
                oRpt.PrintOptions.PrinterName = PrintSetup.PrinterSettings.PrinterName
                oRpt.PrintOptions.PaperSize = CType(PrintDoc.DefaultPageSettings.PaperSize.Kind, 

CrystalDecisions.Shared.PaperSize)
                If PrintSetup.PrinterSettings.DefaultPageSettings.Landscape Then
                    oRpt.PrintOptions.PaperOrientation = PaperOrientation.Landscape
                Else
                    oRpt.PrintOptions.PaperOrientation = PaperOrientation.Portrait
                End If

                CRV.ReportSource = oRpt

                If PrintSetup.PrinterSettings.PrintRange = PrintRange.AllPages Then
                    oRpt.PrintToPrinter(PrintSetup.PrinterSettings.Copies, 

PrintSetup.PrinterSettings.Collate, 0, 0)
                ElseIf PrintSetup.PrinterSettings.PrintRange = PrintRange.SomePages Then
                    oRpt.PrintToPrinter(PrintSetup.PrinterSettings.Copies, 

PrintSetup.PrinterSettings.Collate, PrintSetup.PrinterSettings.FromPage, PrintSetup.PrinterSettings.ToPage)
                End If
            End If
        End If

        PrintDoc.Dispose()
        PrintSetup.Dispose()
    End Sub

    Public Sub Show_Report()
        oRpt = New ReportDocument
        If CBool(mdlPrint.PrintReport(oRpt)) Then
            CRV.ReportSource = oRpt          'This line triggers the error 
            CRV.ShowExportButton = True          
            CRV.ShowRefreshButton = False
            CRV.ShowPrintButton = False

            CRV.ShowLastPage()
            lastPageNumber = CRV.GetCurrentPageNumber
            CRV.ShowFirstPage()
        Else
            Exit Sub
        End If
    End Sub

    Private Sub frmCrystalViewer_Closing(ByVal sender As Object, ByVal e As 

System.ComponentModel.CancelEventArgs) Handles MyBase.Closing
        oRpt.Close()
        oRpt.Dispose()
    End Sub

    Private Sub frmCrystalViewer_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles 

MyBase.Load
        Call Show_Report()
    End Sub


Hope someone out of there can help me to fixed the problem, many thanks for your helps.
Posted
Updated 14-Aug-14 8:25am
v2
Comments
[no name] 14-Aug-14 7:25am    
You debug your code, find the object that is null that you are trying to use, then you make it not null.
Member 11012688 27-Aug-14 21:53pm    
I know there is a bug and I have debug but I still cannot solve the problem, could you give me some tips or how to correct the code based on my code above. Many thanks for your helps!
thatraja 2-Sep-14 9:08am    
In which line, the error occurs?
Member 11012688 9-Oct-14 1:07am    
I'm still struggling to fixed the error until today but still facing difficulties to solve this matter, I have check calling method or properties by passing parameter value by ByRef. The line which trigger the error is CRV.ReportSource = oRpt under method Show_Report. I don't know why the oRpt throw a null value.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900