copy pdf file from local directory to sql database

Tech Tip: Click here to run a free scan for Windows Errors and optimize PC performance

From: http://www.visual-basic-data-mining.net/forum (simiking_at_gmail.com)
Date: 09/22/04


Date: Wed, 22 Sep 2004 04:31:15 -0700

Hi, i'm doing a project on generating a crystalreport and viewing it in a
pdf format. I'm using vb.net and sql server.

i managed to general the crystalreport, save it in local directory then view
it. but on the other hand i would also like to save the pdf file into my sql
database. this is the difficult part and also the part which i'm stuck now.
follows are all my code. hope there will be someone who can help me! thanks!

Dim myConnection As New SqlClient.SqlConnection()
myConnection.ConnectionString =
"server=(local);database=st;Trusted_Connection=yes;timeout =45"
Dim MyCommand As New SqlClient.SqlCommand()
MyCommand.Connection = myConnection
Dim CommandStr As String

CommandStr = "execute usp_rpt_note @note_id= '" & Label_NoteID.Text & "'"
MyCommand.CommandText = CommandStr
MyCommand.CommandType = CommandType.Text
Dim MyDA As New SqlClient.SqlDataAdapter()
MyDA.SelectCommand = MyCommand
Dim myDS As New Note_DataSet()

MyDA.Fill(myDS, "usp_rpt_note")
Dim oRpt As New Note_CrystalReport1()
oRpt.SetDataSource(myDS)

oRpt.ResourceName = "Note_CrystalReport1.rpt"

Dim DiskOpts As CrystalDecisions.Shared.DiskFileDestinationOptions = New
CrystalDecisions.Shared.DiskFileDestinationOptions()
oRpt.ExportOptions.ExportDestinationType =
CrystalDecisions.[Shared].ExportDestinationType.DiskFile
oRpt.ExportOptions.ExportFormatType =
CrystalDecisions.[Shared].ExportFormatType.PortableDocFormat

Dim filename, filenamepdf, filenamexls As String
filename = UCase(Logon_Name(Request.ServerVariables("LOGON_user"))) &
CStr(Year(Now())) & CStr(Month(Now())) & CStr(Day(Now())) &
CStr(Hour(Now())) & CStr(Minute(Now())) & CStr(Second(Now())) &
CStr(CInt(Int((100000 * Rnd(200)) + 1)))
filename = filename

filenamepdf = "D:\Inetpub\wwwroot\report\temp\" & filename & ".pdf"

DiskOpts.DiskFileName = filenamepdf

oRpt.ExportOptions.DestinationOptions = DiskOpts
oRpt.Export()

' Database operation variables
Dim connect As New SqlClient.SqlConnection("Initial Catalog=st;Data
Source=localhost;Integrated Security=SSPI;Persist Security Info=True;")
Dim cmd As New SqlClient.SqlCommand("usp_copy_frm_directory", connect)
Dim p As SqlClient.SqlParameter
Dim uploaded_file As String
Dim strFileUpload As String

'uploaded_file = Request.QueryString("strFileName")
strFileUpload = "d:\Inetpub\wwwroot\report\temp\" & filename & ".pdf"
filename = strFileUpload

'File/data variables
Dim imageStream As New IO.FileStream(filename, IO.FileMode.Open)

'Dim imagestream As New IO.FileStream(Server.MapPath("ToBeUpload.doc"),
IO.FileMode.Open)
Dim imageData(imageStream.Length) As Byte
Dim FileSize As Integer = CType(imageStream.Length, Integer)
Dim UploadDate As DateTime = DateTime.Now
'Dim sContentType As String = imageStream.GetType.ToString
'File1.PostedFile.ContentType
Dim sContentType As String = "Application/pdf"
'Dim sContentType As System.IO.Stream = File1.PostedFile.InputStream
' Read the image data and close the stream
imageStream.Read(imageData, 0, imageStream.Length)
imageStream.Close()

' Save the image
cmd.CommandType = CommandType.StoredProcedure

p = New SqlClient.SqlParameter("@ImageData", SqlDbType.Image)
p.Value = imageData
cmd.Parameters.Add(p)

p = New SqlClient.SqlParameter("@ContentType", SqlDbType.NVarChar)
p.Value = sContentType
cmd.Parameters.Add(p)

connect.Open()

cmd.ExecuteNonQuery()
connect.Close()

cmd.Dispose()
connect.Dispose()

Dim strPDF As String
strPDF = "./temp/" & filename & ".pdf"
Response.Redirect(strPDF)

Somehow it didnt work! i'm calling the above codes as a function in my
pageload.

Hope help is given! thanks!!

-- 
argeam - forum member
http://www.visual-basic-data-mining.net/forum


Relevant Pages

  • Search pattern
    ... Dim strfile As String ... Dim bAddressFound As Boolean ... Dim strCurrentChar As String ...
    (comp.databases.ms-access)
  • Re: Trouble with getting latest file
    ... Dim FolderName As String ... Dim FileName As String ... If CurrentDateStamp> LatestDateStamp Then ...
    (microsoft.public.access.modulesdaovba)
  • Auto Write Name and Merge across
    ... Dim Sheetname01 As String ... Dim WeekName01 As String ...
    (microsoft.public.excel.misc)
  • Re: Trouble with getting latest file
    ... I also need to check that I will get the filename of the file with the ... Dim The_FileName As String ... If CurrentDateStamp> LatestDateStamp Then ...
    (microsoft.public.access.modulesdaovba)
  • Re: multiplatform (pocketPC & desktopPC) (Daniel !!)
    ... Friend Versione As String ... Public Sub GetMyConnectionPalmare() ... Dim errorMessages As String ... Private Function GetDS_Desktop(ByVal SQL As String) As DataSet ...
    (microsoft.public.dotnet.framework.compactframework)