4

I have populated a list in C# script and assigned its value to SSIS object variable.

Then I used that object variable to execute some SQL query by looping through For each do enumerator.

I tried doing this by Foreach ado enumerator but getting error

X variable doesn't contain a valid data object.

Can anybody provide any inputs.

enter image description here

12
  • Can you please provide your method where you set the variable value? Commented Dec 14, 2017 at 8:42
  • What enumerator do you use? Have you created a object variable that you map the array values to? How do you map the array item (in the loop) to the variable? Found this straightforward example: rad.pasfu.com/index.php?/archives/… Commented Dec 14, 2017 at 8:50
  • I am populating LogList List<vFailedTransactionNo> LogList = new List<vFailedTransactionNo>(); //populating List LogList.Add(new vFailedTransactionNo(Row.asutransectionnumber, Row.StageID, Row.EnrollmentID, "Cannot identify the Store ID,")); LogList.Add(new vFailedTransactionNo(Row.asutransectionnumber, Row.StageID, Row.EnrollmentID, "Cannot identify the Store ID,")); //populating object variable vFailed TransactionNo this.Variables.vFailedTransactionNo = LogList; Commented Dec 14, 2017 at 8:58
  • And used ado.net enumerator Commented Dec 14, 2017 at 9:02
  • Does setting the ssis vFailedTransactionNo work? Im not sure but usally when i set a value in a script task I do something like this: Dts.Variables("SomeVariableName").Value = MyArray Commented Dec 14, 2017 at 9:10

2 Answers 2

9

Youre using a list. Not a recordset and therefore you need to enumerate over a variable.

If you want to use ADO Recordset, you need to fill a datatable instead.

  1. This shows you how to write to object with a variable list

  2. This shows you how to write to object with recordset (using multiple values)

Like this:

1 .C# Script code - Write to Object with list using variable enumerator

public void Main()
    {
        // TODO: Add your code here

        List<string> NewList = new List<string>();

        NewList.Add("Ost");
        NewList.Add("Hest");

        Dts.Variables["User::NameList"].Value = NewList;

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

1. Variable settings in ssis enter image description here

1. Foreach loop container settings

Use Foreach Variable Enumerator and use your object variable

enter image description here

Map your outcome to a variable(s)

enter image description here

1. Execute SQL Task test case

Write your SQL with variables

enter image description here

Map your variable to Parameter mapping

enter image description here

1. Result

enter image description here

2. C# Script code - Write to object with datatable using ADO enumerator

   public void Main()
    {
        // TODO: Add your code here

        DataTable dt = new DataTable();

        dt.Columns.Add("FilmName",typeof(string));
        dt.Columns.Add("ActorName",typeof(string));

        dt.Rows.Add("Starwars", "Harrison ford");
        dt.Rows.Add("Pulp fiction", "Samuel Jackson");


        Dts.Variables["User::NameList"].Value = dt;

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

2. Variable settings in ssis

enter image description here

2. Foreach loop container settings

Use Foreach ADO Enumerator and your object as variable

enter image description here

Map your outcome to variable(s)

enter image description here

2. Execute sql task test case

Write your SQL with variables

enter image description here

Map your variable(s) to Parameter mapping

enter image description here

2. Result

enter image description here

Sign up to request clarification or add additional context in comments.

2 Comments

nice explanation +1
@Hadi Thanks, i appreciate it :)
0

Thanks @plaidDK Second approch solved my problem

2.C# Script code - Write to object with datatable using ADO enumerator

Instead of list I have populated data table:

public DataTable ToDataTable<T>(List<T> items)
{
    DataTable dataTable = new DataTable(typeof(T).Name);
    //Get all the properties by using reflection   
    PropertyInfo[] Props = typeof(T).GetProperties(BindingFlags.Public | BindingFlags.Instance);
    foreach (PropertyInfo prop in Props)
    {
        //Setting column names as Property names  
        dataTable.Columns.Add(prop.Name);
    }
    foreach (T item in items)
    {
        var values = new object[Props.Length];
        for (int i = 0; i < Props.Length; i++)
        {

            values[i] = Props[i].GetValue(item, null);
        }
        dataTable.Rows.Add(values);
    }

    return dataTable;
}  

//Variable passed as below Variables.vFailedTransactionNo = dt;

ANd then ado enumerator done rest of the job.

Thanks for help!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.