0

I am trying to store a String inside a Dts variable in visual c# / visual c sharp. But, I get the error below. How do I fix this ? I have not used C# before so its hard for me to figure out. I do know Java well though.

        String v = Dts.Variables["myVar1"].Value + "";
        String x = Dts.Variables["myVar2"].Value + v + "'";
        Dts.Variables["myVar2"] = SQL;  // Why do I get an error here ?

The errors are -

Error 1 Property or indexer 'Microsoft.SqlServer.Dts.Runtime.Variables.this[object]' cannot be assigned to -- it is read only

Error 2 Cannot implicitly convert type 'string' to 'Microsoft.SqlServer.Dts.Runtime.Variable'

6
  • How is this vb.net ? It is visual C# Commented Oct 14, 2013 at 21:05
  • Until your edit it said Visual Basic in the first sentence. Unfortunately I didn't go over the code snippet to try to figure out which language it is. Commented Oct 14, 2013 at 22:09
  • @TheEvilPenguin - Looks like you really live up to your name. Okay, I made a mistake. Id did use C# in my second sentence though. Can I get my one back ? Commented Oct 14, 2013 at 22:34
  • I'm saying that I made a mistake - the code is C#, and you say C# in the second sentence. The C# tag has been edited back in for over an hour now, so any change I made has been undone. Commented Oct 14, 2013 at 22:41
  • @TheEvilPenguin - Can you give me + 1 now ? Commented Oct 14, 2013 at 22:42

2 Answers 2

4

Because the reference Dts.Variables is a collection of Variable elements and thus you cannot assign a string to a element of this collection. (and, as another answer pointed out, this collection is readonly)

Probably you want

Dts.Variables["myVar2"].Value = SQL;
Sign up to request clarification or add additional context in comments.

Comments

2

The reason for the first error is that the Variables[] indexer is read-only. That is you can only read the value of Dts.Variables["myVar2"] from it and not assign a new value to replace it. The indexer is essentially a syntactic shortcut for invoking a function on the Variables collection.

The second error stems from the fact that you are trying to assign a String type value to the previously mentioned indexer which returns an object of type Microsoft.SqlServer.Dts.Runtime.Variable. Again, it's the incorrect use of the indexer that is really prompting the second error.

The proper way to access the value of the variable object returned by the indexer is via the .Value property, the same as you are doing on the first line in your sample code:

Dts.Variables["myVar2"].Value = SQL;

1 Comment

so, will that put the value of SQL into "myVar2" ?

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.