Click here to Skip to main content
13,200,729 members (57,820 online)
Rate this:
Please Sign up or sign in to vote.
Hi all

I wrote the below code to print the data table in a pdf file

Dim strFilePath As String
        Dim stm As StreamWriter
        Dim i As Integer
        con = New MySqlConnection("Server=localhost; Database=demo;Uid=root; password=1234")
        Dim cmd As MySqlCommand = New MySqlCommand("select * from demodata", con)
        Dim dt As DataTable = New DataTable()
        Dim da As MySqlDataAdapter = New MySqlDataAdapter(cmd)
        '  DataGridView1.DataSource = dt
            strFilePath = "C:\a\test.pdf"
            stm = New StreamWriter(strFilePath, False)
            For i = 0 To dt.Columns.Count - 2
                stm.Write(dt.Columns(i).ColumnName + ControlChars.Tab)
            Next i
            For Each row As DataRow In dt.Rows
                Dim array() As Object = row.ItemArray
                For i = 0 To array.Length - 2
                    stm.Write(array(i).ToString() + ControlChars.Tab)
                Next i
            Next row
            'File.Open(strFilePath, vbReadOnly)
            MsgBox("job done!")
        Catch ex As Exception
        End Try

the problem is it's creating the pdf file but when i m opening it's giving error saying file damaged or not supported file type.

Please tell me the problem..
Posted 3-Jan-13 2:14am
Zoltán Zörgő 3-Jan-13 13:05pm
Sorry, I have pasted the wrong link. See update!
Rate this: bad
Please Sign up or sign in to vote.

Solution 1

StreamWriter ?!?!?!, are you writing your pdf by hand?
Of course you get error. You shouldn't go this way...

Use an external component like the ones listed here:[^]

[Update(zorgoz): link changed to the right one]
Rate this: bad
Please Sign up or sign in to vote.

Solution 2

you can use any reporting tool for the same, using it you can easily export to pdf


This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy |
Web02 | 2.8.171020.1 | Last Updated 3 Jan 2013
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100