Question

I'm having problems with my ASP classic codes. To give you an idea on the flow, the page will upload the image from a specific directory going to server, rename the file then save the file in SQL Server 2005 in a field with 'image' as datatype. Here's my codes:

<!-- #include file="class.mssqlconnect.asp" -->

<%

Const adTypeBinary = 1

Transcode = "S12345678"
strHDLocation = "c:\test\test.bmp"
strFileURL = Server.MapPath("signatures\"& Transcode &".bmp")

Set objADOStream = CreateObject("ADODB.Stream")
objADOStream.Open
objADOStream.Type = 1

objADOStream.LoadFromFile strHDLocation

'Set the stream position to the start
objADOStream.Position = 0 

'this will upload and save the file at the same time
objADOStream.SaveToFile strFileURL

Response.Write("Uploaded")

'if file uploaded successfully
Dim sqlRun
sqlRun =  "UPDATE Bioser SET Singature = (SELECT BulkColumn FROM OPENROWSET (BULK  '" & Server.MapPath("signatures\"& Transcode &".bmp") & "', SINGLE_BLOB) AS Signature) WHERE serviceno = '" & Transcode  & "'"
Conn.execute(sqlRun)
Conn.close

objADOStream.Close
Set objADOStream = Nothing

%>

I'm able to upload the image with no problems, but every time I'm saving the image to the database, it's only saving "BLANK" (not Null) in the Singature field. I don't get any error messages, I'm able to save the image before (I'm seeing a bunch of characters in the table field for the image), and I even enabled the 'BULKADMIN' Role Permission in the SQL username that I'm using...

I also already tried to use the SQL statement below:

"UPDATE Bioser SET Singature = (SELECT * FROM OPENROWSET(BULK '" & Server.MapPath("signatures\"& Transcode &".bmp") & "', SINGLE_BLOB) as Singature) WHERE ServiceNo = '" & Transcode  & "'"

Now I'm already stuck and I need help... Thanks for those who's gonna reply to this question.

Was it helpful?

Solution

I'm able to fix my own problem, I added "N" in my SQL Statement, from

sqlRun =  "UPDATE Bioser SET Singature = (SELECT * FROM OPENROWSET (BULK '" & Server.MapPath("signatures\"& Transcode &".bmp") & "', SINGLE_BLOB) as Singature) WHERE ServiceNo = '" & Transcode  & "'"

I revised it as:

sqlRun =  "UPDATE Bioser SET Singature = (SELECT * FROM OPENROWSET (BULK N'" & Server.MapPath("signatures\"& Transcode &".bmp") & "', SINGLE_BLOB) as Singature) WHERE ServiceNo = '" & Transcode  & "'"

I forgot that I need the N for converting the image base64 string to UNICODE (NVARCHAR/NCHAR). And my code is now working again. Yay!

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top