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;pw d=shashi;d atabase=ra mesh")
'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.Writ e(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.StackTra ce)
End Try
// This is the stack trace with error
Line 1: Incorrect syntax near '='.
at System.Data.SqlClient.SqlC ommand.Exe cuteReader (CommandBe havior cmdBehavior, RunBehavior runBehavior, Boolean returnStream) at System.Data.SqlClient.SqlC ommand.Exe cuteReader () at Categories.download.btnDow nload_Clic k(Object sender, EventArgs e) in c:\inetpub\wwwroot\Categor ies\downlo ad.aspx.vb :line 67
Btn_click()
Try
' Create Connection object
con = New SqlConnection("server=hcc-
'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.Writ
'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.StackTra
End Try
// This is the stack trace with error
Line 1: Incorrect syntax near '='.
at System.Data.SqlClient.SqlC
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
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
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
ASKER
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 .HtmlInput File) _
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(fileuplo ad) Then
intFileLength = fileupload.PostedFile.Cont entLength
ReDim bytData(intFileLength)
objStream = fileupload.PostedFile.Inpu tStream
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....!!!!!
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
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(fileuplo
intFileLength = fileupload.PostedFile.Cont
ReDim bytData(intFileLength)
objStream = fileupload.PostedFile.Inpu
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thank u .....
That link helped me.......
cheers....
Sreehari
That link helped me.......
cheers....
Sreehari
cmd = New SqlCommand("select * from fileproperties where downloadname=" & fname, con)
by this:
cmd = New SqlCommand("select * from fileproperties where downloadname='" & fname & "'", con)
cheers