Tuesday 26 September 2017

Read PDF document from database ~ gniithelp ~ programming info

Part of code that I use for reading a PDF document from database into Internet explorer.
We first need one asp.net page with link . When we click on it we do Response.Redirect() to this page and send a parameter ID which we read in Page Load and use it to do simple sql query which will read a PDF data stored in it . (We store PDF as Byte array in data table column) . 
This time I will not post all the detail about it, if someone need more info let's message me and I will try to show more about these simple task :) 

Protected Sub Page_Load(ByVal sender As ObjectByVal e As System.EventArgs) Handles Me.Load
If Request.QueryString("id"IsNot Nothing Then
Dim ImageID As Integer = Convert.ToInt32(Request.Params("id"))
Dim strConnString2 As String = System.Configuration.ConfigurationManager.ConnectionStrings("conString").ConnectionString()
Dim con2 As New Data.SqlClient.SqlConnection(strConnString2)
Dim command2 As SqlCommand = New SqlCommand("procKOP_DokumentiSelect", con2)
command2.CommandType = CommandType.StoredProcedure
command2.Parameters.AddWithValue("@ZahtjevID", ImageID)
command2.Parameters.AddWithValue("@TipSelecta", 0)
con2.Open()
Dim reader As SqlDataReader = command2.ExecuteReader()
If reader.Read() Then
Response.ContentType = "application/pdf"
Response.BinaryWrite(DirectCast(reader("fldDokument"), [Byte]()))
End If
reader.Close()
con2.Close()
End If
End Sub

No comments:

Post a Comment