Reputation: 141
I have developed an ssis package and i have a script which fetches few text command from a .txt file , stores those in variables and i use those variables to see what to do next and which dft to execute.
When i go to the .dtsx file--> right click--> execute -->i get the error message saying "Exception has been thrown by the target of an invocation."
However when i stop running the package and try to re execute it it runs successfully.
I tried to deploy this to the Integration Services Catalogs and it throws the same error which i see in the report files
Script task error: exception has been thrown by the target of an invocation
varERMLoadTxt
hold the text file path which stream reader reads
public void Main()
{
// TODO: Add your code here
string path = Dts.Variables["User::varERMLoadTxt"].Value.ToString();
using (StreamReader sr = File.OpenText(path))
{
string line = File.ReadAllText(path);
string[] lines = line.Split(',');
if(lines[0].Equals("load", StringComparison.CurrentCultureIgnoreCase))
Dts.Variables["User::varIsLoad"].Value = true;
else if (lines[0].Equals("update", StringComparison.CurrentCultureIgnoreCase))
Dts.Variables["User::varIsUpdate"].Value = true;
Dts.Variables["User::varCommand"].Value = lines[0].ToString();
Dts.Variables["User::varAnalysisDate"].Value = lines[1].ToString();
sr.Close();
}
Dts.TaskResult = (int)ScriptResults.Success;
}
enum ScriptResults
{
Success = Microsoft.SqlServer.Dts.Runtime.DTSExecResult.Success,
Failure = Microsoft.SqlServer.Dts.Runtime.DTSExecResult.Failure
};
Upvotes: 3
Views: 5941
Reputation: 37368
I have 3 suggestions:
The whole code should looks like
public void Main()
{
try{
string path = Dts.Variables["User::varERMLoadTxt"].Value.ToString();
if (File.Exists(path))
{
using (StreamReader sr = File.OpenText(path))
{
string line = File.ReadAllText(path);
string[] lines = line.Split(',');
if(lines[0].Equals("load", StringComparison.CurrentCultureIgnoreCase))
Dts.Variables["User::varIsLoad"].Value = true;
else if (lines[0].Equals("update", StringComparison.CurrentCultureIgnoreCase))
Dts.Variables["User::varIsUpdate"].Value = true;
Dts.Variables["User::varCommand"].Value = lines[0].ToString();
Dts.Variables["User::varAnalysisDate"].Value = lines[1].ToString();
sr.Close();
}
}
Dts.TaskResult = (int)ScriptResults.Success;
}catch(Exception ex){
Dts.FireError(0,"An error occured", ex.Message,String.Empty, 0);
Dts.TaskResult = (int)ScriptResults.Failure;
}
}
enum ScriptResults
{
Success = Microsoft.SqlServer.Dts.Runtime.DTSExecResult.Success,
Failure = Microsoft.SqlServer.Dts.Runtime.DTSExecResult.Failure
};
Upvotes: 1