Monday, July 2, 2012

Good Learning - 2 : UnauthorizedAccessException


First of all, apologies. This post can be pretty much useless as I dont remember the exact sequence of events, neither do I remember the exact lines of code that brought this error. 

But from what I remember, I was trying to access a file (file.copy or file.move, one of those) through C# script in SSIS, and I got this error.
system.io.__error.winioerror access denied

System.UnauthorizedAccessException: Access to the path '\\myserver\myfolder\mysubfolder' is denied. 

Well, the mistake I did was that I gave the path of the file without the actual file name.
So C# was trying to create a file with the same name as "mysubfolder" under "myfolder" which was obviously not allowed because it would be duplicates
.
The solution is to provide the path like this :
\\myserver\myfolder\mysubfolder\myfilename.txt
 or whatever the extension should be.

Friday, June 29, 2012

Good Learning - 1 : StreamReader error in C# script


The Problem : I was using the StreamReader and StreamWriter C# classes in many Script tasks of my SSIS package. I was trying to read an XML file, perform some operations on it, copy the read file to another folder etc etc. After a while I started getting this weird error that "The operation cannot perform… the resource is already in use". The error was coming from the XML file which I was trying to copy from one folder to the other. I checked - my file was not physically open anywhere. What could be using it?

The Solution : The file was not physically open anywhere but it was programmatically open!  I had forgotten to do a streamReader.Close(); after my initial read. As a result, the file was still held by the streamReader object, not releasing it to the FileSystem Task which was trying to copy it to another folder.

Here is my code snippet-
StreamReader streamReader = new StreamReader(filePath);
String streamText = streamReader.ReadToEnd();
this.Dts.Variables["XMLFileContent"].Value = streamText;
streamReader.Close();
Dts.TaskResult = (int)ScriptResults.Success;

Where "filePath" is the absolute file path of my file in the folder  ( including the file name).