HarleySkater
asked on
Using DataSource.InsertParameters.Add to Insert Image into MS SQL
I have a insert stored procedure "AGProfileSetup"
Parameters -----
@UserId uniqueidentifier,
@ProfilePic image,
@ProfilePicMIME varchar(50)
used to insert an image for a user into a database. The problem is from my understanding sql type "Image" is some kind of binary format :D I don't know how to pass a binary through the InsertParameters.Add function. Whats the best way to do this? By the way I am using ASP.NET 3.5 and MS SQL 2008
Parameters -----
@UserId uniqueidentifier,
@ProfilePic image,
@ProfilePicMIME varchar(50)
used to insert an image for a user into a database. The problem is from my understanding sql type "Image" is some kind of binary format :D I don't know how to pass a binary through the InsertParameters.Add function. Whats the best way to do this? By the way I am using ASP.NET 3.5 and MS SQL 2008
Dim imageBytes(Picture.PostedFile.InputStream.Length) As Byte
Picture.PostedFile.InputStream.Read(imageBytes, 0, imageBytes.Length)
Dim AGDataSource As New SqlDataSource()
AGDataSource.ConnectionString = ConfigurationManager.ConnectionStrings("AGConnectionString").ToString()
AGDataSource.InsertCommandType = SqlDataSourceCommandType.StoredProcedure
AGDataSource.InsertCommand = "AGProfileSetup"
AGDataSource.InsertParameters.Add("UserId", CurrentUser)
AGDataSource.InsertParameters.Add("@ProfilePic", TypeCode.Byte, imageBytes.ToString())
AGDataSource.InsertParameters.Add("@ProfilePicMIME", MIMEType)
AGDataSource.Insert()
AGDataSource = Nothing
You need to pass a byte array from the image that you are working with, not a string, so remove the .ToString() from imageBytes.ToString().
ASKER
Im sorry, I should have said that. But, I put ToString() because imageBytes was throwing an error. I will post that error to, I tried that on another attempt.
ASKER
ERROR: Error 1 Overload resolution failed because no accessible 'Add' can be called with these arguments:
'Public Function Add(name As String, dbType As System.Data.DbType, value As String) As Integer': Value of type '1-dimensional array of Byte' cannot be converted to 'String'.
'Public Function Add(name As String, type As System.TypeCode, value As String) As Integer': Value of type '1-dimensional array of Byte' cannot be converted to 'String'. C:\Documents and Settings\Administrator.ACF UNDING.000 \My Documents\AG\AG Website\Settings\Profile\P rofile.asp x.vb 58 9 C:\...\AG Website\
Thats why I added the .ToString()
Is there an easier way to do this?
'Public Function Add(name As String, dbType As System.Data.DbType, value As String) As Integer': Value of type '1-dimensional array of Byte' cannot be converted to 'String'.
'Public Function Add(name As String, type As System.TypeCode, value As String) As Integer': Value of type '1-dimensional array of Byte' cannot be converted to 'String'. C:\Documents and Settings\Administrator.ACF
Thats why I added the .ToString()
Is there an easier way to do this?
Try this:
AGDataSource.InsertParamet ers.AddWit hValue("@P rofilePic" , imageBytes)
AGDataSource.InsertParamet
ASKER
also if i take out the TypeCode.Byte I get this error -
Error 1 Value of type '1-dimensional array of Byte' cannot be converted to 'String'. C:\Documents and Settings\Administrator.ACF UNDING.000 \My Documents\AG\AG Website\Settings\Profile\P rofile.asp x.vb 58 58 C:\...\AG Website\
:D
Error 1 Value of type '1-dimensional array of Byte' cannot be converted to 'String'. C:\Documents and Settings\Administrator.ACF
:D
ASKER
AddWithValue gave me this error
Error: 1 'AddWithValue' is not a member of 'System.Web.UI.WebControls .Parameter Collection '. C:\Documents and Settings\Administrator.ACF UNDING.000 \My Documents\AG\AG Website\Settings\Profile\P rofile.asp x.vb 58 9 C:\...\AG Website\
Error: 1 'AddWithValue' is not a member of 'System.Web.UI.WebControls
ASKER
all I want to do is upload a picture from asp.net 3.5 to my sql 2008 db table hehehe
I have never inserted objects into a database before from asp.net, I didn't it would be this tricky, because its so easy to insert everything else ;D
I have never inserted objects into a database before from asp.net, I didn't it would be this tricky, because its so easy to insert everything else ;D
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
So I am guessing this is an example of when you should not use a stored procedure : )
I'll give it a try : )!
I'll give it a try : )!
I try to use stored procedures as much as possible, so if that is what you would like to do, it is a simple fix:
Using myConnection As New SqlConnection(ConfigurationManager.ConnectionStrings("ImageGalleryConnectionString").ConnectionString)
Using myCommand As New SqlCommand("InsertImageData", myConnection)
myCommand.CommandType = CommandType.StoredProcedure
myCommand.Parameters.AddWithValue("@Title", PictureTitle.Text.Trim())
myCommand.Parameters.AddWithValue("@MIMEType", MIMEType)
'Load FileUpload's InputStream into Byte array
Dim imageBytes(UploadedFile.PostedFile.InputStream.Length) As Byte
UploadedFile.PostedFile.InputStream.Read(imageBytes, 0, imageBytes.Length)
myCommand.Parameters.AddWithValue("@ImageData", imageBytes)
myConnection.Open()
myCommand.ExecuteNonQuery()
End Using
End Using
End Sub
ASKER
I got it to compile and run! but it didn't actually work hahaha
I am going to throw a try catch in there and see what i can find ; )
I am going to throw a try catch in there and see what i can find ; )
ASKER
i got this message this time :D
Your Profile Is Setup Is Confirmed!!!!!!!!!!!!!!!!! !!!!!!!!!! !!!!!!!!!! !!!!!!!!!! !!!!!!!!!! !!!!!!!
THANK YOUUUUUUUUUUUU
Your Profile Is Setup Is Confirmed!!!!!!!!!!!!!!!!!
THANK YOUUUUUUUUUUUU
Ur velcome :D
ASKER
thank youuu
ASKER
Input string was not in a correct format. hehe