3

I've just spent well over an hour finding a solution to a problem - now I'm curious as to why it works. I was getting this error in SSIS when I executed a package:

enter image description here

I had a script task which was accessing a variable. I'd correctly added this to the ReadOnlyVariables collection for the script task, and was confident I'd used the right case. Here was my script line which wasn't working:

string textToWrite = (string)Dts.Variables["User::TestNumber"].Value;

Eventually I changed this to:

string textToWrite = Dts.Variables["User::TestNumber"].Value.ToString();

and everything worked OK. My question is: why did this fail before?

2
  • What is the result of running GetType() on that value? As in: string typeOfTextToWrite = Dts.Variables["User::TestNumber"].Value.GetType().ToString(). ... and what was the type of the variable in SSIS? Commented Apr 18, 2013 at 12:41
  • Variable type was Int32 in SSIS, and the line you suggest gives System.Int32. Commented Apr 18, 2013 at 18:49

1 Answer 1

1

If test number is an Integer then an exception would be thrown with the first line of code because there is no type conversion defined from Int32 to string and that's why we have ToString()

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

5 Comments

But surely (string) casts it to a string data type?
because the explicit cast is not implemented, but a conversion is, hence why we use .ToString()
It's a separate argument whether the cast should be implemented, but unfortunately it's not, so for now, we do .ToString()
that's a good question and I think this thread covers it well; channel9.msdn.com/Forums/TechOff/…
That's a great discussion. However, I think there might be something else going on here to do with SSIS. There seems to be a bug: sometimes when I execute my package it works perfectly, and on other occasions it crashes as above. Twice I've set a breakpoint in debug and this has solved the problem - even after I then remove the breakpoint. Hopefully this discussion will help someone, and thanks for your help saj.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.