Monday, March 26, 2012

How to show a stored PDF

New to asp and have no idea what I'm doing wrong here. I just want to open a pdf that I have stored in a table and show it on the page. Here is my code. I keep getting theSystem.NullReferenceException was unhandled by user code.

<asp:SqlDataSourceID="SqlDataSource1"runat="server"ConnectionString="<%$ ConnectionStrings:smithsdaConnectionStringtest %>"

ProviderName="<%$ ConnectionStrings:smithsdaConnectionStringtest.ProviderName %>"

SelectCommand="SELECT * FROM [correspondence] WHERE ([facilitiesID] = ?)">

<SelectParameters>

<asp:QueryStringParameterName="facilitiesID"QueryStringField="DACorr"Type="Int32"/>

</SelectParameters>

</asp:SqlDataSource>

<scriptrunat="server">

ProtectedSub Page_Load(ByVal senderAsObject,ByVal eAs System.EventArgs)

Dim ScanDocAsNew DataViewDim argAsNew DataSourceSelectArguments

ScanDoc = SqlDataSource1.Select(arg)

Dim ScanLet(1)AsByte

ScanLet(0) =CByte(ScanDoc(0)("Letter"))

If ScanDocIsNot""Then

Response.Buffer =True

Response.Clear()

Response.ClearContent()

Response.ClearHeaders()

Response.ContentType ="application/pdf"

Response.BinaryWrite(ScanLet)

Response.End()

EndIf

EndSub

</script

Any Help greatly appreciated.

My suggestion would be to store the PDF files, all in one directory, then, in the database, store the names of the PDF files.

Then, show the relevant information in the database, using a Gridview, including one TemplateField, which would have a Hyperlink to the folder where the pDFs are stored, and the PDF itself. Then, when people clicked on the hyperlink, the PDF would show

|||

David,

Thanks for the reply. I wish I could do it that way, unfortunately they want the pdfs stored in the database so noone can accidentally delete them. The db has probably 200 stored pdfs associated with records and they need to be able to pull them up for viewing.

Thanks,

DeWayne

|||

I've changed the code to this and now it just opens a blank web page.

Dim ScanDocAsNew DataViewDim argAsNew DataSourceSelectArguments

ScanDoc = SqlDataSource1.Select(arg)

Dim ScanLet(1)AsByte

ScanLet(0) =CByte(Request.QueryString("Letter"))

If ScanDocIsNot""Then

Response.Buffer =True

Response.Clear()

Response.ClearContent()

Response.ClearHeaders()

'Response.AddHeader("content-disposition", "inline;filename=" + ScanLet)

Response.ContentType ="application/pdf"

'Response.AppendHeader("content-disposition", "inline;filename=" + "Scan.PDF")

Response.BinaryWrite(ScanLet)

Response.End()

'Else

' Response.Write("Error")

EndIf

I think I am on the right track but no PDF file yet.

|||

I finally got it, thanks to reading other posts in the forum and especially the one from adam v on how to strip the ole header. Now I just have to deal with .doc and .tif headers. This is a great forum and I have found lots of useful tips and tricks in here.

Thanks.

Here is the code I ended up with...

myConnection.Open()Dim myDataReaderAs OleDbDataReader = myCommand.ExecuteReader() myDataReader.Read()Dim intLoopCountAs Integer Dim intPositionAs Integer Dim ScanLetAs Byte() = myDataReader("letter")Do While intLoopCount < ScanLet.LengthIf ScanLet(intLoopCount) = 37Then'%If ScanLet(intLoopCount + 1) = 80Then'PIf ScanLet(intLoopCount + 2) = 68Then'DIf ScanLet(intLoopCount + 3) = 70Then'FIf ScanLet(intLoopCount + 4) = 45Then'- intPosition = intLoopCount intLoopCount = ScanLet.LengthEnd If End If End If End If End If intLoopCount = intLoopCount + 1Loop Dim bytStrippedData(ScanLet.Length - intPosition - 1)As Byte System.Buffer.BlockCopy(src:=ScanLet, srcOffset:=intPosition, dst:=bytStrippedData, dstOffset:=0, count:=ScanLet.Length - intPosition) Response.Clear() Response.ClearContent() Response.ClearHeaders() Response.ContentType ="application/pdf" Response.BinaryWrite(bytStrippedData) Response.Flush() Response.End() myConnection.Close() Response.Write("Person info successfully retrieved!")End UsingEnd Sub

No comments:

Post a Comment