views:

64

answers:

3

Hello,

I'm trying to convert a pdf to binary dato and saving it to my SQL database. When i just output the pdf (from binary) to the user, it works perfectly.

The field in my database for the binary data is image.

Here is what i'm doing atm:

Set oFileStream = Server.CreateObject("ADODB.Stream")
oFileStream.Open
oFileStream.Type = 1 'Binary
oFileStream.LoadFromFile strPDF

And if i do Response.BinaryWrite(oFileStream.Read) the PDF pops to user.

Then i want to store it to SQL:

Set Conn = Server.CreateObject("ADODB.Connection")
Conn.Open "xxx"
strSQL = "INSERT INTO vAnalysesHistory (datetime,chosendatetime,companyid,code,content) VALUES (?,?,?,?,?)"
Set cmd = Server.CreateObject("ADODB.Command")
Set cmd.ActiveConnection = Conn
cmd.CommandType = adCmdText
cmd.CommandText = strSQL
cmd.Parameters.Append cmd.CreateParameter("@datetime", adDate, adParamInput, 255, now)
cmd.Parameters.Append cmd.CreateParameter("@chosendatetime", adDate, adParamInput, 255, Request.Form("date"))
cmd.Parameters.Append cmd.CreateParameter("@companyid", adVarChar, adParamInput, 255, Request.Form("companyid"))
cmd.Parameters.Append cmd.CreateParameter("@code", adVarChar, adParamInput, 255, Request.Form("code"))
cmd.Parameters.Append cmd.CreateParameter("@content", adLongVarBinary, adParamInput, 8000, oFileStream.Read)
cmd.Execute()

Conn.close
Set Conn = Nothing

And i just get this error: Application uses a value of the wrong type for the current operation.

I've tried a bunch of other things also with some other errors.

A: 

Read the stream into a Byte first, and then pass the Byte variable to the @content parameter

Vidar Nordnes
Can you give me a code example?
s0mmer
Is there a method on oFileStream called ReadBinary or something like that? If so, you could doDim b = oFileStream.ReadBinaryand then use b as your parameter
Vidar Nordnes
A: 

Based on what Vidar said, see the section "Uploading Image File to a Database" here:

http://www.beansoftware.com/ASP.NET-Tutorials/Save-Read-Image-Database.aspx

NinjaCat
A: 

Ended up using Persits ASDPdf which works fine. Thanks anyway

s0mmer