Problem in displaying a file from sqlserver database

Here is the program that i wrote to display a file which is stored in sql server database as 'image' type of data.  The problem is  It is showing an error at the statement "dr=cmd.executereader()" The stack trace is given after the program

Btn_click()
Try
            ' Create Connection object
            con = New SqlConnection("server=hcc-pc232c;uid=shashi;pwd=shashi;database=ramesh")
            'Create Command Object
            cmd = New SqlCommand("select * from fileproperties where downloadname=" & fname, con)
            'Open(Connection)
            con.Open()
            If con.State = ConnectionState.Open Then
                'Execute command and receive DataReader
                dr = cmd.ExecuteReader() 'HERE IT IS SHOWING THE ERROR
                'Read row
                While dr.Read()
                    'Clear Response buffer
                    Response.Clear()
                    ' Set ContentType to the ContentType of our file
                    Response.ContentType = dr(1)
                    'Write data out of database into Output Stream
                    Response.OutputStream.Write(dr(0), 0, dr(2))
                    'Close database reader and connection
                    dr.Close()
                    con.Close()
                    'End the page
                    Response.End()
                End While
            End If
        Catch ex As Exception
            Response.Write(ex.Message)
            Response.Write("<br>")
            Response.Write(ex.StackTrace)
        End Try

// This is the stack trace  with error

Line 1: Incorrect syntax near '='.
at System.Data.SqlClient.SqlCommand.ExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream) at System.Data.SqlClient.SqlCommand.ExecuteReader() at Categories.download.btnDownload_Click(Object sender, EventArgs e) in c:\inetpub\wwwroot\Categories\download.aspx.vb:line 67
sreehariprasadAsked:
Who is Participating?
 
t_itaniumConnect With a Mentor Commented:
this link should help...

if not tell me..

http://www.dbazine.com/sql/sql-articles/charran5
0
 
t_itaniumCommented:
try to replace this:

cmd = New SqlCommand("select * from fileproperties where downloadname=" & fname, con)


by this:


cmd = New SqlCommand("select * from fileproperties where downloadname='" & fname & "'", con)

cheers


0
 
sreehariprasadAuthor Commented:
the code is executed correctly with ur manipulation.

Could u please send me the code how to retrieve an image from sql server .
The problem is, I have uploaded a file(.txt) using a web form to sql server.
I stored the content of the file  in column with data type Image.
I want to retrieve that file from sql server to a web form.
could u plz reply me abt this as soon as possible


Regards
hari
0
Cloud Class® Course: MCSA MCSE Windows Server 2012

This course teaches how to install and configure Windows Server 2012 R2.  It is the first step on your path to becoming a Microsoft Certified Solutions Expert (MCSE).

 
t_itaniumCommented:
suppose that d(0) is the image field..
try


dim s as string
s=d(0).toString();

by the way y r u storing .txt as image type....  you can stre them as binary...

cheers
0
 
t_itaniumCommented:
try to read this:

http://www.dbazine.com/sql/sql-articles/charran5

cheers
tarek
0
 
sreehariprasadAuthor Commented:
Not only text files, but also some .doc, .ppt, .jpeg,.gif etc..... are all has to store in database

When i am uploading the file, i have converted it to byte array and then byte array is going to store in image column of table.

Public Function GetByteArrayFromfileupload( _
      ByVal fileupload As System.Web.UI.HtmlControls.HtmlInputFile) _
      As Byte()
        ' Returns a byte array from the passed
        ' file field controls file
        Dim intFileLength As Integer, bytData() As Byte
        Dim objStream As System.IO.Stream
        Try
            If FileFieldSelected(fileupload) Then
                intFileLength = fileupload.PostedFile.ContentLength
                ReDim bytData(intFileLength)
                objStream = fileupload.PostedFile.InputStream
                objStream.Read(bytData, 0, intFileLength)
                Return bytData
            End If
        Catch ex As Exception
            Response.Write(ex.Message)
        End Try
       
    End Function

I am passing the bytData to table.....and also, file type, length and filename...

The table structure is :

FileData(content image,type varchar,length integer,filename varchar)


basing on the type of the file, i have to open the file in  corresponding application
But, i could not able to do so......

Hope u understand, my problem....!!!!!




0
 
sreehariprasadAuthor Commented:
Thank u .....
That link helped me.......

cheers....
Sreehari
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.