Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

SSIS Script Task Error

Posted on 2012-03-28
11
Medium Priority
?
4,583 Views
Last Modified: 2012-03-31
SSIS Script Task.

I am using a ForEach Container to get the file as a variable (User::foundFiles).   I am using it in a previously built DataFlow Task so I know it works just fine for that.

Within this Script it's throwing an error saying "Empty path name is not legal"

What am I missing?  Can't figure out why it's not working.

Error:

Error: System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. ---> System.ArgumentException: Empty path name is not legal.
   at System.IO.FileStream.Init(String path, FileMode mode, FileAccess access, Int32 rights, Boolean useRights, FileShare share, Int32 bufferSize, FileOptions options, SECURITY_ATTRIBUTES secAttrs, String msgPath, Boolean bFromProxy)
   at System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize, FileOptions options)
   at System.IO.StreamWriter.CreateFile(String path, Boolean append)
   at System.IO.StreamWriter..ctor(String path, Boolean append, Encoding encoding, Int32 bufferSize)
   at System.IO.StreamWriter..ctor(String path, Boolean append, Encoding encoding)
   at System.IO.File.WriteAllText(String path, String contents, Encoding encoding)
   at ST_c71263698669469292bfc5b447ef5fa6.csproj.ScriptMain.Main()
   --- End of inner exception stack trace ---
   at System.RuntimeMethodHandle._InvokeMethodFast(Object target, Object[] arguments, SignatureStruct& sig, MethodAttributes methodAttributes, RuntimeTypeHandle typeOwner)
   at System.RuntimeMethodHandle.InvokeMethodFast(Object target, Object[] arguments, Signature sig, MethodAttributes methodAttributes, RuntimeTypeHandle typeOwner)
   at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture, Boolean skipVisibilityChecks)
   at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
   at System.RuntimeType.InvokeMember(String name, BindingFlags bindingFlags, Binder binder, Object target, Object[] providedArgs, ParameterModifier[] modifiers, CultureInfo culture, String[] namedParams)
   at System.Type.InvokeMember(String name, BindingFlags invokeAttr, Binder binder, Object target, Object[] args, CultureInfo culture)
   at Microsoft.SqlServer.Dts.Tasks.ScriptTask.VSTATaskScriptingEngine.ExecuteScript()

Code:

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

namespace ST_c71263698669469292bfc5b447ef5fa6.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
       
        public void Main()
        {
            string fileContent = System.IO.File.ReadAllText(Dts.Variables["foundFiles"].Value.ToString());
            string delimiterInfile = fileContent.Substring(103, 1); //103 because it's zero-based
            fileContent = fileContent.Replace(delimiterInfile, "*");
            string delimiterInfile2 = fileContent.Substring(104, 1); //104 because it's zero-based
            fileContent = fileContent.Replace(delimiterInfile2, ":");
            System.IO.File.WriteAllText(Dts.Variables["User::OutputFile"].Value.ToString(), fileContent);
            Dts.TaskResult = (int)ScriptResults.Success;
        }
    }
}
0
Comment
Question by:simplyfemales
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 4
11 Comments
 
LVL 21

Expert Comment

by:Jason Yousef, MS
ID: 37782029
Are you sure the 'OutputFile' variable's value is not empty and has correct path with file name?
0
 

Author Comment

by:simplyfemales
ID: 37782320
At this point I'm not sure of anything and am a relative newb in Scripting.  The Script was supplied by someone else.  It builds just fine, but the error comes from the package execution.

What would you suggest the value be and how do I assign the path name to it?

Within the ForEach Container I have the Collection set to the proper folder path and it's instructed to collect *.txt and also include the fully qualified filename.

This is then mapped to the variable User::foundFiles (Package, String, no value)

OutputFile has been set up as User::OutputFile (Package, String, no value)

As indicated before, the foundFiles variable was previously used for a DataFlow task and worked just fine with multiple files in the directory (it goes through the directory and addresses each file independently without problem).  The intention of this script is to read each file in the directory (which should be identified as the User::foundFiles variable and looped through via the ForEach Container), replace the characters located at position 103 and 104 and then output the file so that they are then available to the DataFlow Task which previously used the User::foundFiles variable but has now been reassigned to use the User:OutputFile variable.

The package is failing at the Script so it's not even getting to the output.... or at least it does not appear to be making it that far.

FYI.  When looking at the Variable Mappings within the ForEach Loop Container, the Variable indicates User::foundFiles and the Index shows the number 0 (zero).
0
 

