Click here to Skip to main content
15,886,806 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
i am having problem to select range of pages or a single page for my program to print output data from my application. The application print all the pages whenever i want to print from the application.The code given below is working well but only printing all pages'
Thanks

What I have tried:

Dim strConnection As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data source=c:\Users\Public\COUNCILDATA.mdb;Jet OLEDB:Database Password=ibadanc1;"
       Dim cn As OleDb.OleDbConnection = New OleDb.OleDbConnection(strConnection)
       cn.Open()
       Dim da As New OleDb.OleDbCommand("select * from DEPTNAME_DATA where DEPT_NAME='" & (dep_name) & "'", cn)
       Dim dr As OleDb.OleDbDataReader = da.ExecuteReader()
       If dr.HasRows Then
           dr.Close()
           cn.Close()
           Dim printDialog1 As PrintDialog = New PrintDialog
           PrintDocument1.DefaultPageSettings.PaperSize = PrintDocument1.PrinterSettings.PaperSizes(1)
           PrintPreviewDialog1.ShowDialog()
           Dim result As DialogResult = printDialog1.ShowDialog
           If (result = Windows.Forms.DialogResult.OK) Then
               PrintDocument1.Print()
           End If
       Else
           MsgBox("IT SEEMS THE DEPARTMENT'S NAME IS NOT CORRECT")
       End If
Posted
Updated 11-Aug-20 11:14am
Comments
Richard Deeming 12-Aug-20 11:07am    
Dim da As New OleDb.OleDbCommand("select * from DEPTNAME_DATA where DEPT_NAME='" & (dep_name) & "'", cn)

Your code is vulnerable to SQL Injection[^]. NEVER use string concatenation to build a SQL query. ALWAYS use a parameterized query.

Everything you wanted to know about SQL injection (but were afraid to ask) | Troy Hunt[^]
How can I explain SQL injection without technical jargon? | Information Security Stack Exchange[^]
Query Parameterization Cheat Sheet | OWASP[^]

1 solution

If it prints all the pages, that's because that is what you told it to do: the PrintDocument.PrintPage Event (System.Drawing.Printing) | Microsoft Docs[^] gets called repeatedly until it sets the PrintPageEventArgs.HasMorePages to false.

The system has no idea what you do or don't want to print: it relies on you to decide that and set HasMorePages to false when you are done.
So if you want to print less info you need to change the PrintPage event handler code - which we can't see - to restrict it to "just the pages" you need it to.

Sorry, but we can't do that for you!
 
Share this answer
 
Comments
ashlytech 11-Aug-20 17:34pm    
when the PrintPageEventArgs.HasMorePages to false, i will see just a page of my output data in my printpreview. here is what i used to vyew all pages in printpreview:
If currow <= ds.Tables("ADMIN_SAL").Rows.Count - 1 Then
countd = 1
e.HasMorePages = True

Else
e.HasMorePages = False
currow = 0
End If
OriginalGriff 12-Aug-20 1:59am    
:sigh:
While HasMorePages is true, you will get another Event.
Instead of just setting it false on the first page, or doing what you are at the moment and just printing all data, it is your job to print only that data that you want output - which means filling a page, checking if there is more to print and setting it appropriately. Which means that if the user wants pages 2 & 3 only, you need to "skip" the data for page 1, and stop when you have printed the data for page 3.
The system doesn't count pages for you: it has no idea what you are trying to print on each page until it raises the PrintPage Event and you write some data on the graphics context.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900