I have seen many codes lying around but apparently they don't help.
Here is the logic I have to follow:
I have a file getting read which has an amount field. This amount field can have negatives. I have to move the file and fail the package if I find a negative amount in the file.
What I did so far:
Added a script task to get file path, added loop to read all files in that path, inside the loop(added a DTS task to move members with negative amounts to a record set, added script task to check wether there are any rows in the recordset, if count > 0 then FAIL the script task thus failing the Package, else pass)
The scipt task's code:
using System;
using System.Data;
using Microsoft.SqlServer.Dts.Runtime;
using System.Windows.Forms;
using System.Data.OleDb;
using System.IO;
using System.Xml;
// name space call here etc, etc..
enum ScriptResults
{
Success = Microsoft.SqlServer.Dts.Runtime.DTSExecResult.Success,
Failure = Microsoft.SqlServer.Dts.Runtime.DTSExecResult.Failure
};
public void Main()
{
// TODO: Add your code here
bool fireAgain = true;
DataTable dtable = new DataTable();
OleDbDataAdapter da = new OleDbDataAdapter();
// Setting this incase there are multiple files being checked
Dts.Variables["InvalidFile"].Value = "False";
da.Fill(dtable, Dts.Variables["NegativeAmountMember"].Value);
// I do get my desired result here of 1
Dts.Events.FireInformation(0, "dtable", dtable.Rows.Count.ToString(), String.Empty, 0, ref fireAgain);
if (dtable.Rows.Count > 0)
{
Dts.Variables["InvalidFile"].Value = "True";
Dts.Events.FireInformation(0, "InvalidFile", Dts.Variables["InvalidFile"].Value.ToString(), String.Empty, 0, ref fireAgain); // This does get displayed but, nothing happens after this step.
Dts.TaskResult = (int)ScriptResults.Failure; // this method never technically executes
return; // added this because someone somewhere said in a post to do so
}
else
{
Dts.TaskResult = (int)ScriptResults.Success;
}
}
I have everything working, except failing this Script Task.
I also tried FireError, even that does not get called.
What is happening, can anyone explain please?
Guys I figuered out the problem. The issue was a deadlock in the code with the InvalidFile variable.
Added:
Variables lockedVariables = null;
Dts.VariableDispenser.LockOneForWrite("InvalidFile", ref lockedVariables);
lockedVariables["InvalidFile"].Value = "True";
lockedVariables.Unlock();
to the code and the issue is now gone. With regards to @Ben Gribaudo's comment on invalid loop, the loop was valid, my comments in the code suggest such.
The deadlocked variable was also being used in an onError task where an email gets sent out. Because of the variable being locked in the loop for a write and the script task never actually failing until the onError is completed, the variable stayed locked. After adding the lock for one write
code, it all worked.
Thank you for your help, explanations and answers.