Monday, March 26, 2012

Image upload problem into database

Hi guys,

I'm currently trying to insert image into my SQL db. I have tried a number of methods that were posted online, and so far with no luck.

My current code reads:


Dim conn As New Data.SqlClient.SqlConnection()
conn.ConnectionString = ConfigurationManager.ConnectionStrings("MainDBConnection").ToString
conn.Open()

Dim cmd As New Data.SqlClient.SqlCommand("SP_SAVEImage", conn)

cmd.CommandType = Data.CommandType.StoredProcedure

Dim nUserID As New Data.SqlClient.SqlParameter("@.nUserID", Data.SqlDbType.Int)
nUserID.Value = "1"

Dim nAlbumID As New Data.SqlClient.SqlParameter("@.nAlbumID", Data.SqlDbType.Int)
nAlbumID.Value = "1"

Dim sDescription As New Data.SqlClient.SqlParameter("@.sDescription", Data.SqlDbType.VarChar, 50)
sDescription.Value = "image1"

Dim sImageName As New Data.SqlClient.SqlParameter("@.sImageName", Data.SqlDbType.VarChar, 50)
sImageName.Value = sImageName

Dim sImageType As New Data.SqlClient.SqlParameter("@.sImageType", Data.SqlDbType.VarChar, 50)
sImageType.Value = fileType

Dim sImageData As New Data.SqlClient.SqlParameter("@.sImageData", Data.SqlDbType.Image, uploadedFile.Length)
sImageData.Value = uploadedFile

cmd.Parameters.Add(nUserID)
cmd.Parameters.Add(nAlbumID)
cmd.Parameters.Add(sDescription)
cmd.Parameters.Add(sImageName)
cmd.Parameters.Add(sImageType)
cmd.Parameters.Add(sImageData)

Dim reader1 As Data.SqlClient.SqlDataReader

reader1 = cmd.ExecuteReader

Running through debug, everything runs up until the last line, where an error is caught saying : Failed to convert parameter value from a SqlParameter to a String

I reckon it's to do with the input sImageData being input as a byte array - but I can't seem to find a way around it.Angry


Any help greatly appreciated!!

Dim sImageName As New Data.SqlClient.SqlParameter("@.sImageName", Data.SqlDbType.VarChar, 50)
sImageName.Value = sImageName

You are setting an SqlParameter equal to a string value. Must be a typeo. Fix and retry.

|||

THANK YOU!!!!!!

I feel such an idiot now!! I've been looking at this code for so long I couldn't see the obvious.

I had honestly just given up on the idea of saving my data this way; but decided to check the post before going to bed - I can sleep well now knowing at least some of my code works! lol.

Cheers mate

No comments:

Post a Comment