1

I am trying to assign a value to a variable from a flat file in an SSIS package. The script i am using is below

#Region "Imports"
Imports System
Imports System.Data
Imports System.Math
Imports Microsoft.SqlServer.Dts.Pipeline.Wrapper
Imports Microsoft.SqlServer.Dts.Runtime.Wrapper
#End Region

' This is the class to which to add your code.  Do not change the name, attributes, or parent
' of this class.
<Microsoft.SqlServer.Dts.Pipeline.SSISScriptComponentEntryPointAttribute()> _
<CLSCompliant(False)> _
Public Class ScriptMain
    Inherits UserComponent

    Dim StartDate As String
    Dim FinishDate As String

    Public Overrides Sub PreExecute()
        MyBase.PreExecute()

    End Sub

    Public Overrides Sub PostExecute()
        MyBase.PostExecute()
        Me.ReadWriteVariables("StartDate").Value = StartDate
        Me.ReadWriteVariables("FinishDate").Value = FinishDate

    End Sub

    Public Overrides Sub Input0_ProcessInputRow(ByVal Row As Input0Buffer)

        StartDate = Row.StartDate
        FinishDate = Row.FinishDate

    End Sub

End Class

The problem Im having is that sometimes the fields from the flat file could be blank, when they are blank I get an error:

The type of the value being assigned to variable "User::StartDate" differs from the current variable type. Variables may not change type during execution. Variable types are strict, except for variables of type Object.

Does anyone have any ideas what could be causing the error?

2 Answers 2

2

I'm guessing that's because the value will be null when you're doing the assignment. SSIS doesn't support null values for its variables.

What value do you want your variable to have when there are no values in the file? And is the problem occurring when there are no rows in the file, or when there is a row but the columns are empty for StartDate and EndDate?

If an empty string is what you want, try changing:

Dim StartDate As String
Dim FinishDate As String

to

Dim StartDate As String = ""
Dim FinishDate As String = ""

...to guard against the situation where there are no rows. In that case, your ProcessInputRow will never run, so you need to set up the strings as empty from the very beginning.

For the situation where there are rows, you want to guard against setting the values to be null in ProcessInputRow:

If Not Row.StartDate_IsNull Then
    StartDate = Row.StartDate
End If

If Not Row.EndDate_IsNull Then
    EndDate = Row.EndDate
End If

Though if you didn't tick "Retain null values from the source as null values in the data flow" on the Source of the file you're using, they should be coming through as empty strings anyway...

Sign up to request clarification or add additional context in comments.

1 Comment

There will be a data row but the StartDate EndDate columns will be blank, so I think solution one will be the best way forward. I dont know any VB at all but Im guessing that will give the variable a default value of "" then if there is a row this blank value will be overwritten with the actual date
1

If empty start and end dates are a valid scenario, you can use the 'ColumnName'_IsNull properties to guard against empty fields. In your code, you can add something like this:

if (String.IsNullOrEmpty(Row.StartDate))
{       
Row.StartDate_IsNull = true;
}        
else        
{            
StartDate = Row.StartDate;           
}

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.