Hello friends, today I am back with a topic suggested by one
of my blog reader, he asked me to write articles on different enumerator
present in foreach loop container. I have already exampled about two
enumerators of foreach loop container earlier, today I am going to talk about “Foreach
Item enumerator”. Before implementing “Foreach
item enumerator”, we should know where this can be used.
Foreach Item Enumerator: Foreach item enumerator can be used
for loop over a custom table or array. This definition doesn’t give clarity
about its functionality. Once we implement this, we will be able to correlate the
definition with the functionality of “Foreach item enumerator”.
To implement “Foreach item enumerator”, we will drag and
drop “Foreach loop container” in control flow region (As shown below).
To configure “foreach loop container”, we will right click
on it and click on “Edit” menu that will bring a the “Foreach loop editor”
screen in front of us (As shown below).
We will select the “Collection” tab, since we are
implementing “Foreach item enumerator”, we will select the same in the
enumerator combo box(As shown above). Now we will create an array by clicking
on “Columns” button(encircled by red color) as shown above. Once we click on
the button a new model window will appear in front of us(As shown below).
In the above window, we will add one column by clicking on “Add”
button. We can also set the data type for that column. This column will act as
an array since it will contain a list of elements with same data type.
Similarly we can add multiple columns, then it will act as custom table. In this
example I am explaining the array example, in the same manner you can implement
the custom table scenario. The only difference is that in custom table example,
we will have multiple column instead of one column.
We will click on “OK” button to save the column settings and
current window will be closed. Now we are back to our earlier screen(As shown
below).
In the above window, I have entered a list of names in “Column
0”(which we created few minutes back).In other way “Column 0” will act as an array which is
containing a list of names. We will loop through on this array using “Foreach
item enumerator”. Now we will select the
“Variable Mappings” tab and the current screen will be transformed in a new
screen(As shown below).
We will click on combo box field “Variable” and click on “New
variable” option that will bring a new model window “Add Variable”(As shown
below).
In the above screen, we will give the name to our variable.
I have given name to the variable as “EmployeeName”. We will click on the “OK”
button to save the variable. Now we are back to the previous screen, we will
click on “OK” button further to close the earlier screen as well.
To see the output of the foreach loop, we will take a “Script
task” as shown below.
To configure the script task, we will right click on it and
click on “Edit” menu that will bring a new screen as shown below.
We will pass the variable “EmployeeName” to our script task,
for that reason, we will click on the button(encircled by red color) that will
open a new model window with the list of variables to choose(As shown below).
We will select the “EmployeeName” variable from the list and
click on “OK” button to save the settings. Now we are back to our previous
screen as shown below.
We will click on “Edit Script” button that will open the
visual studio IDE(As shown below).
In Main function(Encircled with red color), we will write
below line of code.
MessageBox.Show(Dts.Variables["EmployeeName"].Value.ToString());
and save it then we will close the Visual
studio IDE. After closing the Visual studio IDE, we are back to the earlier
screen, Now we will click on “OK” button to save the settings.
We are done with our package, lets run this
package by pressing “F5” key and see the results.
We can see in the above screenshot that first element of our
array is being shown in the message box, one we click on the “OK” button of the
message box , second element will be shown up and so on.
I hope you will find this article useful and please do
implement the custom table scenario by adding more columns. Please feel free to
ask any question related to this article.