Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1776
  • Last Modified:

SqlDataSource InsertParameters with FileUpload issues

I am using the FileUpload control in vs 2008 to upload documents into a sql server. I have a stored procedure that takes the parameters, including the document.

The problem is that the default value of the InsertParameters is a string. How do I get the byte array into the database and what dbtype should i use?
Dim SqlDataSource1 As New SqlDataSource
 
SqlDataSource1.InsertParameters.Clear()
SqlDataSource1.ConnectionString = Common.GlobalConnectionString
 
SqlDataSource1.InsertCommandType = SqlDataSourceCommandType.StoredProcedure
SqlDataSource1.InsertCommand = "insertDocument"
 
SqlDataSource1.InsertParameters.Add("Description", Description)
SqlDataSource1.InsertParameters.Add("FileName", FileName)
SqlDataSource1.InsertParameters.Add("ContentType", ContentType)
SqlDataSource1.InsertParameters.Add("ContentLength", ContentLength)
** SqlDataSource1.InsertParameters.Add("FileBytes", FileBytes)
 
SqlDataSource1.Insert()

Open in new window

0
syinner
Asked:
syinner
  • 3
  • 2
1 Solution
 
anoyesCommented:
Give this a shot:


SqlDataSource1.InsertParameters.Add(New Parameter("FileBytes", DbType.Object, FileBytes))

Open in new window

0
 
syinnerAuthor Commented:
Thanks for that, but I've already tried that. Gives me the error that Value of type '1-dimensional array of Byte' cannot be converted to 'String'.
0
 
anoyesCommented:
OK, think I've got it.  You have to use a SqlParameter, which comes from the System.Data.SqlClient namespace.  The catch though is that you can't add a SqlParameter directly to the SqlDataSource InsertParameters collection.  You have to add an OnInserting event that will add it for you.  I got this to work for me.

Dim SqlDataSource1 As New SqlDataSource
AddHandler SqlDataSource1.Inserting, AddressOf UploadInserting
 
SqlDataSource1.InsertParameters.Clear()
SqlDataSource1.ConnectionString = Common.GlobalConnectionString
 
SqlDataSource1.InsertCommandType = SqlDataSourceCommandType.StoredProcedure
SqlDataSource1.InsertCommand = "insertDocument"
 
SqlDataSource1.InsertParameters.Add("Description", Description)
SqlDataSource1.InsertParameters.Add("FileName", FileName)
SqlDataSource1.InsertParameters.Add("ContentType", ContentType)
SqlDataSource1.InsertParameters.Add("ContentLength", ContentLength)
 
SqlDataSource1.Insert()
 
------
 
    Protected Sub UploadInserting(ByVal sender As Object, ByVal e As SqlDataSourceCommandEventArgs)
        ''Here's where you collect your FileBytes variable
 
        Dim p As New SqlClient.SqlParameter
        p.ParameterName = "FileBytes"
        p.SqlDbType = SqlDbType.VarBinary
        p.Value = FileBytes
 
        e.Command.Parameters.Add(p)
    End Sub

Open in new window

0
 
syinnerAuthor Commented:
Thanks, that fixed it!
0
 
syinnerAuthor Commented:
Thanks, that fixed it!! Much appreciated!!
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now