Solved

Using DataSource.InsertParameters.Add to Insert Image into MS SQL

Posted on 2008-10-22
15
1,321 Views
Last Modified: 2012-05-05
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
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

Open in new window

0
Comment
Question by:HarleySkater
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 10
  • 5
15 Comments
 
LVL 1

Author Comment

by:HarleySkater
ID: 22779746
oh yah.  my error message is!!!!!!!!!!!


Input string was not in a correct format. hehe
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 22785645
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().
0
 
LVL 1

Author Comment

by:HarleySkater
ID: 22785678
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.
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 1

Author Comment

by:HarleySkater
ID: 22785705
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.ACFUNDING.000\My Documents\AG\AG Website\Settings\Profile\Profile.aspx.vb      58      9      C:\...\AG Website\

Thats why I added the .ToString()

Is there an easier way to do this?
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 22785728
Try this:

AGDataSource.InsertParameters.AddWithValue("@ProfilePic", imageBytes)
0
 
LVL 1

Author Comment

by:HarleySkater
ID: 22785740
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.ACFUNDING.000\My Documents\AG\AG Website\Settings\Profile\Profile.aspx.vb      58      58      C:\...\AG Website\

:D
0
 
LVL 1

Author Comment

by:HarleySkater
ID: 22785751
AddWithValue gave me this error
Error:      1      'AddWithValue' is not a member of 'System.Web.UI.WebControls.ParameterCollection'.      C:\Documents and Settings\Administrator.ACFUNDING.000\My Documents\AG\AG Website\Settings\Profile\Profile.aspx.vb      58      9      C:\...\AG Website\
0
 
LVL 1

Author Comment

by:HarleySkater
ID: 22785818
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
0
 
LVL 96

Accepted Solution

by:
Bob Learned earned 500 total points
ID: 22785846
Aah, yes, I missed that you are using an SqlDataSource.  

Try this instead:

Storing Binary Files Directly in the Database Using ASP.NET 2.0
http://aspnet.4guysfromrolla.com/demos/printPage.aspx?path=/articles/120606-1.aspx

Using myConnection As New SqlConnection(ConfigurationManager.ConnectionStrings("ImageGalleryConnectionString").ConnectionString)
 
    Const SQL As String = "INSERT INTO [Pictures] ([Title], [MIMEType], [ImageData]) VALUES (@Title, @MIMEType, @ImageData)"
    Using myCommand As New SqlCommand(SQL, myConnection)
      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 

Open in new window

0
 
LVL 1

Author Comment

by:HarleySkater
ID: 22785879
So I am guessing this is an example of when you should not use a stored procedure : )

I'll give it a try : )!
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 22785904
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 

Open in new window

0
 
LVL 1

Author Comment

by:HarleySkater
ID: 22786139
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 ; )
0
 
LVL 1

Author Comment

by:HarleySkater
ID: 22786213
i got this message this time :D

Your Profile Is Setup Is Confirmed!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

THANK YOUUUUUUUUUUUU  
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 22786272
Ur velcome :D
0
 
LVL 1

Author Closing Comment

by:HarleySkater
ID: 31508873
thank youuu
0

Featured Post

Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…
Are you ready to implement Active Directory best practices without reading 300+ pages? You're in luck. In this webinar hosted by Skyport Systems, you gain insight into Microsoft's latest comprehensive guide, with tips on the best and easiest way…

726 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question