SSIS 2008, The name 'Dts' does not exist in the current context
Hi, Got issue when converting a SSIS SQL 2005 to 2008 version. The issue is in a script task where the custom class (MyClassBase) cannot access the Dts.Variables collection. Here is the code layout, script rewritten in C# but the same error also affects the VB.NET syntax: namespace ST_d6071454e188869b97484f90a63d5491.csproj { public partial class ScriptMain : Microsoft.SqlServer.Dts.Tasks.ScriptTask.VSTARTScriptObjectModelBase { public void Main() { // code here can access Dts.Variables["User::MyVar1"].Value } } public class MyClassBase { public void MyClassBase() { // Dts.Variables["User::MyVar1"].Value here gets The name 'Dts' does not exist in the current context } } } Question: what is the scope of the Dts object? Is there any way the to make the Dts.Variables collection to be globally accessible in other classes of the same script? In the example above, how to make Dts.Variables readable in MyClassBase so the code can compile? Thanks in advance for any help. EDIT: clarify description.
February 22nd, 2010 7:59pm

Which language you have selected in script task?delete the script task and start coding with new one in VB.Net.Let us TRY this | http://quest4gen.blogspot.com/
Free Windows Admin Tool Kit Click here and download it now
February 22nd, 2010 8:02pm

Hi, Sorry for the description in post #1 not being clear enough. I hope the steps below would be enough to reproduce the issue: 1. Create a new SSIS package in SQL Server 2008 2. Create a variable, package level, type string, variable name = "gvContactName" 3. Create a script task, in the Main() method, we just set a value, something like Dts.Variables("User::gvContactName").Value = "Joe" So far, everything works OK 4. Now in the script task, I create a new class named MyClassBase. In this class, I need to access the Dts object (either to read/write package variables or to set a value to Dts.TaskResult). For this example, let say we just want to assign the value "Joe2" to the gvContactName variable: Here is the corresponding script: C# syntax: (Compile Error: The name 'Dts' does not exist in the current context) public class MyClassBase { public MyClassBase() { Dts.Variables["User::gvContactName"].Value = "Joe2"; } } VB.NET syntax: (Compile Error: Name 'Dts' is not declared) Public Class MyClassBase Public Sub New() Dts.Variables("User::gvContactName").Value = "Joe2" End Sub End Class QUESTION : How to make the Dts object globally accessible to all custom classes? And BTW, what is the reason for the scope of the Dts object to be limited only to the ScriptMain class? Thanks in advance for any help
February 22nd, 2010 10:25pm

The scope is limited because it's actually a private member of the class that ScriptMain is inheriting - the wrapper class that SSIS builds for you. Treat it like an object, not a namespace.Float over a use of Dts in your ScriptMain, and you'll see what data type it is. Now you can pass that object anywhere and operate on/with it.
Free Windows Admin Tool Kit Click here and download it now
February 22nd, 2010 11:30pm

Hi, Thanks Todd for the answer. Yes I saw that Dts object the way you described or by "Go to Definition". Passing the reference of that Dts object to all other classes is all I can do for now. May be I miss something important, but it just seems natural that the Dts context should be globally visible in the entire script. I was hoping there was a way to enable that behavior. As you may guess, I have tried to subclass the custom class the same way as ScriptMain class: public class MyClassBase : Microsoft.SqlServer.Dts.Tasks.ScriptTask.VSTARTScriptObjectModelBase But unfortunately, the error 'The name 'Dts' does not exist in the current context' is replaced by another error 'System.NullReferenceException: Object reference not set to an instance of an object.'
February 23rd, 2010 12:22am

No, you can't change the scope of the variable. And no, you can't "replicate" the behaviour just by creating a new derived class. The wrapper is integrated into the provided class - you have what you have.Why is that an issue? If you want to use another class, just add the Dts object to its constructor, or to a class constructor that instances refer to...
Free Windows Admin Tool Kit Click here and download it now
February 23rd, 2010 12:33am

Hi Todd, Thank you for your advice. No problem to pass that Dts reference along to other classes. I was just assuming that Dts object should be globally visible within the entire script. Here is the implementation in case anyone needs: public class MyClassBase { protected static Microsoft.SqlServer.Dts.Tasks.ScriptTask.ScriptObjectModel CurrDtsContext; public MyClassBase(Microsoft.SqlServer.Dts.Tasks.ScriptTask.ScriptObjectModel dtsObj) { CurrDtsContext = dtsObj; CurrDtsContext.Variables["User::gvContactName"].Value = "Joe2"; } }
February 23rd, 2010 1:18am

Is that worked for you? please let me know.
Free Windows Admin Tool Kit Click here and download it now
February 25th, 2011 5:02am

It should be as simple as this: string x = this.Variables.YourVariable; string y = this.Variables.YourOtherVariable; BECAUSE: SSIS creates UserComponent (for example) on your behalf. The ScriptMain inherits UserComponent. [Microsoft.SqlServer.Dts.Pipeline. SSISScriptComponentEntryPointAttribute] public class ScriptMain : UserComponent { ... } When you go to the definition of UserComponent, you may see something like this: public class UserComponent: ScriptComponent { ... public Variables Variables; publicUserComponent() { ... Variables = new Variables(this); } } Then when you scroll down, you would see: public class Variables { ... public String YourVariable { get { return (String)(ParentComponent.ReadOnlyVariables["YourVariable"].Value); } } public String YourOtherVariable { get { return (String)(ParentComponent.ReadOnlyVariables["YourOtherVariable"].Value); } } } So now, put it all together, you can access your variables this way: [Microsoft.SqlServer.Dts.Pipeline.SSISScriptComponentEntryPointAttribute] public class ScriptMain : UserComponent { ... string x = this.Variables.YourVariable; string y = this.Variables.YourOtherVariable; ... }
November 2nd, 2011 11:03pm

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics