SSIS ScriptTask change Value property of Variable

6.2k views Asked by At

I try to simply change value of SSIS variable doing this code in ScriptTask:

        string path = Dts.Connections["BazyPobrane"].ConnectionString.ToString();
        string[] nameZIParray = Directory.GetFiles(path, "*.ZIP");
        string[] nameRARarray = Directory.GetFiles(path, "*.RAR");

        foreach (string nameZIP in nameZIParray) //search new ZIP
        {
            if (File.GetCreationTime(nameZIP) > DateTime.Now.AddDays(-1))
            {
                Dts.Variables["User::NazwaPliku"].Value = Path.GetFileName(nameZIP);
            }

        }
        foreach (string nameRAR in nameRARarray) //search new RAR
        {
            if (File.GetCreationTime(nameRAR) > DateTime.Now.AddDays(-1))
            {
                Dts.Variables["User::NazwaPliku"].Value = Path.GetFileName(nameRAR);
            }

        }
        Dts.TaskResult = (int)ScriptResults.Success;

After executing ScriptTask it simply don't change the variable Value. Debug mode seems fine. Maybe i miss some component settings? Thx!

2

There are 2 answers

1
Geoff On BEST ANSWER

Some things to check:

  1. Are you sure the variable isn't changing? If you put a subsequent script task with a MessageBox in place, does it show the correct value?

  2. I don't think you need the variable type, i.e. remove "user::"

  3. Make sure the variable is in the ReadWriteVariables property, as suggested by @OcasoP

  4. What's the scope of the variable? Make sure you don't have two copies at different scopes, or that at least the one you do have is visible from the scope of the script

  5. You could try locking the variable before writing to it (which should be equivalent to (3) above)

Code example for the last point:

IDTSVariables100 variables = null;
this.VariableDispenser.LockOneForWrite("NazwaPliku",ref variables);
variables[0].Value = myValue;
variables.Unlock();
0
Diego On

debug your script task adding MsgBox(variable_name) and see its value through the execution. Best debugging option :)