0

アプリケーションでは、多くの Crystal レポートがあり、それらはすべて 1 つの方法で (同じ方法で) ロードされ、そのうちの 1 つはおそらく 5 秒または 10 秒ごとにロードされます (より明確にするために、多くのアイテムを含むリスト ビューがあり、それぞれがユーザーがそれをダブルクリックすると、Crystal レポートが開き、その項目に関するデータが表示されます)

現在の問題は、ユーザーがダブルクリックで 3 つまたは 4 つ以上のレポートを開き (同時にではなく、1 つを閉じて別のレポートを開く)、別のレポートを開くと、メモリが原因でアプリがクラッシュすることです!

編集: Form Closed Event で、レポート ドキュメントの Close() と Dispose() を作成します。

レポートはフォームクラスで一般変数として宣言されていることに注意してください。これは、フォームクローズイベントで破棄するためです。

これは担当者フォームのコードです:

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

何が問題ですか ?

どうもありがとうございます

4

0 に答える 0