e.HasMorePages Error in my Code

tpra21

Active member
Joined
Oct 21, 2006
Messages
26
Programming Experience
1-3
I have the following code (I apologize for the lack of indenting, the pasting always takes away the formatting):

VB.NET:
[SIZE=2][/SIZE][SIZE=2][COLOR=#0000ff]Private[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE][SIZE=2] PrintDocument1_PrintPage([/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] sender [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]Object[/COLOR][/SIZE][SIZE=2], [/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] e [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] System.Drawing.Printing.PrintPageEventArgs) [/SIZE][SIZE=2][COLOR=#0000ff]Handles[/COLOR][/SIZE][SIZE=2] PrintDocument1.PrintPage
[/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] dailyTransFile [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]String[/COLOR][/SIZE][SIZE=2] = "C:\Program Files\LakeviewFarms\MobileTransactions\dailytrans.csv"
[/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] dailyTransFileStream [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]New[/COLOR][/SIZE][SIZE=2] FileStream(dailyTransFile, _
FileMode.Open, FileAccess.Read)
[/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] DTReader [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]New[/COLOR][/SIZE][SIZE=2] StreamReader(dailyTransFileStream)
[/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] todaysDateTime [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]Date[/COLOR][/SIZE][SIZE=2] = DateTime.Now
[/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] cx [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]Integer[/COLOR][/SIZE][SIZE=2] = 0
[/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] caseTotal [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]Integer[/COLOR][/SIZE][SIZE=2] = 0
[/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] itemCaseTotal [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]Integer[/COLOR][/SIZE][SIZE=2] = 0
[/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] itemTotal [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]Integer[/COLOR][/SIZE][SIZE=2] = 0
[/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] route1 [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]String[/COLOR][/SIZE][SIZE=2] = ""
[/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] grandTotal [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]Integer[/COLOR][/SIZE][SIZE=2] = 0
[/SIZE][SIZE=2][COLOR=#008000]'Read the report detail
[/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] holdrec(), rec1 [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]String[/COLOR][/SIZE]
[SIZE=2][COLOR=#0000ff] 
[/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#0000ff]If[/COLOR][/SIZE][SIZE=2] File.Exists(dailyTransFile) [/SIZE][SIZE=2][COLOR=#0000ff]Then
[/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#008000]'Header 1
[/COLOR][/SIZE][SIZE=2]e.Graphics.DrawString(Space(10) & "Case Quantity Sold by Route" & Space(10) & todaysDateTime, _
[/SIZE][SIZE=2][COLOR=#0000ff]New[/COLOR][/SIZE][SIZE=2] Font("courier new", 10, FontStyle.Regular), Brushes.Black, 10, 10)
[/SIZE][SIZE=2][COLOR=#008000]'Header 2
[/COLOR][/SIZE][SIZE=2]e.Graphics.DrawString("Route" & Space(5) & "Item" & Space(3) & _
"Description" & Space(25) & "Case Qty", _
[/SIZE][SIZE=2][COLOR=#0000ff]New[/COLOR][/SIZE][SIZE=2] Font("courier new", 10, FontStyle.Regular), Brushes.Black, 10, 40)
[/SIZE][SIZE=2][COLOR=#008000]'Header 3
[/COLOR][/SIZE][SIZE=2]e.Graphics.DrawString("_____" & Space(5) & "____" & Space(3) & _
"___________" & Space(25) & "________", _
[/SIZE][SIZE=2][COLOR=#0000ff]New[/COLOR][/SIZE][SIZE=2] Font("courier new", 10, FontStyle.Regular), Brushes.Black, 10, 50)
lineCount = 4 [/SIZE][SIZE=2][COLOR=#008000]'line count
[/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#008000]'Horizontal
[/COLOR][/SIZE][SIZE=2]xPos = 10
[/SIZE][SIZE=2][COLOR=#008000]'Vertical
[/COLOR][/SIZE][SIZE=2]yPos = 70
rec1 = DTReader.ReadLine
 
[/SIZE][SIZE=2][COLOR=#0000ff]Do[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]While[/COLOR][/SIZE][SIZE=2] DTReader.Peek <> -1
holdrec = Split(rec1, ",")
route1 = holdrec(1).ToString.Substring(0, 1)
records(cx) = [/SIZE][SIZE=2][COLOR=#0000ff]New[/COLOR][/SIZE][SIZE=2] DailyTransaction(route1, holdrec(2), holdrec(3), holdrec(5))
rec1 = DTReader.ReadLine
cx += 1
[/SIZE][SIZE=2][COLOR=#0000ff]Loop[/COLOR][/SIZE]
[SIZE=2][COLOR=#0000ff] 
[/COLOR][/SIZE][SIZE=2]holdrec = Split(rec1, ",")
route1 = holdrec(1).ToString.Substring(0, 1)
records(cx) = [/SIZE][SIZE=2][COLOR=#0000ff]New[/COLOR][/SIZE][SIZE=2] DailyTransaction(route1, holdrec(2), holdrec(3), holdrec(5))
DTReader.Close()
[/SIZE][SIZE=2][COLOR=#008000]'Resize the array to avoid null value
[/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#0000ff]ReDim[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]Preserve[/COLOR][/SIZE][SIZE=2] records(cx)
[/SIZE][SIZE=2][COLOR=#008000]' Make the proper comparer.
[/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] DT_comparer [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]New[/COLOR][/SIZE][SIZE=2] DailyTransComparer([/SIZE][SIZE=2][COLOR=#0000ff]True[/COLOR][/SIZE][SIZE=2])
[/SIZE][SIZE=2][COLOR=#008000]' Sort the array.
[/COLOR][/SIZE][SIZE=2]Array.Sort(records, DT_comparer)
[/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] routeHold [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]String[/COLOR][/SIZE][SIZE=2] = ""
[/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] itemHold [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]Integer[/COLOR][/SIZE][SIZE=2] = 0
[/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] routeTotal [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]Integer[/COLOR][/SIZE][SIZE=2] = 0
[/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] ItemTot [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]Integer[/COLOR][/SIZE][SIZE=2] = 0[/SIZE]
[SIZE=2] 
[/SIZE][SIZE=2][COLOR=#0000ff]For[/COLOR][/SIZE][SIZE=2] cx = 0 [/SIZE][SIZE=2][COLOR=#0000ff]To[/COLOR][/SIZE][SIZE=2] records.GetUpperBound(0)[/SIZE]
[SIZE=2] 
[/SIZE][COLOR=red][SIZE=2]If[/SIZE][SIZE=2] lineCount >= 67 [/SIZE][SIZE=2]Then
[/SIZE][/COLOR][SIZE=2][COLOR=red]lineCount = 4 [/COLOR][/SIZE][SIZE=2][COLOR=red]'line count[/COLOR]
[/SIZE][SIZE=2][COLOR=red]e.HasMorePages = [/COLOR][/SIZE][SIZE=2][COLOR=red]True[/COLOR][/SIZE][SIZE=2]
[/SIZE][SIZE=2][/SIZE][COLOR=red][SIZE=2]End[/SIZE][SIZE=2] [/SIZE][SIZE=2]If[/SIZE][/COLOR]
[SIZE=2][COLOR=#0000ff] 
[/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#0000ff]If[/COLOR][/SIZE][SIZE=2] (itemHold <> records(cx).item) [/SIZE][SIZE=2][COLOR=#0000ff]And[/COLOR][/SIZE][SIZE=2] itemHold <> 0 [/SIZE][SIZE=2][COLOR=#0000ff]Then
[/COLOR][/SIZE][SIZE=2]e.Graphics.DrawString(records(cx - 1).route.PadRight(10) & records(cx - 1).item.ToString.PadRight(5) & Space(3) & _
records(cx - 1).descrip.PadRight(35) & Space(5) & itemCaseTotal, _
[/SIZE][SIZE=2][COLOR=#0000ff]New[/COLOR][/SIZE][SIZE=2] Font("courier new", 10, FontStyle.Regular), Brushes.Black, xPos, yPos)
[/SIZE][SIZE=2][COLOR=#008000]'Update the vertical position of the next line to be printed
[/COLOR][/SIZE][SIZE=2]yPos = yPos + 15
lineCount += 1
itemCaseTotal = 0
[/SIZE][SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]If[/COLOR][/SIZE]
[SIZE=2][COLOR=#0000ff] 
[/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#008000]'Route Break[/COLOR][/SIZE][SIZE=2][COLOR=#008000]
[/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#0000ff]If[/COLOR][/SIZE][SIZE=2] (routeHold <> records(cx).route) [/SIZE][SIZE=2][COLOR=#0000ff]And[/COLOR][/SIZE][SIZE=2] (routeHold <> "") [/SIZE][SIZE=2][COLOR=#0000ff]Then
[/COLOR][/SIZE][SIZE=2]yPos += 15
lineCount += 1
e.Graphics.DrawString("Route Total: " & Space(5) & routeTotal, _
[/SIZE][SIZE=2][COLOR=#0000ff]New[/COLOR][/SIZE][SIZE=2] Font("courier new", 10, FontStyle.Regular), Brushes.Black, xPos, yPos)
yPos += 30
lineCount += 2
routeTotal = 0
[/SIZE][SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]If[/COLOR][/SIZE]
[SIZE=2][COLOR=#0000ff] 
[/COLOR][/SIZE][SIZE=2]itemCaseTotal += records(cx).cases
[/SIZE][SIZE=2][COLOR=#008000]'Total up the cases
[/COLOR][/SIZE][SIZE=2]routeTotal += records(cx).cases
caseTotal += records(cx).cases
grandTotal += records(cx).cases
itemHold = records(cx).item
routeHold = records(cx).route
 
[/SIZE][SIZE=2][COLOR=#0000ff]Next[/COLOR][/SIZE][SIZE=2] cx
[/SIZE][SIZE=2][COLOR=#008000]'Froce last item/route totals
[/COLOR][/SIZE][SIZE=2]e.Graphics.DrawString(records(cx - 1).route.PadRight(10) & records(cx - 1).item.ToString.PadRight(5) & Space(3) & _
records(cx - 1).descrip.PadRight(35) & Space(5) & itemCaseTotal, _
[/SIZE][SIZE=2][COLOR=#0000ff]New[/COLOR][/SIZE][SIZE=2] Font("courier new", 10, FontStyle.Regular), Brushes.Black, xPos, yPos)
[/SIZE][SIZE=2][COLOR=#008000]'Update the vertical position of the next line to be printed
[/COLOR][/SIZE][SIZE=2]yPos = yPos + 30
lineCount += 1
[/SIZE][SIZE=2][COLOR=#008000]'Froce last item/route totals
[/COLOR][/SIZE][SIZE=2]e.Graphics.DrawString("Route Total: " & Space(5) & routeTotal, _
[/SIZE][SIZE=2][COLOR=#0000ff]New[/COLOR][/SIZE][SIZE=2] Font("courier new", 10, FontStyle.Regular), Brushes.Black, xPos, yPos)
yPos += 15
lineCount += 1
routeTotal = 0
itemCaseTotal = 0
[/SIZE][SIZE=2][COLOR=#008000]'Print the case total at end
[/COLOR][/SIZE][SIZE=2]yPos += 15
lineCount += 1
e.Graphics.DrawString("Grand Total: " & Space(5) & grandTotal, _
[/SIZE][SIZE=2][COLOR=#0000ff]New[/COLOR][/SIZE][SIZE=2] Font("courier new", 10, FontStyle.Regular), Brushes.Black, xPos, yPos)
[/SIZE][SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]If
[/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]Sub
[/COLOR][/SIZE]

When my code hits the HasMorePages variable, for some reason it will run my printDocument routine infinately and print tons of pages. i know I have a coding error, but I cannot figure out where. Also, I have tried reseting the hasMorePages variable to false after it is set to true, but then my report only prints one page.

Thanks, Adam
 
Too much is happening in that sub method. For example getting the data into your records array does not belong in the printpage event handler.

Setting e.HasMorePages does only set a Boolean value. When the printpage event handler method is finished the HasMorePages boolean value is checked, if it is True then that same sub is processed again, this happens again and again until your logic tells HasMorePages that there aren't any more pages to print.
 
Back
Top