Error in VB Function in SSIS but not in VisualStudio - vb.net

I have some VB.Net code that I'm trying to add to a SSIS package. The code runs fine in VisualStudio 2012 and when I compile it there are no errors and the executable runs as well. When I insert the code into an SSIS package Script Task and copy/paste my code in I'm getting an error in one of my functions:
'Any' is not a member of 'System.Collections.Generic.IEnumerable(Of String)'.
I copied and pasted the main body of the code exactly and inserted one Imports line:
#Region "Imports"
Imports System
Imports System.Data
Imports System.Math
Imports System.IO 'Added this one Imports
Imports Microsoft.SqlServer.Dts.Runtime
#End Region
<Microsoft.SqlServer.Dts.Tasks.ScriptTask.SSISScriptTaskEntryPointAttribute()> _
<System.CLSCompliantAttribute(False)> _
Partial Public Class ScriptMain
Inherits Microsoft.SqlServer.Dts.Tasks.ScriptTask.VSTARTScriptObjectModelBase
Public Sub Main()
Dim sDirectoryRoot As String = "\\Server\Drive$\SubFolder\SubFolder2\"
Dim dirList As New DirectoryInfo(sDirectoryRoot)
ListFiles(dirList)
Dts.TaskResult = ScriptResults.Success 'left this line from SSIS default
End Sub
Public Sub ListFiles(ByVal dirList As DirectoryInfo)
Dim bEmpty As Boolean = False
[...] bunch of code with no errors. Actual function call below where clientdir is a directory path
bEmpty = IsDirectoryEmpty(clientdir.ToString)
End Sub
Public Function IsDirectoryEmpty(ByVal path As String) As Boolean
Return Not (Directory.EnumerateFileSystemEntries(path).Any())
End Function
The Directory.EnumerateFileSystemEntries(path).Any is underlined with the 'Any' is not a member of 'System.Collections.Generic.IEnumerable(Of String)' error. I can't figure out why it runs fine in Visual Studio, but when I use the Edit Script button in the Script Task Editor (which starts another instance of Visual Studio) it errors out.

Any is an extension method and, as with all extension methods, requires that you import the appropriate namespace. It is, in fact, the System.Linq.Enumerable.Any method, hence you must import the System.Linq namespace. That's probably done at the project level by default in your non-SSIS project. Note that you will also have to have referenced the System.Core.dll assembly.

Related

SSIS Script task error "Exception has been thrown by the target of an invocation."

I have the following script in SSIS using Pdfsharp library:
#Region "Imports"
Imports System
Imports System.Data
Imports System.Math
Imports PdfSharp.Pdf
Imports PdfSharp.Pdf.IO
Imports Microsoft.SqlServer.Dts.Runtime
#End Region
Public Sub Main()
'
' Add your code here
'
Dim document As PdfDocument
Dim doc_name As String
doc_name = "C:\temp\Testfile.pdf"
document = PdfReader.Open(doc_name)
document.Info.Author = "TestTest"
document.Save(doc_name)
Dts.TaskResult = ScriptResults.Success
End Sub
It's failing with an error "Exception has been thrown by the target of an invocation." on PdfReader.Open line. Steps executed: Pdfsharp library is placed in C:\Program Files (x86)\Microsoft SQL Server\130\DTS\Binn folder and solution recognizes it. I have access to that file and can open it. What else can cause this error in this super simple code? Would appreciate any advice.

Parse solution using DTE Visual Basic

I have to parse my solution to list all files that it is using.
I have created this:
Imports EnvDTE
Imports EnvDTE80
Imports Microsoft.VisualBasic
Imports System.Collections
Public Class C
Implements VisualCommanderExt.ICommand
Sub Run(DTE As EnvDTE80.DTE2, package As Microsoft.VisualStudio.Shell.Package) Implements VisualCommanderExt.ICommand.Run
listing(DTE)
End Sub
Sub listing(DTE As EnvDTE80.DTE2)
Dim prj As Project
Dim prjs As Projects
prjs = DTE.Solution.Projects
For Each prj In prjs
Dim item As String
Dim itemEnum as IEnumerator = prj.GetEnumerator()
itemEnum.Reset()
While itemEnum.MoveNext()
item = itemEnum.Current().FullName
My.Computer.FileSystem.WriteAllText("C:\tmp\list.txt", item, True)
End While
Next
End Sub
End Class
Unfortunately I encounter the Exception:
System.Runtime.InteropServices.COMExcption(0x80020003): Member not found....
My solution has 10 projects.
Navigating projects and files is recursive because they can be nested.
See my articles:
HOWTO: Navigate the files of a solution from a Visual Studio .NET macro or add-in.
HOWTO: Navigate the files of a solution using the IVsHierarchy interface from an add-in.
http://www.visualstudioextensibility.com/articles/add-ins/

Reference to non-shared member requires an object reference

