Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Multiple pate PrintDocument with a DataTable

Posted on 2009-04-08
5
Medium Priority
?
1,448 Views
Last Modified: 2012-05-06
I have a VERY simple report to develop and thought that developing it would be very simple.  Unfortunately just the opposite has turned out to be true because I am unfamiliar with the PrintDocument object.

I retrieve a DataReader from the database and Loop through the records.  Each record I want to add a line to the report.  With each line I add a specific about of space which actually prints well.  The problem is that the code prints on one page,each page on top of the other.  

BTW, I am always open to a BETTER way if the PrintDocument is not how I should be doing simple reports.

Thanks in advance,

db
Private Sub pdReport_PrintPage(ByVal sender As System.Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles pdReport.PrintPage
 
        'Get the data
        Dim cn As New OleDbConnection(ConfigurationManager.ConnectionStrings("TheConnection").ConnectionString)
        cn.Open()
        Dim cm As New OleDbCommand("Select Name,City,State from Suppliers", cn)
        cm.CommandType = CommandType.StoredProcedure
        Dim rdr As OleDbDataReader = cm.ExecuteReader
        'Set the starting vertical location for printing
        Dim y As Integer = 55
        'Loop through the records
        Do While rdr.Read
            'Print each column at the specific location
            e.Graphics.DrawString(rdr.Item("Name").ToString, printFont, Brushes.Black, 100, y + 15)
            e.Graphics.DrawString(rdr.Item("City").ToString, printFont, Brushes.Black, 250, y + 15)
            e.Graphics.DrawString(rdr.Item("State").ToString, printFont, Brushes.Black, 425, y + 15)
            'Increment y so the next line will print after the current line
            y += 15
            'Evaluate y to see if a new page is needed
            If y >= 1000 Then
                e.HasMorePages = True
                'Reset where y starts 
                y = 55
            End If
        Loop
        'No more records end the document
        e.HasMorePages = False
End Sub

Open in new window

0
Comment
Question by:dbote
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
5 Comments
 
LVL 96

Accepted Solution

by:
Bob Learned earned 750 total points
ID: 24103090
The PrintPage event is called for each page of the PrintDocument.  You can't have local instances of counter variables, data readers, etc.  They need to be defined outside, at the module-level.
0
 
LVL 83

Expert Comment

by:CodeCruiser
ID: 24105294
>BTW, I am always open to a BETTER way if the PrintDocument is not how I should be doing simple reports.

If reports is what you are doing then why not use the Crystal Reports which come built in with the .NET?
0
 
LVL 1

Author Comment

by:dbote
ID: 24106287
I'm guessing that you are suggesting the ReportViewer control?
0
 
LVL 83

Assisted Solution

by:CodeCruiser
CodeCruiser earned 750 total points
ID: 24106357
The ReportViewer control displays reports but first you would need to design them.
Some links

http://www.codeproject.com/KB/cs/Crystal_Report.aspx
http://www.vbdotnetheaven.com/Articles/ArticleListing.aspx?SectionID=2&SubSectionID=105
0
 
LVL 1

Author Closing Comment

by:dbote
ID: 31567950
I appreciate the help, thank you.
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Well, all of us have seen the multiple EXCEL.EXE's in task manager that won't die even if you call the .close, .dispose methods. Try this method to kill any excels in memory. You can copy the kill function to create a check function and replace the …
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…

715 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question