Steam
Steam

Reputation: 9856

Cannot implicitly convert type string to microsoft.sqlserver.dts.runtime.variable

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'

Upvotes: 0

Views: 2390

Answers (2)

Mike Dinescu
Mike Dinescu

Reputation: 55760

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;

Upvotes: 2

Steve
Steve

Reputation: 216342

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;

Upvotes: 4

Related Questions