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();
Parameters into package:
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?
As the second answer of Hadi I cannot reference pkgPaquete.Variables:
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();
}
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