在应用程序中,有许多水晶报告,并且所有这些报告都使用一种方法(相同方式)加载,其中一种加载可能每5或10秒(为了进一步说明,有一个包含许多项目的列表视图,每个如果用户双击它,则打开水晶报告并显示有关该项目的数据)
现在的问题是,当用户通过双击打开超过3或4个报告时(不是同时关闭一个并打开另一个),然后打开另一个报告,应用程序因内存而崩溃!
已编辑:在表单已关闭事件中,我为报表文档创建了Close()和Dispose()。
请注意,报表在表单类中声明为通用变量,用于将其置于表单close事件中。
这是代表形式代码:
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
有什么问题?
非常感谢