Dataset data not displayed on Printpreview on second click of Preview button
Hi to all,
I have to preview dataset data using printdoc and my code is below.
Upon clicking the Preview button the preview dialog displays fine with data.
when I close the preview dialog then click the preview button the data is not diplayed.
weird behavior to me.
what should be the cause? Am I missing something?
Private Sub PrintDocument_PrintPage(ByVal sender As Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles PrintDocument.PrintPage
Dim PrintAreaHeight, PrintAreaWidth, marginLeft, marginRight, marginTop As Int32
With PrintDocument.DefaultPageSettings
' initializing local variables that contain the bounds of the printing area rectangle
PrintAreaHeight = .PaperSize.Height - .Margins.Top - .Margins.Bottom
PrintAreaWidth = .PaperSize.Width - .Margins.Left - .Margins.Right
' initializing local variables to hold margin values that will serve
' as the X and Y coordinates for the upper left corner of the printing
' area rectangle.
marginLeft = .Margins.Left
marginRight = .Margins.Right
marginTop = .Margins.Top
' X and Y coordinate
'.Landscape = True
End With
If PrintDocument.DefaultPageSettings.Landscape Then
Dim intTemp As Int32
intTemp = PrintAreaHeight
PrintAreaHeight = PrintAreaWidth
PrintAreaWidth = intTemp
' if the user selects landscape mode, swap the printing area height and width
End If
Dim TitleFont As Font = New Font("courier new", 24, FontStyle.Bold Or FontStyle.Underline)
Dim SubTitleFont As Font = New Font("courier new", 18, FontStyle.Bold)
Dim SubTitleFont1 As Font = New Font("courier new", 18, FontStyle.Bold Or FontStyle.Underline)
Dim BodyFont As Font = New Font("courier new", 12, FontStyle.Regular)
Dim BodyFont1 As Font = New Font("courier new", 14, FontStyle.Bold)
Dim str As String = "PAYMENTS BY CLASS - " & lblAcadyr.Text
' Get Invoice Data:
Dim InvSql As String
InvSql = "SELECT StudentID StudID,RollNo No,StudentNames Student ,Old_New, "
InvSql += "RegFee Reg,PTAFee PTA,OtherFee Other,TotalFee Total "
InvSql += "FROM tblStudents WHERE ClassID =" & cboClasses.Items(cboClasses.SelectedIndex)(0) & " ORDER BY RollNo"
Dim da As SqlDataAdapter = New SqlDataAdapter(InvSql, Conn)
Dim ds As New DataSet
da.Fill(ds, "tblStudents")
Dim LinesPerPage As Integer = Convert.ToInt32((e.MarginBounds.Height) / BodyFont.GetHeight(e.Graphics)) - 8
Dim LinesPrinted As Integer = 0
Static CentresPrinted As Integer = 0
Static StudentsPrinted As Integer = 0
Dim X, Y As Integer
Static pageno As Integer
X = e.MarginBounds.Left - 50
Y = e.MarginBounds.Top
If pageno = 0 Then 'put heading on first page
'centre the headertext and draw
Dim Xmid As Integer = Convert.ToSingle(e.PageBounds.Width / 2 - e.Graphics.MeasureString(str, TitleFont).Width / 2)
e.Graphics.DrawString(str, TitleFont, Brushes.Blue, Xmid, Y)
Y = Y + 50
'Get Classname
Dim Xmid1 As Integer = Convert.ToSingle(e.PageBounds.Width / 2 - e.Graphics.MeasureString("Class - " & cboClasses.Text, SubTitleFont).Width / 2)
e.Graphics.DrawString("Class - " & cboClasses.Text, SubTitleFont1, Brushes.Blue, Xmid1, Y)
Y = Y + 50
'column headers on each page
e.Graphics.DrawString("StudID", SubTitleFont, Brushes.Blue, X, Y)
X = X + 120
e.Graphics.DrawString("No", SubTitleFont, Brushes.Blue, X, Y)
X = X + 40
e.Graphics.DrawString("Student", SubTitleFont, Brushes.Blue, X, Y)
X = X + 190
e.Graphics.DrawString(" ", SubTitleFont, Brushes.Blue, X, Y)
X = X + 70
e.Graphics.DrawString("Reg.", SubTitleFont, Brushes.Blue, X, Y)
X = X + 80
e.Graphics.DrawString("PTA", SubTitleFont, Brushes.Blue, X, Y)
X = X + 70
e.Graphics.DrawString("Other", SubTitleFont, Brushes.Blue, X, Y)
X = X + 100
e.Graphics.DrawString("Total", SubTitleFont, Brushes.Blue, X, Y)
LinesPrinted = 3
Y = Y + 20
Else
Y = Y - FontHeight - 40 'shift startpoint upwards
'column headers on each page
e.Graphics.DrawString("StudID", SubTitleFont, Brushes.Blue, X, Y)
X = X + 120
e.Graphics.DrawString("No", SubTitleFont, Brushes.Blue, X, Y)
X = X + 40
e.Graphics.DrawString("Student", SubTitleFont, Brushes.Blue, X, Y)
X = X + 190
e.Graphics.DrawString(" ", SubTitleFont, Brushes.Blue, X, Y)
X = X + 70
e.Graphics.DrawString("Reg.", SubTitleFont, Brushes.Blue, X, Y)
X = X + 80
e.Graphics.DrawString("PTA", SubTitleFont, Brushes.Blue, X, Y)
X = X + 70
e.Graphics.DrawString("Other", SubTitleFont, Brushes.Blue, X, Y)
X = X + 100
e.Graphics.DrawString("Total", SubTitleFont, Brushes.Blue, X, Y)
LinesPrinted = 3
Y = Y + 20
End If
Dim Xleft As Integer
Dim FieldValue As String
Do While (LinesPrinted < LinesPerPage) And (StudentsPrinted <= ds.Tables(0).Rows.Count - 1)
'body
Y = Y + FontHeight + 8
Xleft = e.MarginBounds.Left - 50
FieldValue = ds.Tables(0).Rows(StudentsPrinted)("StudID").ToString
e.Graphics.DrawString(FieldValue, BodyFont, Brushes.Black, Xleft, Y)
Xleft = Xleft + 40
FieldValue = ds.Tables(0).Rows(StudentsPrinted)("Student").ToString
If (FieldValue.Length > 17) Then
FieldValue = FieldValue.Remove(17, FieldValue.Length - 17)
End If
e.Graphics.DrawString(FieldValue, BodyFont, Brushes.Black, Xleft, Y)
Xleft = Xleft + 190
FieldValue = ds.Tables(0).Rows(StudentsPrinted)("Old_New").ToString
If FieldValue = String.Empty Then
e.Graphics.DrawString(" ", BodyFont, Brushes.Blue, Xleft, Y)
Else
e.Graphics.DrawString("[" & FieldValue & "]", BodyFont, Brushes.Blue, Xleft, Y)
End If
Xleft = Xleft + 70
FieldValue = ds.Tables(0).Rows(StudentsPrinted)("Reg").ToString
If FieldValue = String.Empty Then
FieldValue = " "
Else
FieldValue = CInt(FieldValue).ToString("n0")
End If
e.Graphics.DrawString(FieldValue, BodyFont, Brushes.Black, Xleft, Y)
Xleft = Xleft + 80
FieldValue = ds.Tables(0).Rows(StudentsPrinted)("PTA").ToString
If FieldValue = String.Empty Then
FieldValue = " "
Else
FieldValue = CInt(FieldValue).ToString("n0")
End If
e.Graphics.DrawString(FieldValue, BodyFont, Brushes.Black, Xleft, Y)
Xleft = Xleft + 70
FieldValue = ds.Tables(0).Rows(StudentsPrinted)("Other").ToString
If FieldValue = String.Empty Then
FieldValue = " "
Else
FieldValue = CInt(FieldValue).ToString("n0")
End If
e.Graphics.DrawString(FieldValue, BodyFont, Brushes.Black, Xleft, Y)
Xleft = Xleft + 100
FieldValue = ds.Tables(0).Rows(StudentsPrinted)("Total").ToString
If FieldValue = String.Empty Then
FieldValue = " "
Else
FieldValue = CInt(FieldValue).ToString("n0")
End If
e.Graphics.DrawString(FieldValue, BodyFont, Brushes.Black, Xleft, Y)
LinesPrinted = LinesPrinted + 1
StudentsPrinted = StudentsPrinted + 1
Y = Y
Loop
End Sub
Private Sub btnPreview_Click(sender As System.Object, e As System.EventArgs) Handles btnPreview.Click
If cboClasses.Text = String.Empty Then
MsgBox("Select Class.", MsgBoxStyle.Critical, "")
Exit Sub
Else
With PrintPreviewDialog
.Document = PrintDocument
.StartPosition = FormStartPosition.CenterScreen
.WindowState = FormWindowState.Maximized
.ShowDialog()
End With
End If
End Sub
Re: Dataset data not displayed on Printpreview on second click of Preview button
As for the question, have you debugged the code? If code doesn't work then of course the first step is to read it in case there's something obvious amiss but, if you don't see anything, the next step is to watch the code in action. Place a breakpoint on the first line and then, when execution breaks, step through the code line by line. At each step, use the Autos, Locals, Watch and Immediate windows plus more to evaluate the current state of the app. As soon as that state is not what you expect, you've found an issue. Even if you still can't solve it, at least you point us, who don't have the project or the data, in the right direction.
Re: Dataset data not displayed on Printpreview on second click of Preview button
Originally Posted by gbhs
Code runs perfectly but on second click of preview button, data from dataset is not displayed.
Is it because I closed dataset?
Debug the code. If it's the second time through that is the issue then only step through the code the second time. It will most likely because some variable has retained a value from the first run through but stuff like that is hard to spot just by reading the code. That's why you need to debug it. Debugging is an integral part of software development so don't try to avoid it.
Re: Dataset data not displayed on Printpreview on second click of Preview button
Well Thanks to all.
As a newbie ,
Here is a small sample attachment of what I want.
As a newbie in VB.net an example with statements will help me understand how to use printpage,printpreview dialog,beginprint,endprint,hasmorepages in previewing data from a dataset.
Can someone help me with a simple code for this.
Thanks in advance
Re: Dataset data not displayed on Printpreview on second click of Preview button
Originally Posted by gbhs
Can someone help me with a simple code for this.
Go to the VB.NET CodeBank forum and sort by Thread Starter, then page until you find threads started by the very same Merrion who posted above. Among them you will find a beginners guide to printing.