Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot apply indexing with[] to an expression of type

Tags:

c#

ssis

I am creating an SSIS package and want to include a script which checks if a file exist before retrieving the file and saving that data to a table.

I have three separate variable that I have set up:

fileExistFlag Int32 0

fileName String check.txt

folderPath String C:\

My C# code looks like this, where I am checking:

public void Main()
{
    // TODO: Add your code here
    String fp = Dts.Variables["User::folderPath"].Value.ToString() + Dts.Variables["User::fileName"].Value.ToString();
    if (File.Exists(fp))
    {
        Dts.Variables["User::fileExistFlag"].Value = 1;
    }
    MessageBox.Show(fp);
    MessageBox.Show(Dts.Variables["User::fileExistFlag"].Value.ToString());
    Dts.TaskResult = (int)ScriptResults.Success;
}

When I try to compile my script, I receive the following error:

Cannot apply indexing with [] to an expression of type 'Microsoft.SqlServer.Dts.Runtime.Variables for all four instances.

How can I solve the issue?

Updated code:

/*
   Microsoft SQL Server Integration Services Script Task
   Write scripts using Microsoft Visual C# 2008.
   The ScriptMain is the entry point class of the script.
*/

using System;
using System.Data;
using Microsoft.SqlServer.Dts.Runtime;
using System.Windows.Forms;
using System.IO;

namespace ST_04f6fa3ba49a4ddeac3d3d7fc29f04f2.csproj
{
    [System.AddIn.AddIn("ScriptMain", Version = "1.0", Publisher = "", Description = "")]
    public partial class ScriptMain : Microsoft.SqlServer.Dts.Tasks.ScriptTask.VSTARTScriptObjectModelBase
    {

        #region VSTA generated code
        enum ScriptResults
        {
            Success = Microsoft.SqlServer.Dts.Runtime.DTSExecResult.Success,
            Failure = Microsoft.SqlServer.Dts.Runtime.DTSExecResult.Failure
        };
        #endregion

        /*
        The execution engine calls this method when the task executes.
        To access the object model, use the Dts property. Connections, variables, events,
        and logging features are available as members of the Dts property as shown in the following examples.

        To reference a variable, call Dts.Variables["MyCaseSensitiveVariableName"].Value;
        To post a log entry, call Dts.Log("This is my log text", 999, null);
        To fire an event, call Dts.Events.FireInformation(99, "test", "hit the help message", "", 0, true);

        To use the connections collection use something like the following:
        ConnectionManager cm = Dts.Connections.Add("OLEDB");
        cm.ConnectionString = "Data Source=localhost;Initial Catalog=AdventureWorks;Provider=SQLNCLI10;Integrated Security=SSPI;Auto Translate=False;";

        Before returning from this method, set the value of Dts.TaskResult to indicate success or failure.

        To open Help, press F1.
    */

        public void Main()
        {
            // TODO: Add your code here
            String fp = Dts.Variables.Get("User::folderPath").Value.ToString() + Dts.Variables.Get("User::fileName").Value.ToString();
            if (File.Exists(fp))
            {
                Dts.Variables.Get("User::fileExistFlag").Value = 1;
            }
            MessageBox.Show(fp);
            MessageBox.Show(Dts.Variables.Get("User::fileExistFlag").Value.ToString());
            Dts.TaskResult = (int)ScriptResults.Success;
        }
    }
    public static Microsoft.SqlServer.Dts.Runtime.Variable Get(
        this Microsoft.SqlServer.Dts.Runtime.Variables variables, string name)
    {
        foreach(Microsoft.SqlServer.Dts.Runtime.Variable item in variables)
        {
            if(item.Name == name) return item;
        }
        return null;
    }
}
like image 757
SearchForKnowledge Avatar asked Jun 25 '14 13:06

SearchForKnowledge


3 Answers

This is a known BUG in SQL Server BIDS 2005/2008 after installing side by side a later version of SSIS. For example if you are developing a SSIS 2008 package and then install SSIS 2012.

A workaround is to move the file "Microsoft.SQLServer.ManagedDTS.dll" located in the path: "C:\Program Files (x86)\Microsoft SQL Server\110\SDK\Assemblies" to a backup folder, then the bids take the reference from the path "C:\Windows\assembly\GAC_MSIL\Microsoft.SqlServer.ManagedDTS\10.0.0.0__89845dcd8080cc91\"

But it doesn't seem to work for all the cases reported.

Source:

https://connect.microsoft.com/SQLServer/feedback/details/744390/ssis-any-pre-2012-error-cannot-apply-indexing-with-to-an-expression-of-type-microsoft-sqlserver-dts-runtime-variables

http://support.microsoft.com/kb/938608/en-us

like image 117
Guilherme Avatar answered Nov 09 '22 02:11

Guilherme


Oddly, this indexer does seem to exist. If it isn't working, though, you might be able to use an extension method:

public static class MyExtensionMethods
{
    public static Microsoft.SqlServer.Dts.Runtime.Variable Get(
        this Microsoft.SqlServer.Dts.Runtime.Variables variables, string name)
    {
        foreach(Microsoft.SqlServer.Dts.Runtime.Variable item in variables)
        {
            if(item.Name == name) return item;
        }
        return null;
    }
}

and use:

... Dts.Variables.Get("User::folderPath").Value ...

instead.

like image 32
Marc Gravell Avatar answered Nov 09 '22 01:11

Marc Gravell


Use Browse in the add references window and look for this dll: C:\Program Files (x86)\Microsoft SQL Server\100\SDK\Assemblies\Microsoft.SQLServer.ManagedDTS.dll

like image 1
Gabriel G Avatar answered Nov 09 '22 02:11

Gabriel G