I am writing macro for Visual Studio in VB:
Imports EnvDTE
Imports EnvDTE80
Imports Microsoft.VisualBasic
Public Class C
Implements VisualCommanderExt.ICommand
Sub Run(DTE As EnvDTE80.DTE2, package As Microsoft.VisualStudio.Shell.Package) Implements VisualCommanderExt.ICommand.Run
FileNamesExample()
End Sub
Sub FileNamesExample()
Dim proj As Project
Dim projitems As ProjectItems
' Reference the current solution and its projects and project items.
proj = DTE.ActiveSolutionProjects(0)
projitems = proj.ProjectItems
' List the file name associated with the first project item.
MsgBox(projitems.Item(1).FileNames(1))
End Sub
End Class
And I got this after compilation:
Error (17,0): error BC30469: Reference to a non-shared member requires an object reference.
Have you any ideas what is wrong? I didn't develop in VB earlier and I just need instant help.
DTE is a type, as well as what you've given to the name of a parameter in your Run method. When used in this line:
proj = DTE.ActiveSolutionProjects(0)
It's using the type, rather than an instance of an object. You need to pass through the variable into your method:
Imports EnvDTE
Imports EnvDTE80
Imports Microsoft.VisualBasic
Public Class C
Implements VisualCommanderExt.ICommand
Sub Run(DTE As EnvDTE80.DTE2, package As Microsoft.VisualStudio.Shell.Package) Implements VisualCommanderExt.ICommand.Run
FileNamesExample(DTE)
End Sub
Sub FileNamesExample(DTE As EnvDTE80.DTE2)
Dim proj As Project
Dim projitems As ProjectItems
' Reference the current solution and its projects and project items.
proj = DTE.ActiveSolutionProjects(0)
projitems = proj.ProjectItems
' List the file name associated with the first project item.
MsgBox(projitems.Item(1).FileNames(1))
End Sub
End Class

Debugging SSIS vb.net Script

I am debugging a SSIS vb.net script in a Visual Studio 2005 SSIS project.
Is there a way to execute just the script without having to start in my control flow? Otherwise I have to work through my other steps and drill down through my Script Task into the Editor into the actual script.
As a side not my script is pretty simple, it just creates a directory if a directory with today's date is not found.
Imports System
Imports System.IO
Imports Microsoft.VisualBasic
Imports System.Data
Imports System.Math
Imports Microsoft.SqlServer.Dts.Runtime
Public Class ScriptMain
' Checks to see if todays folder exists on sqlzdocs -> if it doesnt it creates it. Else it errors
Public Sub Main()
Dim todaysdate As String = String.Format("{0:yyyyMMdd}", DateTime.Now)
Dim di As IO.DirectoryInfo = New IO.DirectoryInfo("\\MyServer\Path\Current\" + todaysdate )
If di.Exists = True Then
Dts.Variables("User::FolderExists").Value = True
Else
Try
Dim createdirectory As IO.DirectoryInfo = Directory.CreateDirectory(di.ToString)
Catch ex As Exception
Dts.Variables("User::Errors").Value = "Could not create the directory:" + di.ToString
Dts.Variables("User::FolderExists").Value = False
End Try
End If
Dts.TaskResult = Dts.Results.Success
End Sub
End Class
When i was developing ETL modules that had Script tasks, i followed couple of things to help in testing
Logging the Script task execution points - I wrote simple custom module to handle that
Seperated the functions in the script task and created a seperate Vb.net console project. I executed that project and once it was working successfully, i then included the same in the SSIS project.

SSIS Write Variable Script Task

Not sure what I'm doing wrong here - I am attempting to generate a variable which i will then pass to an FTP task.
In the Script Task, I set variable ReadWrite = User::WildCard. Currently, the User::WildCard variable has a String value of "test.csv". This script should rename the string value to "/forecasts/forecast_20120730.csv" for today. As of now, the script runs successfully, however the variable is not written.
#Region "Imports"
Imports System
Imports System.Data
Imports System.Collections
Imports System.Math
Imports Microsoft.SqlServer.Dts.Runtime
Imports System.IO
Imports System.Text
Imports System.Windows.Forms
#End Region
'ScriptMain is the entry point class of the script. Do not change the name, attributes,
'or parent of this class.
<Microsoft.SqlServer.Dts.Tasks.ScriptTask.SSISScriptTaskEntryPointAttribute()> _
<System.CLSCompliantAttribute(False)> _
Partial Public Class ScriptMain
Inherits Microsoft.SqlServer.Dts.Tasks.ScriptTask.VSTARTScriptObjectModelBase
'This method is called when this script task executes in the control flow.
'Before returning from this method, set the value of Dts.TaskResult to indicate success or failure.
'To open Help, press F1.
Enum ScriptResults
Success = Microsoft.SqlServer.Dts.Runtime.DTSExecResult.Success
Failure = Microsoft.SqlServer.Dts.Runtime.DTSExecResult.Failure
End Enum
Public Sub Main()
Dim FilePart As String
FilePart = Format(Now, "yyyyMMdd")
Dts.Variables("WildCard").Value = "/forecasts/forecast_" & FilePart & "*.csv"
Dts.TaskResult = ScriptResults.Success
End Sub
End Class
In your code, I dont see a LockForWrite anywhere. I think you need that in order to write back to a variable. Here is an excellent and simple example from Jaime Thomson's blog on how to do it. Hope this helps.