2

I'm doing some testing, outputting a full result set from a Execute SQL Task into a Foreach loop as below

enter image description here

and I just want to output my variable values to a message box, however it doesn't seem to work.

public void Main()
    {

        try
        {

            // TODO: Add your code here
            string Variables = Dts.Variables["User::ClientID"].ToString() +
            Dts.Variables["User::Passphrase"].ToString() +
            Dts.Variables["User::KeyFileName"].ToString() +
            Dts.Variables["User::InboundEncryptionRequired"].ToString() +
            Dts.Variables["User::SftpResponseRequired"].ToString() +
            Dts.Variables["User::OutboundDecryptionRequired"].ToString() +
            Dts.Variables["User::SftpHost"].ToString() +
            Dts.Variables["User::SftpPort"].ToString() +
            Dts.Variables["User::SftpUserName"].ToString() +
            Dts.Variables["User::Active"].ToString() +
            Dts.Variables["User::SftpDownloadFrom"].ToString() +
            Dts.Variables["User::SftpUploadTo"].ToString() +
            Dts.Variables["User::SftpDeleteFilesFromRemote"].ToString() +
            Dts.Variables["User::ConnectionProtocol"].ToString();

            MessageBox.Show(Variables);

        }



        catch (Exception Ex)
        {

            MessageBox.Show(Ex.Message);

        }
    }
}

}

I'm just getting "element cannot be found in a collection" or something, even though I know the query is outputting 2 rows.

I have also mapped my variables on the foreach loop and specified read only variables on scrip task.

* UPDATE *

This is driving me nuts. I've triple checked my variable names, can confirm I'm getting full result set.

enter image description here

I removed the User:: from my script task variables, but still no luck.

4 Answers 4

9

You have to pass in the variables you are using. Double-click the Script Task and in the "Script Task Editor" modal window, enter the variables you want to display in "ReadOnlyVariables" or "ReadWriteVariables". Then you'll be able to reference them in your code.

script task editor

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

Comments

3

It sounds as if one of your variable names is not spelled correctly and it cannot be found in the array.

Also you could use

string Variables = String.Format("{0},{1},...",Dts.Variables["User::ClientID"],Dts.Variables["User::Passphrase"],...)

to display the values or set your variables string.

3 Comments

sorry i don't really understand what you mean by using string.format, what is the 0,1 etc. can you post a small working example?
he means using string.format to merge vars into string instead of simple concatenation
E.g. var archiveFile = string.Format("{0}, {1}", Dts.Variables["User::ExpPathArchiveFolder"].Value, Dts.Variables["User::ExpCurrentFileName"].Value); If you switched the 0 and 1 around then the strings would be concatenated in the switched order.
3

You don't have to specify the scope: User or System. Remove User:: from each name. If it still doesn't work - you must have misspelled one of the names.

Also you need to get the value and convert it to string. Correct format is: Dts.Variables["Passphrase"].Value.ToString()

Comments

2

I just stumbled upon this after trying to solve a similar problem. The first issue I found with your code is that you don't have

.value

before the .ToString in the string you are concatenating. I changed this on mine and it worked.

1 Comment

Welcome to Stack overflow! This answer came to me to review as it is your first post. With regard to your comment about the try..catch - it probably is not required, but you can test it by adding the try and catch around your code and seeing whether it still works. If it does - I suggest removing the bit about the try...catch as not relevant to the answer.

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.