troubleshooting Question

How can I write a byte array to a file using a SaveFileDialog

Avatar of mainrotor
mainrotor asked on
.NET ProgrammingVisual Basic.NET
1 Comment1 Solution265 ViewsLast Modified:
Good Morning Experts,
I need help Reading and Saving files and images stored as varbinary data in my database using SaveFileDialog in my VB.Net application.

I am able retrieve the data column from my SQL Server DB.  Now that I have the data, I want to save the data to a selected file path via SaveFileDialog.  How do I do this?

The varbinary data could be a .pdf, .tif, .jpg, or .docx.

This is the code I have so far.  What should I put inside the parenthesis of bw.Write()
 
    Private Sub btnRetrieve_Click(sender As System.Object, e As System.EventArgs) Handles btnRetrieve.Click
        Dim dta As New TestDBDataSetTableAdapters.sp_SelectFileTableAdapter
        Dim dt As TestDBDataSet.sp_SelectFileDataTable = dta.GetData(6)
        Dim file As String = "D:\temp\test.docx"

        Dim dr As DataRow
        For Each dr In dt.Rows
            WriteWordDoc(file, dr("FileData"))
        Next
    End Sub

    Private Sub WriteWordDoc(filename As String, data As Byte())
        Dim fs As New System.IO.FileStream(filename, IO.FileMode.Create)
        Dim bw As New System.IO.BinaryWriter(fs)
        bw.Write()
        bw.Close()
        fs.Close()
    End Sub

Thank you,
mrotor
ASKER CERTIFIED SOLUTION
Avodah
Serves the Most High - Not Religion

Our community of experts have been thoroughly vetted for their expertise and industry experience.

Join our community to see this answer!
Unlock 1 Answer and 1 Comment.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 1 Comment.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros