Get Help:Ask a Question in our Forums|Report a Bug|More Help Resources
Last post Feb 28, 2011 06:23 AM by Qin Dian Tang - MSFT
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")
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
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.Charset = ""
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.
Feb 28, 2011 06:23 AM|LINK
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.