1

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.

Mortech
  • 111
  • 1
  • 9
  • 1
    Why show us huge screenshots of mostly nothing useful? If you're going to post a screenshot, post ONLY what's relevant. Also, don't require us to get an error message from a bad screenshot when an error message is text and you should post text as text. – jmcilhinney Jun 04 '20 at 10:13
  • 1
    Show us the stack trace for the exception. – jmcilhinney Jun 04 '20 at 10:17
  • Create the Bitmap `groupbox` in a `Using ... End Using` block or add `groupbox.Dispose()` after drawing the image. –  Jun 04 '20 at 10:25
  • @JQSOFT did you mean something like this `Using 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 Using` if yes, i tried it but still the same issue – Mortech Jun 04 '20 at 20:53
  • @jmcilhinney this is what i got `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.` – Mortech Jun 04 '20 at 21:41
  • I can't tell relying on images. Anyways, @jmcilhinney has something around this [here](http://www.vbforums.com/showthread.php?858051-RESOLVED-Cannot-find-or-open-the-PDB-file). Also, check [this](https://stackoverflow.com/questions/3469368/how-to-handle-accessviolationexception) out. –  Jun 04 '20 at 21:42
  • @JQSOFT i have edited it, kindly help to look into it – Mortech Jun 04 '20 at 22:16
  • @jmcilhinney i have edited it, kindly help to look into it – Mortech Jun 04 '20 at 22:16
  • Seems you are calling the print routine again and again while the PrintDocument is already busy doing the previous jobs. Also other things in this code need to be fixed. –  Jun 05 '20 at 07:33
  • @JQSOFT ok, so how do i fix the issue? – Mortech Jun 05 '20 at 07:41

1 Answers1

0

Here's some points to consider:

  • Use the same objects as long as that is possible. No need to create new OleDbCommand cd1, cd2, ..etc. Just create one and reuse it by changing its CommandText and update its Parameters.
  • Always use parameterized queries and avoid string concatenating the SQL statements like WHERE [matric]='" & matricno.
  • When you create a new OleDbConnection the connection remains closed until you call the Open method explicitly. So checking the connection State just right after creating the object doesn't make any sense.
  • Keep the connection opened as long as you need to use the same connection again and again. So no need to close and reopen it.
  • The Read method and the HasRows property both return True if the OleDbDataReader has something to read.
  • You should dispose the disposable objects, always create them in Using blocks.

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.

  • thanks so much for you time, but when i run the code it bring the following error `lErrorinfo.GetDescription failed with E_FAIL(0x80004005)` and did not return the values in the database, and for the printing, just like before but the error this time is `Unhandled exception at 0x75ecb57f (shlwapi.dll) in Pau_CGPA.exe: 0xC000041D: 0xC000041D: An unhandled exception was encountered during a user callback.` – Mortech Jun 06 '20 at 09:25
  • @Morakinyo To fix this `lErrorinfo.GetDescription failed with E_FAIL(0x80004005)`, in the SQL, keep the table and fields names in square brackets. As for the second exception, try to reinstall your VS (or maybe its time to upgrade?) and read [this](https://www.wikifixes.com/en/errors/dll/shlwapi.dll?gclid=Cj0KCQjw_ez2BRCyARIsAJfg-kttxSVOm6skDr5J4o_I5Iiqpli19-hCXjfd45_mcia-uz32Sf2hYWQaAo-QEALw_wcB) for details. –  Jun 06 '20 at 11:52