troubleshooting Question

get binary data type out of SQL Server DB and read to HTML from user clicking Hyperlink button in datagrid

Avatar of Wendy__Parker
Wendy__Parker asked on
1 Comment1 Solution277 ViewsLast Modified:
Here is my situation.  I am programming an ASP with VB.NET.  I have a datagrid that displays file info (file name, file type, file ID).  The file Name field is a hyperlink button that directs the user to another page.  On that other page, I pull the ID out of the url, query the SQL Server DB and get the binary data from the DB.  the file is a pdf, but since some are scanned images the info will not necessarily be text, could be image, but always pdf.  When I use Response.BinaryWrite(), it will not let me pass in my binary data.  I need to convert it to a byte array, but I don't know how to do this.  Here is what I have so far:

If Page.IsPostBack = False Then
            Dim URL_FileID As Integer
            Dim sql As String
            Dim sdr As SqlClient.SqlDataReader
            Dim cmd As SqlClient.SqlCommand
            Dim myData() As Byte

            URL_FileID = CType((Trim(Page.Request.QueryString("ID"))), Integer)
            cmd = New SqlClient.SqlCommand("Select BinaryData from dbo.Files where (FileID = " & URL_FileID & ")", Me.SqlConnection1)
            sdr = cmd.ExecuteReader
            Dim recordExists As Boolean
            recordExists = sdr.Read

            If (recordExists) Then
                If Not (sdr.GetValue(0).ToString() = "") Then
                    sdr.GetBytes(0, 50, myData, 0, 0)
                    Dim blob As SqlTypes.SqlBinary = sdr.GetSqlBinary(0)
                    ReDim myData(blob.Length)
                    Response.ContentType = "application/pdf"
                End If
            End If

I have tried several different things and have a bunch of commented out code, but this will give you an idea of where I am in my logic and knowledge.  Any help is much appreciated.  I am officially stuck!

Join the community to see this answer!
Join our exclusive community to see this answer & millions of others.
Unlock 1 Answer and 1 Comment.
Join the Community
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