Author Comment

by:simplyfemales
ID: 37782577
New info.  When I comment out the line

System.IO.File.WriteAllText(Dts.Variables["User::OutputFile"].Value.ToString(), fileContent);

the script runs successfully.

What is occurring with the OutputFile line and how to fix it?
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 21

Expert Comment

by:Jason Yousef, MS
ID: 37782611
You need to add a value to that variable, so the script can write the files to.

try setting a local path and see what would happen.
0
 

Author Comment

by:simplyfemales
ID: 37782659
Huslayer.  I set a local path on the User::OutputFile variable c:\localpath\1.txt and it works just fine.

Now, this is supposed to write to a variable, not to a fixed location.  The variable is then supposed to be available to a DataFlow task.  How do I make that work?  I obviously don't want it to write to a new file, rather I want it available as the variable inside the Loop.
0
 
LVL 21

Accepted Solution

by:
Jason Yousef, MS earned 1500 total points
ID: 37782716
2 ways, y u can write to a clean txt file like what you did, then use that as a clean source.

or you can use the script task as source...

there are many examples if you google it

http://microsoft-ssis.blogspot.com/2011/02/script-component-as-source.html
0
 

Author Comment

by:simplyfemales
ID: 37782994
huslayer.  A little bit confused.  If I commit the results of the script to a variable, then that variable should be available without it being defined as a "source" by a script, shouldn't it?

I'm using this in an EDI workflow, and more specifically, using the CozyRoc EDI Source within the DataFlow task.  It has the ability to utilize a variable source and so I have set it to use the User:OutputFile variable as it's source.

Obviously there's a disconnect occurring where the ControlFlow Script is not placing the information anywhere, even though it seems that it's being told to place it in the OutputFile Variable?  I'm not sure why it's not committing this value anywhere?  My assumption is that this Variable is supposed to just be random memory that gets committed and is available for use as a global variable but it isn't being populated by the script for some reason.

Anyways.  Excellent link and I have bookmarked it for future use, but as the EDI Source component can't receive any inputs in the normal fashion, I can't connect it to a Script Source.  I strictly have to use the variable.
0
 
LVL 21

Expert Comment

by:Jason Yousef, MS
ID: 37783022
how big is the contents of the file?
I'm not familiar with the cozyroc but you could ask for their support or read on what type of variable is supported: string, ojbect or even text
0
 

Author Comment

by:simplyfemales
ID: 37783105
file contents are variable.  1K up to 300K or so.

Sent an email to cozyRoc, but not sure anything gets past the point that I assume the code of this line -----

System.IO.File.WriteAllText(Dts.Variables["User::OutputFile"].Value.ToString(), fileContent);

is telling the system to write the values within fileContent to the Variable OutputFile, correct?

If it's a variable, it should not need a fixed location, right?

If it's a variable, it's committed to memory rather than a physical location?

I'm missing something here, but I just don't know enough to know what it is.  I am stuck on the thought though that the variable is not a fixed value and should not need to be.
0
 
LVL 37

Expert Comment

by:ValentinoV
ID: 37786905
Some explanation:

System.IO.File.WriteAllText(Dts.Variables["User::OutputFile"].Value.ToString(), fileContent);

That line is creating a new file on the file system.  The content of that file is the content of the fileContent variable.  The path and location of the file is provided through the OutputFile package variable.

To be able to make the output file name dynamic, you should put only the path as value for the package variable, and pass another value into the script that can help you create a filename.  The User::foundFiles which you use in your loop would make a good candidate.

Possibly you'll need to do some further string manipulation, depending on how you set up the loop: "name only", "fully qualified", "name and extension"?

Assuming "name only", you could do this:

string outFile = Dts.Variables["User::OutputFile"].Value.ToString() + "\\" + Dts.Variables["User::foundFiles"].Value.ToString() + ".txt";
System.IO.File.WriteAllText(outFile, fileContent);

The above assumes your OutputFile package var does not end in a backslash and that foundFiles contains the name of the file without extension.
0
 

Author Closing Comment

by:simplyfemales
ID: 37792131
turns out the source needs to reference a fixed file and not a variable.  I dumped the variable to a fixed temp file and it overwrites each time as the loop works its way through.

Thanks for the help
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
In the first part of this tutorial we will cover the prerequisites for installing SQL Server vNext on Linux.
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.

721 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question