0
votes

In writing an SSIS package (BIDS version 2012), I have declared a package level variable strFileLocation and set it to type String and gave it a value of "C:\Temp\file.txt".

In the SSIS package I then created a C# script task. Opening the script task editor, I declared the strFileLocation variable to be a ReadOnlyVariable.

Then I opened the script editor and declared this C# variable

string strFilePath = ""

And then I wrote this line of code:

strFilePath = Dts.Variables["strFileLocation"].Value; 

But this code does not work, generating this compilation error:

cannot implicitily convert type 'object' to 'string'. An explicit conversion exists(are you missing a cast?)

I was able to fix the problem by using this code:

strFilePath = Dts.Variables["strFileLocation"].Value.ToString();

It would seem that the SSIS type string is not the same as the C# type string. Does this have something to do with Unicode? And will I always have to explicity convert my SSIS variables to an appropriate C# datatype counterpart?

1
When I open script, it gives some notes like : Assign values from the DTS variables collection; This is case sensitive; User:: is not required; you must convert it from the Object type to a strong type; So I guess you must convert.Darka
Too lazy for an answer so you get a comment. A Variable is strongly typed in a Script Component (data flow object). In the Script Task, it is returned from the object collection as an Object type and must be cast to specific type for use. Why this is so is a question for the developers at MS.billinkc

1 Answers

1
votes

Doing a little more research, I believe I can answer my own question. It would appear that the SSIS package and the script task code does not see the variable in the same way. The variable will be seen as type string in the SSIS package, but the same variable will be seen as an object in the C# (or VB) script.

From microsoft's web site: http://msdn.microsoft.com/en-us/library/ms135941(d=printer).aspx

The Script task uses the Variables property of the Dts object to read from and write to Variable objects in the package.

Note The Value property of the Variable class is of type Object. Because the Script task has Option Strict enabled, you must cast the Value property to the appropriate type before you can use it.