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:

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?
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?