How to print Specific Invoice with Crystal Report ?

rajdh75

Member
Joined
Mar 30, 2020
Messages
20
Programming Experience
Beginner
Hello,
I have a project in which Invoice is generated and saved in MS Access database.
When User search the Invoice by Date wise and Customer wise with Form - FrmSalesReport, the data is displayed from TblInvoiceQuery in datagridview.
When user click desire customer, another Form named FrmInvoiceTrial is opened where the saved Invoice is displayed.
In this Form there is a button named BtnPrint for printing saved Invoice. Following is code for this Button.
VB.NET:
FrmInvoiceCrystalReport.Show()
I have made a Form named as FrmInvoiceCrystalReport in which CrystalReportViewer1 is name of Crystal Report Viewer and made a Crystal Report named as InvoiceCrystalReport for print.
I am using SAP Crystal Report.
I have a Button called BtnPrintPreview for previewing Invoice which opens Crystal Report. The Button has following code
VB.NET:
Dim cmd As OleDbCommand
        Dim adp As New OleDbDataAdapter
        Dim dt As New DataSet
        Dim sql As String
        sql = "select * from TblTest"
        Try
            con.Open()
            cmd = New OleDbCommand(sql, con)
            adp.SelectCommand = cmd
            adp.Fill(dt, "TblTest")
            Dim report As New InvoiceCrystalReport
            report.SetDataSource(dt)
            CrystalReportViewer1.ReportSource = report
            CrystalReportViewer1.Refresh()
            cmd.Dispose()
            adp.Dispose()
            dt.Dispose()
            con.Close()

        Catch ex As Exception
            MsgBox(ex.Message)
            con.Close()
        End Try
        con.Close()
This code shows all data in Table named TblTest.
I only want to print report for Invoice which User has searched for and not all Invoices.

I have a Text box named as TxtInvoiceNo.Text in FrmInvoiceTrial which shows the Invoice No.
So I had made another code for printing searched record as follow
VB.NET:
Dim cr As New InvoiceCrystalReport
Dim cmd As New OleDbCommand("SELECT * from TblTest WHERE InvoiceID=@InvoiceID", con)
        With cmd.Parameters
            .Add("@InvoiceID", OleDbType.VarChar).Value = FrmInvoiceTrial.TxtInvoiceNo.Text

            Dim adp As New OleDbDataAdapter
            Dim dt As New DataSet

            Try
                con.Open()
                adp.SelectCommand = cmd
                adp.Fill(dt, "TblTest")

                cr.SetDataSource(dt)
                CrystalReportViewer1.ReportSource = cr
                CrystalReportViewer1.Refresh()
                cmd.Dispose()
                adp.Dispose()
                dt.Dispose()
                con.Close()

            Catch ex As Exception
                MsgBox(ex.Message)
                con.Close()
            End Try
            con.Close()
        End With
But when this code runs it does not display details it only displays page headers.
How can I resolve this problem ?

Thank you
 

benshaws

Hobbyist Programmer
Joined
Sep 19, 2013
Messages
17
Location
Leeds, UK
Programming Experience
5-10
Hi rajdh75,

Just a guess from me but when you dispose of the dataset is the data the report is linked too lost?

What happens if you comment out line 19 dt.Dispose()?
 
Top Bottom