Link to home
Start Free TrialLog in
Avatar of sreehariprasad
sreehariprasad

asked on

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
Avatar of t_itanium
t_itanium
Flag of United Arab Emirates image

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


Avatar of sreehariprasad
sreehariprasad

ASKER

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
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
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....!!!!!




ASKER CERTIFIED SOLUTION
Avatar of t_itanium
t_itanium
Flag of United Arab Emirates image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thank u .....
That link helped me.......

cheers....
Sreehari