954,517 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Accessing documents from SQL Server database with VB.NET

Hi,

I have saved documents into my SQL Server 2005 database and they have been converted to bytes. My web page is made with VB.NET in Visual Studio 2008 with ASP.NET. The following is exactly what I need to achieve;

1. Allow user select document with a button


2. Select the document from the SQL Server table

3. Allow the user to download this document.

The error I am getting is that Category is set as Byte and it can't be passed through GetDocument because filePath is a String. I think that I am pulling the file path from the database wrong, any help would be greatly appreciated!

[
     Dim Category As Byte() = Nothing
    Dim fs As FileStream
    Dim br As BinaryReader
    Dim document As Byte()
    Dim documentFilePath As String




    Protected Sub ImageButton1_Click(ByVal sender As Object, ByVal e As System.Web.UI.ImageClickEventArgs) Handles ImageButton1.Click


        myConnection = New SqlConnection("server=ORLA-PC\SQLEXPRESS;database=Edpac;Trusted_Connection=Yes")
        myConnection.Open()


        myCommand = New SqlCommand("Select FileURL from tblSubmittalFiles where FileID = 1", myConnection)




        Dim dr21 As SqlDataReader = myCommand.ExecuteReader()
        If dr21.HasRows Then
            Do While dr21.Read()
                For Me.Columns = 0 To dr21.FieldCount - 1
                    Category = dr21.Item(Columns)
                Next


                documentFilePath = Category
                GetDocument(documentFilePath)


            Loop


        End If
        dr21.Close()


        myConnection.Close()


    End Sub




    Public Sub GetDocument(ByVal filePath As String)


        fs = New FileStream(filePath, FileMode.Open, FileAccess.Read)
        br = New BinaryReader(fs)


        document = br.ReadBytes(Int(fs.Length))
        MsgBox(document.ToString)


        br.Close()
        fs.Close()


    End Sub
]
OrlaLynch
Newbie Poster
1 post since Feb 2009
Reputation Points: 10
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You