Search code examples
sql-serverssisetlssis-2012dts

SSIS The variable cannot be found


I want to execute package from visual studio solution

Code:

private Microsoft.SqlServer.Dts.Runtime.Package pkgPaquete;
private Application appAplicacion;

public DTSExecResult EjecucionPaquete(string str_Paquete, List < CatVariablesEtl > Vars = null) {

  DTSExecResult respuesta;
  try {
    appAplicacion = new Application();

    appAplicacion.PackagePassword = "pass";

    pkgPaquete = appAplicacion.LoadPackage(str_Paquete, null);
    foreach(CatVariablesEtl item in Vars) {
      pkgPaquete.Variables[item.str_NombreVariable.ToString()].Value = item.str_ValorVariable.ToString();
    }

    respuesta = pkgPaquete.Execute();

    return respuesta;
  } catch (Exception ex) {

    throw new NotImplementedException();
  }

}
}
}

It stop into foreach statement just in this line it throw catch:

pkgPaquete.Variables[item.str_NombreVariable.ToString()].Value = item.str_ValorVariable.ToString();

enter image description here

str_NombredeVariable value: enter image description here

item.str_ValorVariable value:enter image description here

Parameters into package:

enter image description here

Error:

The variable cannot be found. This occurs when an attempt is made to retrieve a variable from the Variables collection on a container during execution of the package, and the variable is not there. The variable name may have changed or the variable is not being created.

I read another relationed questions but I don´t found some correct answer to my problem. Help is very appreciated. Regards

Update: as Hadi comment I try to use

var setValueParameters = new Collection<PackageInfo.ExecutionValueParameterSet>();

But I just get vs red mark, I need to import some reference?enter image description here

As the second answer of Hadi I cannot reference pkgPaquete.Variables:enter image description here


Solution

  • First Attempt

    Try removing User:: from the variable name

    foreach(CatVariablesEtl item in Vars) {
      pkgPaquete.Variables[item.str_NombreVariable.ToString().Replace("User::","")].Value = item.str_ValorVariable.ToString();
    }
    

    Second Method to assing variables

    Also try using this method to assign value to variables:

    Microsoft.SqlServer.Dts.RunTime.Variables myVars = pkgPaquete.Variables;
    
    foreach(CatVariablesEtl item in Vars) {
      myVars[item.str_NombreVariable.ToString().Replace("User::","")].Value = item.str_ValorVariable.ToString();
    }
    
    Microsoft.SqlServer.Dts.Runtime.DTSExecResult results = pkgPaquete.Execute(null, myVars, null, null, null);
    

    if using parameters, It look like that they cannot be modified programatically. Try using variables instead of them