Here is the full code
Imports System.Data.OleDb
Public Class veterinarycgpa
Private Sub veterinarycgpa_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim matricno, sessionad As String
matricno = pretranscript.matric.Text
sessionad = pretranscript.session.Text
'semester = checkresult.semester.Text
Dim allunit, allwgp As Decimal
Try
Dim con As New System.Data.OleDb.OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0; Data Source=cgpa.accdb")
Dim cmd As New System.Data.OleDb.OleDbCommand
If con.State = ConnectionState.Closed Then
con.Open()
End If
Dim dr As OleDbDataReader
cmd.CommandText = System.Data.CommandType.Text
Dim str As String
str = "SELECT [fullname],[matric], [maiden_name], [gender], [session_admitted], [mode_of_entry], [programme] FROM student WHERE [matric]='" & matricno & "' AND [session_admitted] = '" & sessionad & "' "
Dim cd As OleDbCommand = New OleDbCommand(str, con)
dr = cd.ExecuteReader
dr.Read()
If dr("matric") = pretranscript.matric.Text Then 'checkresult.matric.Text
fullname.Text = dr("fullname").ToString
matric.Text = dr("matric").ToString
mname.Text = dr("maiden_name").ToString
session.Text = dr("session_admitted").ToString
gender.Text = dr("gender").ToString
mode_of_entry.Text = dr("mode_of_entry").ToString
programme.Text = dr("programme").ToString
End If
'MsgBox("No Result found for the given matric no and session", MsgBoxStyle.Critical)
con.Close()
If con.State = ConnectionState.Closed Then
con.Open()
End If
str = " SELECT [matric], [session], [pvp711] ,[pvp713] ,[pvp719] ,[pvp703] ,[pvp715], [pvp717], [pvp701] FROM veterinaryone WHERE [matric]='" & matricno & "' AND [session] = '" & sessionad & "'"
Dim cd1 As OleDbCommand = New OleDbCommand(str, con)
dr = cd1.ExecuteReader
dr.Read()
If dr("matric") = pretranscript.matric.Text Then
score1.Text = dr("pvp711").ToString
score2.Text = dr("pvp713").ToString
score3.Text = dr("pvp719").ToString
score4.Text = dr("pvp703").ToString
score5.Text = dr("pvp715").ToString
score6.Text = dr("pvp717").ToString
score7.Text = dr("pvp701").ToString
End If
con.Close()
If con.State = ConnectionState.Closed Then
con.Open()
End If
str = " SELECT [matric], [session], [pvp710] ,[pvp712] ,[pvp714] ,[pvp716] ,[pvp702], [pvp704], [pvp718], [pvp722], [pau3101], [pau3104] FROM veterinarytwo WHERE [matric]='" & matricno & "' AND [session] = '" & sessionad & "'"
Dim cd2 As OleDbCommand = New OleDbCommand(str, con)
dr = cd2.ExecuteReader
dr.Read()
If dr("matric") = pretranscript.matric.Text Then
score8.Text = dr("pvp710").ToString
score9.Text = dr("pvp712").ToString
score10.Text = dr("pvp714").ToString
score11.Text = dr("pvp716").ToString
score12.Text = dr("pvp702").ToString
score13.Text = dr("pvp704").ToString
score14.Text = dr("pvp718").ToString
score15.Text = dr("pvp722").ToString
score16.Text = dr("pau3101").ToString
score17.Text = dr("pau3104").ToString
End If
con.Close()
If con.State = ConnectionState.Closed Then
con.Open()
End If
str = " SELECT [matric], [session], [pvp721] FROM veterinarythree WHERE [matric]='" & matricno & "' AND [session] = '" & sessionad & "'"
Dim cd3 As OleDbCommand = New OleDbCommand(str, con)
dr = cd3.ExecuteReader
dr.Read()
If dr("matric") = pretranscript.matric.Text Then
score18.Text = dr("pvp721").ToString
End If
con.Close()
If con.State = ConnectionState.Closed Then
con.Open()
End If
str = " SELECT [matric], [session], [pvp720], [pvp724] FROM veterinaryfour WHERE [matric]='" & matricno & "' AND [session] = '" & sessionad & "'"
Dim cd4 As OleDbCommand = New OleDbCommand(str, con)
dr = cd4.ExecuteReader
dr.Read()
If dr("matric") = pretranscript.matric.Text Then
score19.Text = dr("pvp720").ToString
score20.Text = dr("pvp724").ToString
End If
con.Close()
Catch ex As Exception
MsgBox(ex.Message, MsgBoxStyle.Information, "ALERT")
End Try
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Button1.Visible = False
Button2.Visible = False
PrintPreviewDialog1.Document = PrintDocument1
'PrintPreviewDialog1.PrintPreviewControl.Zoom = 1
PrintPreviewDialog1.ShowDialog()
'PrintDocument1.DefaultPageSettings.Landscape = True
PrintDocument1.Print()
Button1.Visible = True
Button2.Visible = True
End Sub
Private Sub PrintDocument1_PrintPage(sender As Object, e As Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage
Dim groupbox As New Bitmap(Me.Width, Me.Height)
Me.DrawToBitmap(groupbox, New Rectangle(0, 0, Me.Width, Me.Height))
e.Graphics.DrawImage(groupbox, 0, 0)
End Sub
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
Me.Close()
End Sub
End Class
I have other forms containing code like this as well, so if I print from this, it print to PDF automatically successfully, but if I close the form and open it again or any other form to print the second time then it breaks and brings about this error.
First-chance exception at 0x5adf7ecf (dui70.dll) in Pau_CGPA.exe: 0xC0000005: Access violation reading location 0x0000001c. Unhandled exception at 0x5adf7ecf (dui70.dll) in Pau_CGPA.exe: 0xC000041D: 0xC000041D: An unhandled exception was encountered during a user callback.
So I don't know what is wrong.
Here's some points to consider:
cd1
, cd2
, ..etc. Just create one and reuse it by changing its CommandText and update its Parameters.WHERE [matric]='" & matricno
.True
if the OleDbDataReader has something to read. Now, applying that on the Load
event block would produce:
Imports System.Data.OleDb
Imports System.Drawing.Printing
'...
Private Sub veterinarycgpa_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim matricno = pretranscript.matric.Text
Dim sessionad = pretranscript.session.Text
Try
Using con As New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0; Data Source=cgpa.accdb"),
cmd As New OleDbCommand("SELECT [fullname], [matric], [maiden_name], [gender], [session_admitted], [mode_of_entry], [programme] FROM [student] WHERE [matric] = ? AND [session_admitted] = ?", con)
'Better to use the Add(..) method instead and define the right OleDbType and size...
cmd.Parameters.AddWithValue("matric", matricno)
cmd.Parameters.AddWithValue("session_admitted", sessionad)
con.Open()
Using dr = cmd.ExecuteReader
If dr.Read() Then
fullname.Text = dr("fullname").ToString
matric.Text = dr("matric").ToString
mname.Text = dr("maiden_name").ToString
session.Text = dr("session_admitted").ToString
gender.Text = dr("gender").ToString
mode_of_entry.Text = dr("mode_of_entry").ToString
programme.Text = dr("programme").ToString
End If
End Using
cmd.Parameters.Clear()
cmd.CommandText = "SELECT [matric], [session], [pvp711], [pvp713], [pvp719], [pvp703], [pvp715], [pvp717], [pvp701] FROM [veterinaryone] WHERE [matric] = ? AND [session] = ?"
cmd.Parameters.AddWithValue("matric", matricno)
cmd.Parameters.AddWithValue("session", sessionad)
Using dr = cmd.ExecuteReader
If dr.Read() Then
score1.Text = dr("pvp711").ToString
score2.Text = dr("pvp713").ToString
score3.Text = dr("pvp719").ToString
score4.Text = dr("pvp703").ToString
score5.Text = dr("pvp715").ToString
score6.Text = dr("pvp717").ToString
score7.Text = dr("pvp701").ToString
End If
End Using
cmd.Parameters.Clear()
cmd.CommandText = "SELECT [matric], [session], [pvp710], [pvp712], [pvp714], [pvp716], [pvp702], [pvp704], [pvp718], [pvp722], [pau3101], [pau3104] FROM [veterinarytwo] WHERE [matric] = ? AND [session] = ?"
cmd.Parameters.AddWithValue("matric", matricno)
cmd.Parameters.AddWithValue("session", sessionad)
Using dr = cmd.ExecuteReader
If dr.Read() Then
score8.Text = dr("pvp710").ToString
score9.Text = dr("pvp712").ToString
score10.Text = dr("pvp714").ToString
score11.Text = dr("pvp716").ToString
score12.Text = dr("pvp702").ToString
score13.Text = dr("pvp704").ToString
score14.Text = dr("pvp718").ToString
score15.Text = dr("pvp722").ToString
score16.Text = dr("pau3101").ToString
score17.Text = dr("pau3104").ToString
End If
End Using
cmd.Parameters.Clear()
cmd.CommandText = "SELECT [matric], [session], [pvp721] FROM [veterinarythree] WHERE [matric] = ? AND [session] = ?"
cmd.Parameters.AddWithValue("matric", matricno)
cmd.Parameters.AddWithValue("session", sessionad)
Using dr = cmd.ExecuteReader
If dr.Read() Then
score18.Text = dr("pvp721").ToString
End If
End Using
cmd.Parameters.Clear()
cmd.CommandText = "SELECT [matric], [session], [pvp720], [pvp724] FROM [veterinaryfour] WHERE [matric]= ? AND [session] = ?"
cmd.Parameters.AddWithValue("matric", matricno)
cmd.Parameters.AddWithValue("session", sessionad)
Using dr = cmd.ExecuteReader
If dr.Read() Then
score19.Text = dr("pvp720").ToString
score20.Text = dr("pvp724").ToString
End If
End Using
End Using
Catch ex As Exception
MsgBox(ex.Message, MsgBoxStyle.Information, "ALERT")
End Try
End Sub
As for the printing part:
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
PrintDialog1.Document = PrintDocument1
If PrintDialog1.ShowDialog = DialogResult.OK Then
PrintDialog1.Document.Print()
End If
End Sub
Private Sub PrintDocument1_BeginPrint(sender As Object, e As PrintEventArgs) Handles PrintDocument1.BeginPrint
Button1.Visible = False
Button2.Visible = False
End Sub
Private Sub PrintDocument1_PrintPage(sender As Object, e As PrintPageEventArgs) Handles PrintDocument1.PrintPage
Using bmp As New Bitmap(Width, Height)
DrawToBitmap(bmp, New Rectangle(0, 0, Width, Height))
e.Graphics.DrawImage(bmp, 0, 0)
'You might want to try:
'e.Graphics.DrawImage(bmp, ea.PageBounds.X, ea.PageBounds.Y, ea.PageBounds.Width, ea.PageBounds.Height)
'e.Graphics.DrawImage(bmp, ea.MarginBounds.X, ea.MarginBounds.Y, ea.MarginBounds.Width, ea.MarginBounds.Height)
End Using
End Sub
Private Sub PrintDocument1_EndPrint(sender As Object, e As PrintEventArgs) Handles PrintDocument1.EndPrint
Button1.Visible = True
Button2.Visible = True
End Sub
Accordingly, you can delete both the PrintPreviewDialog1
and PrintDocument1
. And don't forget to delete the PrintDocument1_PrintPage
handler.
User contributions licensed under CC BY-SA 3.0