Conversion of System.Object to collection

  • Need assistance in looping an Object variable. Basically I have a SQL task populating the variable

    select field1 from table1 where field2 = 'N'

    This gets dumped into Variable1 of data type "Object" using the Result Set mapping.

    I need to then loop through this result set in the variable in a script task concatenating the results to a string variable (which is used in an email message body)

    I've tried the ctype(variable1, collections.arraylist) and a host of other conversions but to no avail.

    Any guidance, suggestions much appreciated.

    Cheers

  • Yeah - I already have it looping on the For..Next components elsewhere in the package, its just getting it to loop (or converting it to something that will allow a scripted loop) within a .NET script.

    If push comes to shove then I'll probably end up creating another external For...Next loop and putting my script in that. . .but it starts to look messy.

    Thanks for the response!

  • Setting the ResultSet to XML in the Execute SQL Task is another option that would make it easy to use the results in a Script Task.

    Just add "FOR XML AUTO" to the end of the SELECT query. Then in the Script Task read through the XML to build an ArrayList or string.

    Script Task:

    ' Add Imports System.Xml and a reference to System.XML

    ' myXML and myCollection are package variables of type Object

    Dim myXMLDoc as XmlDocument = DirectCast(Dts.Variables("myXML").Value, XmlDocument)

    Dim nodes as XmlNodeList = myXMLDoc.SelectNodes("//table1[@field1]")

    Dim myList as New Collections.ArrayList

    For Each node as XmlNode in nodes

    myList.Add(node.Attributes.GetNamedItem("field1").Value.Tostring)

    Next

    Dts.Variables("myCollection").Value = myList

  • Fantastic - thank you! 😀

    Its been a long few weeks!:crazy:

Viewing 5 posts - 1 through 4 (of 4 total)

You must be logged in to reply to this topic. Login to reply