Kamran
Kamran

Reputation: 147

SSIS Script Task not working in Visual Studio 2010, Exception has been thrown by the target of an invocation

I am using SSIS Script Task but whenever I am running it the the SSIS package fails and it gives the following error: Exception has been thrown by the target of an invocation.

Is it possible that it is giving this issue because I was using this script in Visual Studio 2008 and I am trying to implement the package in Visual Studio 2010.

here is my code:

  enter code here ' Microsoft SQL Server Integration Services Script Task
 ' Write scripts using Microsoft Visual Basic 2008.
 ' The ScriptMain is the entry point class of the script.

Imports System
Imports System.Data
Imports System.Math
Imports Microsoft.SqlServer.Dts.Runtime
Imports System.IO

<Microsoft.SqlServer.Dts.Tasks.ScriptTask.SSISScriptTaskEntryPointAttribute> _
<System.CLSCompliantAttribute(False)> _
Partial Public Class ScriptMain
Inherits 
Microsoft.SqlServer.Dts.Tasks.ScriptTask.VSTARTScriptObjectModelBase

Enum ScriptResults
    Success = Microsoft.SqlServer.Dts.Runtime.DTSExecResult.Success
    Failure = Microsoft.SqlServer.Dts.Runtime.DTSExecResult.Failure
End Enum


' The execution engine calls this method when the task executes.
' To access the object model, use the Dts property. Connections, variables, events,
' and logging features are available as members of the Dts property as shown in the following examples.
'
' To reference a variable, call Dts.Variables("MyCaseSensitiveVariableName").Value
' To post a log entry, call Dts.Log("This is my log text", 999, Nothing)
' To fire an event, call Dts.Events.FireInformation(99, "test", "hit the help message", "", 0, True)
'
' To use the connections collection use something like the following:
' ConnectionManager cm = Dts.Connections.Add("OLEDB")
' cm.ConnectionString = "Data Source=localhost;Initial Catalog=AdventureWorks;Provider=SQLNCLI10;Integrated Security=SSPI;Auto Translate=False;"
'
' Before returning from this method, set the value of Dts.TaskResult to indicate success or failure.
' 
' To open Help, press F1.

Public Sub Main()
    Dim file_stream As New FileStream(CType(ReadVariable("filepath"), String) + "AIR_FEE1.TRN", FileMode.Append)
    Using w As New StreamWriter(file_stream, Text.Encoding.UTF8)
        w.WriteLine("T|" + CType(ReadVariable("Count"), String))
    End Using
    Dim FName As String
    Dim LFName As String
    FName = CType(ReadVariable("filename"), String)
    LFName = CType(ReadVariable("logfile"), String)
    WriteVariable("StaticLogFileName", LFName)
    WriteVariable("StaticFileName", FName)
    Dim file_stream1 As New FileStream("StaticFileName", FileMode.Create)
    file_stream.Close()
    Dts.TaskResult = ScriptResults.Success
End Sub
Private Function ReadVariable(ByVal varName As String) As Object
    Dim result As Object
    Try
        Dim vars As Variables
        Dts.VariableDispenser.LockForRead(varName)
        Dts.VariableDispenser.GetVariables(vars)
        Try
            result = vars(varName).Value
        Catch ex As Exception
            Throw ex
        Finally
            vars.Unlock()
        End Try
    Catch ex As Exception
        Throw ex
    End Try
    Return result
End Function

Private Sub WriteVariable(ByVal varName As String, ByVal varValue As Object)
    Try
        Dim vars As Variables
        Dts.VariableDispenser.LockForWrite(varName)
        Dts.VariableDispenser.GetVariables(vars)
        Try
            vars(varName).Value = varValue
        Catch ex As Exception
            Throw ex
        Finally
            vars.Unlock()
        End Try
    Catch ex As Exception
        Throw ex
    End Try
End Sub

End Class

Upvotes: 2

Views: 1228

Answers (1)

Hadi
Hadi

Reputation: 37313

First of all, "Exception has been thrown by the target of an invocation" is a generic message that is thrown when an error occurred during script task execution, try to debug your code to find a more precise error message.

I think you can write the same script without defining functions to manipulate your variables:

Public Sub Main()

    Dim file_stream As New FileStream(Dts.Variables("filepath").Value + "AIR_FEE1.TRN", FileMode.Append)

    Using w As New StreamWriter(file_stream, Text.Encoding.UTF8)
        w.WriteLine("T|" + Dts.Variables("Count").Value)
    End Using

    Dim FName As String
    Dim LFName As String

    FName = Dts.Variables("filename").Value
    LFName = Dts.Variables("logfile").Value

    Dts.Variables("StaticLogFileName").Value =  LFName
    Dts.Variables("StaticFileName").Value =  FName

    Dim file_stream1 As New FileStream("StaticFileName", FileMode.Create)
    file_stream.Close()


    Dts.TaskResult = ScriptResults.Success

End Sub

Make sure that you have selected your ReadOnly Variables and ReadWrite Variables properly from the Script task properties form.

Helpful links

Upvotes: 1

Related Questions