Solved

SqlDataSource InsertParameters with FileUpload issues

Posted on 2008-10-04
5
1,685 Views
Last Modified: 2012-05-05
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
Comment
Question by:syinner
  • 3
  • 2
5 Comments
 
LVL 16

Expert Comment

by:anoyes
ID: 22642181
Give this a shot:


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

Open in new window

0
 

Author Comment

by:syinner
ID: 22642240
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
 
LVL 16

Accepted Solution

by:
anoyes earned 250 total points
ID: 22642577
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
 

Author Comment

by:syinner
ID: 22644185
Thanks, that fixed it!
0
 

Author Closing Comment

by:syinner
ID: 31503056
Thanks, that fixed it!! Much appreciated!!
0

Featured Post

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

In this Article, I will provide a few tips in problem and solution manner. Opening an ASPX page in Visual studio 2003 is very slow. To make it fast, please do follow below steps:   Open the Solution/Project. Right click the ASPX file to b…
One of the pain points with developing AJAX, JavaScript, JQuery, and other client-side behaviors is that JavaScript doesn’t allow for cross domain request for pulling content. For example, JavaScript code on www.johnchapman.name could not pull conte…
In a recent question (https://www.experts-exchange.com/questions/28997919/Pagination-in-Adobe-Acrobat.html) here at Experts Exchange, a member asked how to add page numbers to a PDF file using Adobe Acrobat XI Pro. This short video Micro Tutorial sh…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

786 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