Solved

How to Pass DataTable Content to a StreamReader?

Posted on 2011-09-07
3
423 Views
Last Modified: 2012-05-12
I need to print a report directly from a DataTable to stream to a printer. What I am missing is how to get the table's content into a stream to send to the printer and print each row on a new line.

            Dim da As New SqlServerCe.SqlCeDataAdapter(strQuery, gc_ConnString)
            Dim TableReport As New DataTable

            da.Fill(TableReport)
            strReport = TableReport.Rows.ToString()
            streamToPrint = New StreamReader(<table content>)

Thank you for any advice or solution.
Imports System
Imports System.IO
Imports System.Drawing
Imports System.Drawing.Printing

Public Class WriteReport
    Dim clsErrLog As New ErrorLog
    Private components As System.ComponentModel.Container
    Private printFont As Font
    Private streamToPrint As StreamReader
    Dim strReport As String

    Public Sub Print(ByRef strQuery As String)
        Try
            Dim da As New SqlServerCe.SqlCeDataAdapter(strQuery, gc_ConnString)
            Dim TableReport As New DataTable

            da.Fill(TableReport)
            strReport = TableReport.Rows.ToString()
            streamToPrint = New StreamReader(<datatable content>)  <<

            Try
                printFont = New Font("Arial", 10)
            Dim pd As New PrintDocument()
            AddHandler pd.PrintPage, AddressOf Me.pd_PrintPage
            pd.Print()
        Finally
            streamToPrint.Close()
        End Try
        Catch ex As Exception
            clsErrLog.LogError(ex.Message, "WriteReport.Print")
        End Try
    End Sub

    Private Sub pd_PrintPage(ByVal sender As Object, ByVal ev As PrintPageEventArgs)
        Dim linesPerPage As Single = 0
        Dim yPos As Single = 0
        Dim count As Integer = 0
        Dim leftMargin As Single = ev.MarginBounds.Left
        Dim topMargin As Single = ev.MarginBounds.Top
        Dim line As String = Nothing

        linesPerPage = ev.MarginBounds.Height / printFont.GetHeight(ev.Graphics)

        While count < linesPerPage
            line = streamToPrint.ReadLine()
            If line Is Nothing Then
                Exit While
            End If
            yPos = topMargin + count * printFont.GetHeight(ev.Graphics)
            ev.Graphics.DrawString(line, printFont, Brushes.Black, leftMargin, yPos, New StringFormat())
            count += 1
        End While

        If (line IsNot Nothing) Then
            ev.HasMorePages = True
        Else
            ev.HasMorePages = False
        End If
    End Sub
End Class

Open in new window

0
Comment
Question by:bobcann
[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
3 Comments
 
LVL 75

Accepted Solution

by:
käµfm³d   👽 earned 250 total points
ID: 36499130
The StreamReader constructor expects either a filename or an existing Stream object where the data will be written. If you want to dump out your DataTable, then you'll need to loop over the rows and use the WriteLine method (or one of the other Write methods) to output the data.
0
 
LVL 83

Assisted Solution

by:CodeCruiser
CodeCruiser earned 250 total points
ID: 36499152
You dont have to use stream. You can merge datatable column values into the line object. For example

For i As integer = 0 to dTable.Rows.Count - 1
   msgbox dTable.Rows(i).Item(0) & ", " & dTable.Rows(i).Item(1)...
Next

To print to next line, you have to increment the y position in the DrawString method.

Example

http://www.codeproject.com/KB/printing/datagridprinter.aspx
0
 

Author Comment

by:bobcann
ID: 36505632
Thanks guys. This was a huge help.

All I need to do is:
Dim daReport As New SqlServerCe.SqlCeDataAdapter(strQuery, gc_ConnString)
    daReport.Fill(dtReport)
    drReport = dtReport.Rows

    For Each drRow As DataRow In drReport
	line = Pad(drRow.Item("WONUMBER").ToString, " ", 30, ppPaddingPlacement.ppTrailing)
        line = line + Pad(drRow.Item("WOADDRESS").ToString, " ", 30, ppPaddingPlacement.ppTrailing)
        line = line + Pad(drRow.Item("DESCRIPTION").ToString, " ", 46, ppPaddingPlacement.ppTrailing)
        line = line + drRow.Item("STREETNAME").ToString 

	yPos = topMargin + count * printFont.GetHeight(ev.Graphics)
        ev.Graphics.DrawString(line, printFont, Brushes.Black, leftMargin, yPos, New StringFormat())
        count += 1
    Next

Open in new window

0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

This tutorial demonstrates one way to create an application that runs without any Forms but still has a GUI presence via an Icon in the System Tray. The magic lies in Inheriting from the ApplicationContext Class and passing that to Application.Ru…
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…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …

730 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