?
Solved

Problem in displaying a file from sqlserver database

Posted on 2006-06-05
7
Medium Priority
?
296 Views
Last Modified: 2010-04-17
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
0
Comment
Question by:sreehariprasad
  • 4
  • 3
7 Comments
 
LVL 5

Expert Comment

by:t_itanium
ID: 16839892
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
 

Author Comment

by:sreehariprasad
ID: 16839983
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
 
LVL 5

Expert Comment

by:t_itanium
ID: 16840231
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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 5

Expert Comment

by:t_itanium
ID: 16840281
try to read this:

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

cheers
tarek
0
 

Author Comment

by:sreehariprasad
ID: 16840305
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
 
LVL 5

Accepted Solution

by:
t_itanium earned 435 total points
ID: 16840335
this link should help...

if not tell me..

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

Author Comment

by:sreehariprasad
ID: 16858946
Thank u .....
That link helped me.......

cheers....
Sreehari
0

Featured Post

Upgrade your Question Security!

Add Premium security features to your question to ensure its privacy or anonymity. Learn more about your ability to control Question Security today.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Although it can be difficult to imagine, someday your child will have a career of his or her own. He or she will likely start a family, buy a home and start having their own children. So, while being a kid is still extremely important, it’s also …
Q&A with Course Creator, Mark Lassoff, on the importance of HTML5 in the career of a modern-day developer.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
Simple Linear Regression

840 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question