Results 1 to 8 of 8

Thread: Dataset data not displayed on Printpreview on second click of Preview button

  1. #1

    Thread Starter
    Member
    Join Date
    Aug 2014
    Posts
    33

    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 + 120
    FieldValue = ds.Tables(0).Rows(StudentsPrinted)("No").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

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,299

    Re: Dataset data not displayed on Printpreview on second click of Preview button

    Firstly, please format your posted code snippets. They are two hard to read otherwise, especially when long. Here's what your code should look like:
    vb.net Code:
    1. Private Sub PrintDocument_PrintPage(ByVal sender As Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles PrintDocument.PrintPage
    2.  
    3.         Dim PrintAreaHeight, PrintAreaWidth, marginLeft, marginRight, marginTop As Int32
    4.         With PrintDocument.DefaultPageSettings
    5.             ' initializing local variables that contain the bounds of the printing area rectangle
    6.             PrintAreaHeight = .PaperSize.Height - .Margins.Top - .Margins.Bottom
    7.             PrintAreaWidth = .PaperSize.Width - .Margins.Left - .Margins.Right
    8.             ' initializing local variables to hold margin values that will serve
    9.             ' as the X and Y coordinates for the upper left corner of the printing
    10.             ' area rectangle.
    11.             marginLeft = .Margins.Left
    12.             marginRight = .Margins.Right
    13.             marginTop = .Margins.Top
    14.             ' X and Y coordinate
    15.             '.Landscape = True
    16.         End With
    17.         If PrintDocument.DefaultPageSettings.Landscape Then
    18.             Dim intTemp As Int32
    19.             intTemp = PrintAreaHeight
    20.             PrintAreaHeight = PrintAreaWidth
    21.             PrintAreaWidth = intTemp
    22.             ' if the user selects landscape mode, swap the printing area height and width
    23.         End If
    24.  
    25.         Dim TitleFont As Font = New Font("courier new", 24, FontStyle.Bold Or FontStyle.Underline)
    26.         Dim SubTitleFont As Font = New Font("courier new", 18, FontStyle.Bold)
    27.         Dim SubTitleFont1 As Font = New Font("courier new", 18, FontStyle.Bold Or FontStyle.Underline)
    28.         Dim BodyFont As Font = New Font("courier new", 12, FontStyle.Regular)
    29.         Dim BodyFont1 As Font = New Font("courier new", 14, FontStyle.Bold)
    30.  
    31.         Dim str As String = "PAYMENTS BY CLASS - " & lblAcadyr.Text
    32.  
    33.         ' Get Invoice Data:
    34.         Dim InvSql As String
    35.         InvSql = "SELECT StudentID StudID,RollNo No,StudentNames  Student ,Old_New, "
    36.         InvSql += "RegFee Reg,PTAFee PTA,OtherFee Other,TotalFee Total  "
    37.         InvSql += "FROM tblStudents WHERE ClassID =" & cboClasses.Items(cboClasses.SelectedIndex)(0) & " ORDER BY RollNo"
    38.         Dim da As SqlDataAdapter = New SqlDataAdapter(InvSql, Conn)
    39.         Dim ds As New DataSet
    40.         da.Fill(ds, "tblStudents")
    41.  
    42.         Dim LinesPerPage As Integer = Convert.ToInt32((e.MarginBounds.Height) / BodyFont.GetHeight(e.Graphics)) - 8
    43.         Dim LinesPrinted As Integer = 0
    44.  
    45.         Static CentresPrinted As Integer = 0
    46.         Static StudentsPrinted As Integer = 0
    47.  
    48.         Dim X, Y As Integer
    49.         Static pageno As Integer
    50.  
    51.         X = e.MarginBounds.Left - 50
    52.         Y = e.MarginBounds.Top
    53.  
    54.         If pageno = 0 Then 'put heading on first page
    55.             'centre the headertext and draw
    56.             Dim Xmid As Integer = Convert.ToSingle(e.PageBounds.Width / 2 - e.Graphics.MeasureString(str, TitleFont).Width / 2)
    57.             e.Graphics.DrawString(str, TitleFont, Brushes.Blue, Xmid, Y)
    58.             Y = Y + 50
    59.             'Get Classname
    60.             Dim Xmid1 As Integer = Convert.ToSingle(e.PageBounds.Width / 2 - e.Graphics.MeasureString("Class - " & cboClasses.Text, SubTitleFont).Width / 2)
    61.             e.Graphics.DrawString("Class - " & cboClasses.Text, SubTitleFont1, Brushes.Blue, Xmid1, Y)
    62.             Y = Y + 50
    63.  
    64.             'column headers on each page
    65.             e.Graphics.DrawString("StudID", SubTitleFont, Brushes.Blue, X, Y)
    66.  
    67.             X = X + 120
    68.             e.Graphics.DrawString("No", SubTitleFont, Brushes.Blue, X, Y)
    69.  
    70.             X = X + 40
    71.             e.Graphics.DrawString("Student", SubTitleFont, Brushes.Blue, X, Y)
    72.  
    73.             X = X + 190
    74.             e.Graphics.DrawString("   ", SubTitleFont, Brushes.Blue, X, Y)
    75.  
    76.             X = X + 70
    77.             e.Graphics.DrawString("Reg.", SubTitleFont, Brushes.Blue, X, Y)
    78.  
    79.             X = X + 80
    80.             e.Graphics.DrawString("PTA", SubTitleFont, Brushes.Blue, X, Y)
    81.  
    82.             X = X + 70
    83.             e.Graphics.DrawString("Other", SubTitleFont, Brushes.Blue, X, Y)
    84.  
    85.             X = X + 100
    86.             e.Graphics.DrawString("Total", SubTitleFont, Brushes.Blue, X, Y)
    87.  
    88.             LinesPrinted = 3
    89.             Y = Y + 20
    90.         Else
    91.             Y = Y - FontHeight - 40   'shift startpoint upwards
    92.  
    93.             'column headers on each page
    94.             e.Graphics.DrawString("StudID", SubTitleFont, Brushes.Blue, X, Y)
    95.  
    96.             X = X + 120
    97.             e.Graphics.DrawString("No", SubTitleFont, Brushes.Blue, X, Y)
    98.  
    99.             X = X + 40
    100.             e.Graphics.DrawString("Student", SubTitleFont, Brushes.Blue, X, Y)
    101.  
    102.             X = X + 190
    103.             e.Graphics.DrawString("   ", SubTitleFont, Brushes.Blue, X, Y)
    104.  
    105.             X = X + 70
    106.             e.Graphics.DrawString("Reg.", SubTitleFont, Brushes.Blue, X, Y)
    107.  
    108.             X = X + 80
    109.             e.Graphics.DrawString("PTA", SubTitleFont, Brushes.Blue, X, Y)
    110.  
    111.             X = X + 70
    112.             e.Graphics.DrawString("Other", SubTitleFont, Brushes.Blue, X, Y)
    113.  
    114.             X = X + 100
    115.             e.Graphics.DrawString("Total", SubTitleFont, Brushes.Blue, X, Y)
    116.  
    117.             LinesPrinted = 3
    118.             Y = Y + 20
    119.         End If
    120.  
    121.  
    122.         Dim Xleft As Integer
    123.         Dim FieldValue As String
    124.         Do While (LinesPrinted < LinesPerPage) And (StudentsPrinted <= ds.Tables(0).Rows.Count - 1)
    125.             'body
    126.             Y = Y + FontHeight + 8
    127.             Xleft = e.MarginBounds.Left - 50
    128.             FieldValue = ds.Tables(0).Rows(StudentsPrinted)("StudID").ToString
    129.             e.Graphics.DrawString(FieldValue, BodyFont, Brushes.Black, Xleft, Y)
    130.  
    131.             Xleft = Xleft + 120
    132.             FieldValue = ds.Tables(0).Rows(StudentsPrinted)("No").ToString
    133.             e.Graphics.DrawString(FieldValue, BodyFont, Brushes.Black, Xleft, Y)
    134.  
    135.             Xleft = Xleft + 40
    136.             FieldValue = ds.Tables(0).Rows(StudentsPrinted)("Student").ToString
    137.             If (FieldValue.Length > 17) Then
    138.                 FieldValue = FieldValue.Remove(17, FieldValue.Length - 17)
    139.             End If
    140.             e.Graphics.DrawString(FieldValue, BodyFont, Brushes.Black, Xleft, Y)
    141.  
    142.             Xleft = Xleft + 190
    143.             FieldValue = ds.Tables(0).Rows(StudentsPrinted)("Old_New").ToString
    144.             If FieldValue = String.Empty Then
    145.                 e.Graphics.DrawString("     ", BodyFont, Brushes.Blue, Xleft, Y)
    146.             Else
    147.                 e.Graphics.DrawString("[" & FieldValue & "]", BodyFont, Brushes.Blue, Xleft, Y)
    148.             End If
    149.  
    150.             Xleft = Xleft + 70
    151.             FieldValue = ds.Tables(0).Rows(StudentsPrinted)("Reg").ToString
    152.             If FieldValue = String.Empty Then
    153.                 FieldValue = "      "
    154.             Else
    155.                 FieldValue = CInt(FieldValue).ToString("n0")
    156.             End If
    157.             e.Graphics.DrawString(FieldValue, BodyFont, Brushes.Black, Xleft, Y)
    158.  
    159.             Xleft = Xleft + 80
    160.             FieldValue = ds.Tables(0).Rows(StudentsPrinted)("PTA").ToString
    161.             If FieldValue = String.Empty Then
    162.                 FieldValue = "      "
    163.             Else
    164.                 FieldValue = CInt(FieldValue).ToString("n0")
    165.             End If
    166.             e.Graphics.DrawString(FieldValue, BodyFont, Brushes.Black, Xleft, Y)
    167.  
    168.             Xleft = Xleft + 70
    169.             FieldValue = ds.Tables(0).Rows(StudentsPrinted)("Other").ToString
    170.             If FieldValue = String.Empty Then
    171.                 FieldValue = "      "
    172.             Else
    173.                 FieldValue = CInt(FieldValue).ToString("n0")
    174.             End If
    175.             e.Graphics.DrawString(FieldValue, BodyFont, Brushes.Black, Xleft, Y)
    176.  
    177.             Xleft = Xleft + 100
    178.             FieldValue = ds.Tables(0).Rows(StudentsPrinted)("Total").ToString
    179.             If FieldValue = String.Empty Then
    180.                 FieldValue = "      "
    181.             Else
    182.                 FieldValue = CInt(FieldValue).ToString("n0")
    183.             End If
    184.             e.Graphics.DrawString(FieldValue, BodyFont, Brushes.Black, Xleft, Y)
    185.  
    186.  
    187.             LinesPrinted = LinesPrinted + 1
    188.             StudentsPrinted = StudentsPrinted + 1
    189.             Y = Y
    190.         Loop
    191.     End Sub
    192.     Private Sub btnPreview_Click(sender As System.Object, e As System.EventArgs) Handles btnPreview.Click
    193.         If cboClasses.Text = String.Empty Then
    194.             MsgBox("Select Class.", MsgBoxStyle.Critical, "")
    195.             Exit Sub
    196.         Else
    197.             With PrintPreviewDialog
    198.                 .Document = PrintDocument
    199.                 .StartPosition = FormStartPosition.CenterScreen
    200.                 .WindowState = FormWindowState.Maximized
    201.                 .ShowDialog()
    202.             End With
    203.         End If
    204.     End Sub

  3. #3
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,299

    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.

  4. #4

    Thread Starter
    Member
    Join Date
    Aug 2014
    Posts
    33

    Re: Dataset data not displayed on Printpreview on second click of Preview button

    Code runs perfectly but on second click of preview button, data from dataset is not displayed.
    Is it because I closed dataset?

  5. #5
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,299

    Re: Dataset data not displayed on Printpreview on second click of Preview button

    Quote Originally Posted by gbhs View Post
    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.

  6. #6
    PowerPoster
    Join Date
    Jul 2002
    Location
    Dublin, Ireland
    Posts
    2,148

    Re: Dataset data not displayed on Printpreview on second click of Preview button

    You need to re-initialise these two variables every time the print button is clicked....so that it starts at the start again.


    VB Code:
    1. Static CentresPrinted As Integer = 0
    2.  Static StudentsPrinted As Integer = 0

  7. #7

    Thread Starter
    Member
    Join Date
    Aug 2014
    Posts
    33

    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
    Attached Files Attached Files

  8. #8
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,299

    Re: Dataset data not displayed on Printpreview on second click of Preview button

    Quote Originally Posted by gbhs View Post
    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.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width