Solved

SqlDataSource InsertParameters with FileUpload issues

Posted on 2008-10-04
5
1,676 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Hangfire / asp.net sample 3 50
Multiple file Upload asp.net 2 38
Achieve json result 2 65
Why is some text in blue in Visual Studio? 6 22
Lots of people ask this question on how to extend the “MembershipProvider” to make use of custom authentication like using existing database or make use of some other way of authentication. Many blogs show you how to extend the membership provider c…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
A short film showing how OnPage and Connectwise integration works.

914 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now