I'm writing a web app to retrieve images (pdf files) from an SQL database. The following code works fine when it resides in the load event of a page, but won't work when the code is put in the click event of a gridview select control.
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Using conn As New SqlConnection("Data Source=xxxxxxx;Initial Catalog=xxxxxxx;Integrated Security=false;user=xxxxxxxx;password=xxxxxxx")
conn.Open()
Dim cmd As New SqlCommand("Select TextCopyCol from doc.Documents where ID = @ID", conn)
cmd.Parameters.Add("@ID", SqlDbType.Int).Value = 1002 'Request.QueryString("ID")
Dim dr As SqlDataReader
dr = cmd.ExecuteReader
dr.Read()
Response.Clear()
Dim bytFile As Byte() = dr("TextCopyCol")
Dim buffer() As Byte = dr("TextCopyCol")
Dim blen As Integer = CType(dr("TextCopyCol"), Byte()).Length
Response.Buffer = True
Response.ContentType = "application/pdf"
Response.AddHeader("content-disposition", "attachment;filename=1002.pdf")
Response.Charset = ""
Response.Cache.SetCacheability(HttpCacheability.NoCache)
Response.BinaryWrite(bytFile)
Response.End()
End Using
End Sub
I get a parser error instead of one I can use, I don't really know what's going wrong.
PS: I don't want to use a file path instead of an image from SQL because I don't want any URL to show pointing to files.
Any help would be tremendously appreciated.
Also, please forgive if I posted this in the wrong forum, but frankly I couldn't find a better match.
If you pass any parameter to the code of displaying PDF file in Click event of Button, you need to check them. If not, I am not sure there is anything different between doing in Page_Load event and Click event. You can make a function to do it and call it
in both events. Set breakpoint to see if you read the right data from db. If it is ok, I think it will be done.
T3mplar Kn1g...
Member
5 Points
15 Posts
Disfunctional Image Streaming
Feb 24, 2011 03:40 AM|LINK
I'm writing a web app to retrieve images (pdf files) from an SQL database. The following code works fine when it resides in the load event of a page, but won't work when the code is put in the click event of a gridview select control.
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Using conn As New SqlConnection("Data Source=xxxxxxx;Initial Catalog=xxxxxxx;Integrated Security=false;user=xxxxxxxx;password=xxxxxxx")
conn.Open()
Dim cmd As New SqlCommand("Select TextCopyCol from doc.Documents where ID = @ID", conn)
cmd.Parameters.Add("@ID", SqlDbType.Int).Value = 1002 'Request.QueryString("ID")
Dim dr As SqlDataReader
dr = cmd.ExecuteReader
dr.Read()
Response.Clear()
Dim bytFile As Byte() = dr("TextCopyCol")
Dim buffer() As Byte = dr("TextCopyCol")
Dim blen As Integer = CType(dr("TextCopyCol"), Byte()).Length
Response.Buffer = True
Response.ContentType = "application/pdf"
Response.AddHeader("content-disposition", "attachment;filename=1002.pdf")
Response.Charset = ""
Response.Cache.SetCacheability(HttpCacheability.NoCache)
Response.BinaryWrite(bytFile)
Response.End()
End Using
End Sub
I get a parser error instead of one I can use, I don't really know what's going wrong.
PS: I don't want to use a file path instead of an image from SQL because I don't want any URL to show pointing to files.
Any help would be tremendously appreciated.
Also, please forgive if I posted this in the wrong forum, but frankly I couldn't find a better match.
Qin Dian Tan...
All-Star
113532 Points
12480 Posts
Microsoft
Re: Disfunctional Image Streaming
Feb 28, 2011 06:23 AM|LINK
Hi,
If you pass any parameter to the code of displaying PDF file in Click event of Button, you need to check them. If not, I am not sure there is anything different between doing in Page_Load event and Click event. You can make a function to do it and call it in both events. Set breakpoint to see if you read the right data from db. If it is ok, I think it will be done.
Thanks,
If you have any feedback about my replies, please contactmsdnmg@microsoft.com.
Microsoft One Code Framework