Printing Data Read from a Text File

skylabnn

Member
Joined
Nov 11, 2009
Messages
10
Programming Experience
1-3
I did this and it worked. Please Is there anyway to print all the records to a dot matrix printer directly?

Dim field1(15) As Char
Dim field3(11) As Char

Dim field2 As String

Using reader As New StreamReader("C:\reader.txt")
Do Until reader.EndOfStream
reader.Read(field1, 0, field1.Length)
reader.Read(field3, 0, field3.Length)

field2 = reader.ReadLine()

MessageBox.Show(field3, field1)
Loop
End Using
 
Not sure how big your file may be but one option is you can read the file into a dataset and then pass the dataset to a crystal report for printing.
 
See if this works for you
VB.NET:
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim PrintTextFile As New ProcessStartInfo
        PrintTextFile.UseShellExecute = True
        PrintTextFile.Verb = "print"
        PrintTextFile.WindowStyle = ProcessWindowStyle.Hidden
        PrintTextFile.FileName = "C:\reader.txt"
        Process.Start(PrintTextFile)
    End Sub
 
Ok I have a question, are these values going to be stored into a database? Or are you simply reading the entire file into another file for printing rather then just printing the original source?

As shown here & in your other thread, there are many options for opening & reading a file. You can alter any of these methods to add the data to a DataSet/DataTable.

I would suggest starting by creating a Typed dataset & datatable that matches the columns of data in your data. You can then create records to be added to the datatable as you read each of the lines of data. If you can pass the dataset as a source to a crystal report and/or insert the data into you database.

Below is an example of filling the data into a dataset.

VB.NET:
[COLOR="SeaGreen"]'Form Level Variable[/COLOR]
[COLOR="Blue"]Dim[/COLOR] m_dsData [COLOR="blue"]As New [/COLOR]YouTypedDataSetName

[COLOR="blue"]Using [/COLOR]Reader [COLOR="blue"]As New [/COLOR]TextFieldParser([COLOR="DarkRed"]"C:\TestFolder\test.log"[/COLOR])
   [COLOR="blue"]Dim[/COLOR] astrCurrentRow [COLOR="blue"]As String()[/COLOR] = Nothing
   [COLOR="blue"]Dim [/COLOR]rowData [COLOR="blue"]As DataRow [/COLOR]= Nothing

   Reader.TextFieldType = FieldType.FixedWidth
   Reader.SetFieldWidths(16, 12)

   [COLOR="blue"]While Not[/COLOR] Reader.EndOfData
         astrCurrentRow = Reader.ReadFields()
         
         rowData = m_dsData.Tables([COLOR="darkred"]"YourTableNameHere"[/COLOR]).NewRow
         rowData([COLOR="darkred"]"Column1NameHere"[/COLOR]) = astrCurrentRow(0)
         rowData([COLOR="darkred"]"Column2NameHere"[/COLOR]) = [COLOR="blue"]CInt[/COLOR](astrCurrentRow(1))
         m_dsData.Tables([COLOR="darkred"]"YourTableNameHere"[/COLOR]).Rows.Add(rowData)
   [COLOR="blue"]End While
End Using[/COLOR]

[COLOR="SeaGreen"]'If you want to view the data within you datatable afterwards, you can
'bind it to a datagridview for viewing[/COLOR]
DataGridView.DataSource = m_dsData.Tables([COLOR="darkred"]"YourTableNameHere"[/COLOR])
 
Back
Top