Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

Professional Opinions
Ask a Question
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

troubleshooting Question

execute stored procedure from Script Task

Avatar of TommyTupa
TommyTupa asked on
Microsoft SQL Server
2 Comments1 Solution3523 ViewsLast Modified:
How do I execute a stored procedure in a script task using vb.net?  I'd like to use a script task as opposed to an execute sql task because I have conditional logic and I have lots of input parameters.

I'm using the code below but receive this error when calling the sub:

Unable to cast COM object of type 'System.__ComObject' to class type 'System.Data.SqlClient.SqlConnection'. Instances of types that represent COM components cannot be cast to types that do not represent COM components; however they can be cast to interfaces as long as the underlying COM component supports QueryInterface calls for the IID of the interface.

 Public Sub fRunStoredProc()
        Dim cn As SqlConnection = DirectCast(Dts.Connections("MyConnection").AcquireConnection(Nothing), SqlConnection)
        Dim cmd As SqlCommand

        cmd.Connection = cn
        cmd.CommandText = "[dbo].[MyStoredProcedure]"
        cmd.CommandType = CommandType.StoredProcedure
        cmd.Parameters.AddWithValue("FileName", DbType.String).Value = "MyFileName"
        cmd.Parameters.AddWithValue("Title", DbType.String).Value = "MyTitle"
        cmd.Parameters.AddWithValue("ReturnID", DbType.Int32).Direction = ParameterDirection.Output
 End Sub

Thank you,
Avatar of Steve Hogg
Steve HoggFlag of United States of America imageIT

Our community of experts have been thoroughly vetted for their expertise and industry experience.

This problem has been solved!
Unlock 1 Answer and 2 Comments.
See Answers