0

In application, there are many crystal reports, and all of them load with one method (same way), one of them loads maybe every 5 or 10 seconds (for more clarification, there is a list view with many items, each of them if the user make double click on it, a crystal report open and shows data about that item)

The problem now, is when user opens more than 3 or 4 reports by double click (not at same time, close one and open another one), and then open another reports, the app crashes because of memory!

Edited: in Form Closed Event, I make Close() and Dispose() for the report document.

Note that the report is declared as general variable in form class, that for dispose it in form close event.

This is the rep form code :

Imports System.Drawing.Printing
Imports CrystalDecisions.Shared
Imports CrystalDecisions.CrystalReports.Engine
Imports System.Data.OleDb

Public Class rep

    Public isEm, isShown As Boolean
    Public repName As String
    Dim CurrentReport As CrystalDecisions.CrystalReports.Engine.ReportDocument
    Public onNumT As String
    Public cl As Boolean

    Function loadSource(ByVal placeType As String) As DataTable

        Dim Conn As New OleDbConnection(ConnectionString)
        Dim Command As New OleDbCommand
        Command.CommandType = CommandType.Text

        Select Case placeType
            Case "city"
                Command.CommandText = "select placeName as TT from places where placeType = 'مدينة' order by placeName"
            Case "port"
                Command.CommandText = "select placeType+' '+placeName as TT from places where placeType in('مطار','ميناء','حدود') order by placeName"
            Case "driver"
                Command.CommandText = "select driverName from drivers"
        End Select

        Command.Connection = Conn
        Dim dataadapter As New OleDb.OleDbDataAdapter(Command.CommandText, Conn)

        Try
            Dim dt As New DataTable
            dataadapter.Fill(dt)
            Return dt.Copy
        Catch ex As Exception
            MsgBox(ex.Message, MsgBoxStyle.MsgBoxRight + MsgBoxStyle.Exclamation)
        Finally
            dataadapter.Dispose()
            Conn.Dispose()
            Command.Dispose()
        End Try
    End Function

    Private Sub LogOn(ByVal poReport As CrystalDecisions.CrystalReports.Engine.ReportDocument, _
                      ByVal strServer As String, _
                      ByVal strTable As String, _
                      ByVal strUsername As String, _
                      ByVal strPassword As String)

        Dim lsMethod As String = System.Reflection.MethodBase.GetCurrentMethod().Name
        Dim loConnInfo As New CrystalDecisions.Shared.ConnectionInfo
        loConnInfo.ServerName = strServer
        loConnInfo.DatabaseName = strTable
        loConnInfo.UserID = strUsername
        loConnInfo.Password = strPassword
        Dim loTables As CrystalDecisions.CrystalReports.Engine.Tables
        Dim loTable As CrystalDecisions.CrystalReports.Engine.Table
        Dim loTableLogonInfo As CrystalDecisions.Shared.TableLogOnInfo
        loTables = poReport.Database.Tables

        For Each loTable In loTables
            loTableLogonInfo = loTable.LogOnInfo
            loTableLogonInfo.ConnectionInfo.ServerName = strServer
            loTableLogonInfo.ConnectionInfo.ServerName = strTable
            loTable.ApplyLogOnInfo(loTableLogonInfo)
        Next
    End Sub

    Sub loadReport()

        Cursor = Cursors.WaitCursor

        Select Case repName
            Case "moves1"
                CurrentReport = New moves1
                LogOn(CurrentReport, DataBasePath, DataBasePath, "Admin", "")
                movesPanel.BringToFront()
                CityMoves.DataSource = loadSource("city")
                CityMoves.ValueMember = "TT"
                Me.AcceptButton = showButtonMoves
                movTypeMoves.SelectedItem = "الكل"
            Case "moves2"
                CurrentReport = New moves2
                LogOn(CurrentReport, DataBasePath, DataBasePath, "Admin", "")
                movesPanel.BringToFront()
                CityMoves.DataSource = loadSource("city")
                CityMoves.ValueMember = "TT"
                Me.AcceptButton = showButtonMoves
                movTypeMoves.SelectedItem = "الكل"
            Case "arrTrip"
                CurrentReport = New arrivalReport
                LogOn(CurrentReport, DataBasePath, DataBasePath, "Admin", "")
                tripsPanel.BringToFront()
                portTrip.DataSource = loadSource("port")
                portTrip.ValueMember = "TT"
                tripCaption.Text = "تاريخ الوصول"
                Me.AcceptButton = showButtonTrip
            Case "leaTrip"
                CurrentReport = New leavingReport
                LogOn(CurrentReport, DataBasePath, DataBasePath, "Admin", "")
                tripsPanel.BringToFront()
                portTrip.DataSource = loadSource("port")
                portTrip.ValueMember = "TT"
                tripCaption.Text = "تاريخ المغادرة"
                Me.AcceptButton = showButtonTrip
            Case "transReports"
                CurrentReport = New transReports
                LogOn(CurrentReport, DataBasePath, DataBasePath, "Admin", "")
                Me.AcceptButton = trButton
                transportPanel.BringToFront()
            Case "MovesPayes"
                CurrentReport = New MovesPayes
                LogOn(CurrentReport, DataBasePath, DataBasePath, "Admin", "")
                EmptyMovesPanel.BringToFront()
                driverName.DataSource = loadSource("driver")
                driverName.ValueMember = "driverName"
                Me.AcceptButton = showButtonEmptymoves
            Case "MonthlyMovesPayes"
                CurrentReport = New MonthlyMovesPayes
                LogOn(CurrentReport, DataBasePath, DataBasePath, "Admin", "")
                EmptyMovesPanel.BringToFront()
                driverName.Visible = False
                drCaption.Visible = False
                Me.AcceptButton = showButtonEmptymoves
            Case "trShow"
                CurrentReport = New transReports
                LogOn(CurrentReport, DataBasePath, DataBasePath, "Admin", "")
                Me.AcceptButton = trButton
                hp.BringToFront()
                Me.AcceptButton = Nothing
                CurrentReport.SetParameterValue(0, onNumT)
                Report.ReportSource = CurrentReport
        End Select

        Cursor = Cursors.Default
    End Sub

    Private Sub rep_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load

        Me.Tag = Me.Text
        loadReport()
    End Sub

    Private Sub rep_Shown(sender As System.Object, e As System.EventArgs) Handles MyBase.Shown

        Cursor = Cursors.Default
        isShown = True
    End Sub

    Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles printt.Click

        Dim m As New CrystalDecisions.CrystalReports.Engine.ReportDocument
        Dim oo As New PrintDialog

        Try
            If Report.ReportSource Is Nothing Then Exit Sub Else m = Report.ReportSource
            oo.AllowSomePages = True

            If oo.ShowDialog() <> DialogResult.OK Then Exit Sub
            m.PrintToPrinter(oo.PrinterSettings, oo.PrinterSettings.DefaultPageSettings, False)
        Catch ex As Exception
            MsgBox(ex.Message, MsgBoxStyle.Exclamation + MsgBoxStyle.MsgBoxRight)
        Finally
            m.Dispose()
            oo.Dispose()
        End Try
    End Sub

    Private Sub Button2_Click(sender As System.Object, e As System.EventArgs) Handles send.Click

        If Report.ReportSource Is Nothing Then Exit Sub
        Dim mailf As New MailForm
        Dim reportt As New CrystalDecisions.CrystalReports.Engine.ReportDocument

        Try
            Cursor = Cursors.WaitCursor
            reportt = CurrentReport
            FileIO.FileSystem.CreateDirectory(Application.StartupPath & "\temp\")
        reportt.ExportToDisk(CrystalDecisions.Shared.ExportFormatType.PortableDocFormat, Application.StartupPath & "\temp\temp.pdf")
            If FileIO.FileSystem.FileExists(Application.StartupPath & "\temp\temp.pdf") = True Then mailf.addfile(Application.StartupPath & "\temp\temp.pdf") Else Exit Sub
            mailf.bodyy.Text = " "
            mailf.subject.Text = "شركة البركة"
            mailf.ShowDialog()
            Cursor = Cursors.Default
            Kill(Application.StartupPath & "\temp\temp.pdf")
        Catch ex As Exception
            MsgBox(ex.Message, MsgBoxStyle.Exclamation + MsgBoxStyle.MsgBoxRight)
        Finally
            reportt.Close()
            reportt.Dispose()
            mailf.Dispose()
        End Try
    End Sub

    Private Sub rep_FormClosing(sender As System.Object, e As System.Windows.Forms.FormClosingEventArgs) Handles MyBase.FormClosing

        Cursor = Cursors.Default
    End Sub

    Private Sub showButtonMoves_Click(sender As System.Object, e As System.EventArgs) Handles showButtonMoves.Click, showButtonEmptymoves.Click, showButtonTrip.Click, CityMoves.SelectedValueChanged, dateMoves.ValueChanged, DateTrip.ValueChanged, driverName.SelectedValueChanged, DriverDate.ValueChanged, dateMoves.ValueChanged, portTrip.SelectedValueChanged, trButton.Click, movTypeMoves.SelectedValueChanged

        Try
            If isShown = False Then Exit Sub
            If CurrentReport Is Nothing Then loadReport()
            Report.ReportSource = Nothing

            Select Case repName
                Case "moves1", "moves2"
                    CurrentReport.SetParameterValue("d1", dateMoves.Value)
                    CurrentReport.SetParameterValue("cit", CityMoves.GetItemText(CityMoves.SelectedItem).Replace("المدينة المنورة", "المدينة"))
                    CurrentReport.SetParameterValue("moveType", movTypeMoves.Text)
                    Report.ReportSource = CurrentReport
                Case "leaTrip", "arrTrip"
                    CurrentReport.SetParameterValue("d1", DateTrip.Value.ToShortDateString)
                    CurrentReport.SetParameterValue("customport", portTrip.GetItemText(portTrip.SelectedValue))
                    Report.ReportSource = CurrentReport
                Case "MovesPayes"
                    CurrentReport.SetParameterValue("drivername", driverName.GetItemText(driverName.SelectedValue))
                    CurrentReport.SetParameterValue("mont", DriverDate.Value.Month)
                    Report.ReportSource = CurrentReport
                Case "MonthlyMovesPayes"
                    CurrentReport.SetParameterValue("mont", DriverDate.Value.Month)
                    Report.ReportSource = CurrentReport
                Case "transReports"
                    CurrentReport.SetParameterValue(0, onNum.Text)
                    Report.ReportSource = CurrentReport
            End Select
        Catch ex As Exception
            MsgBox(ex.Message, MsgBoxStyle.MsgBoxRight + MsgBoxStyle.Exclamation)
        End Try
    End Sub

    Private Sub nMoves1_Click(sender As System.Object, e As System.EventArgs) Handles nMoves1.Click

        Report.ShowNextPage()
    End Sub

    Private Sub nfMoves1_Click(sender As System.Object, e As System.EventArgs) Handles nfMoves1.Click

        Report.ShowLastPage()
    End Sub

    Private Sub pMoves1_Click(sender As System.Object, e As System.EventArgs) Handles pMoves1.Click

        Report.ShowPreviousPage()
    End Sub

    Private Sub pfMoves1_Click(sender As System.Object, e As System.EventArgs) Handles pfMoves1.Click

        Report.ShowFirstPage()
    End Sub

    Private Sub rep_FormClosed(sender As System.Object, e As System.Windows.Forms.FormClosedEventArgs) Handles MyBase.FormClosed

        Report.ReportSource = Nothing
        CurrentReport.Close()
    End Sub
End Class

What is the problem ?

Thank You Very Much

4

0 回答 0