Substring on SSIS Variable expression - c#

I Have SSIS Variable Expression like this,
#[System::PackageName]+","+
#[System::SourceName]+","+
(DT_STR,15,1252) #[System::ErrorCode]+","+
#[System::ErrorDescription]+","+
(RIGHT((DT_WSTR,4)
DATEPART("yyyy",GetDate()),4)+"/"+
RIGHT("0"+(DT_WSTR , 2)
DATEPART("mm", GetDate()),2)+"/"+
RIGHT("0"+(DT_WSTR,2)
DATEPART("dd",GetDate()),2)+"_"+
RIGHT("0"+(DT_WSTR,2)
DATEPART("HH",GetDate()),2)+":"+
RIGHT("0"+(DT_WSTR,2)
DATEPART("MM",GetDate()),2)+":"+
RIGHT("0"+(DT_WSTR,2)
DATEPART("SS",GetDate()),2))+","+
#[System::MachineName]
Now I want to get some string from #[System::ErrorDescription] which located before at , here's the sample of the #[System::ErrorDescription] value
System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. ---> System.IO.DirectoryNotFoundException: Could not find a part of the path 'D:\blablablabl\yes.txt'.
at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath)
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, String msgPath, Boolean bFromProxy)
at System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize)
at System.Xml.XmlDownloadManager.GetStream(Uri uri, ICredentials credentials)
at System.Xml.XmlUrlResolver.GetEntity(Uri absoluteUri, String role, Type ofObjectToReturn)
at System.Xml.XmlTextReaderImpl.OpenUrlDelegate(Object xmlResolver)
at System.Threading.CompressedStack.runTryCode(Object userData)
at System.Runtime.CompilerServices.RuntimeHelpers.ExecuteCodeWithGuaranteedCleanup(TryCode code, CleanupCode backoutCode, Object userData)
at System.Threading.CompressedStack.Run(CompressedStack compressedStack, ContextCallback callback, Object state)
what i've done is write the substring code on my variable expressions :
SUBSTRING(#[System::ErrorDescription], 1, FINDSTRING(#[System::ErrorDescription], "at", 1)-1)+","+
but its always give me an error , that substring value cannot be negative, whats wrong with my code?

If the result of FINDSTRING(#[System::ErrorDescription], "at", 1) is zero (i.e. string not found) then the last parameter to your substring expression becomes -1. It's a common problem.
Perhaps you could try evaluating whether it is zero, and only use Findstring if it isn't. I have split the expression below for clarity, but it should all be on one line. I don't have SSIS to test but see if it works.
I don't think you've posted your full expression but this should give you the idea.
FINDSTRING(#[System::ErrorDescription], "at", 1)==0
?
"Nothing Found"
:
SUBSTRING(
#[System::ErrorDescription],
1,
FINDSTRING(#[System::ErrorDescription], "at", 1)-1
)
Here's a link that describes conditional:
http://msdn.microsoft.com/en-us/library/ms141680.aspx
I'm not convinced this is the full story, but try it and it will flush out your next issue.

Related

Visual Studio 2017: Text Template Custom Host: Exception in Code Analysis Metadata Reference

I'm trying to use this example from Microsoft Docs on how to create a custom host for text template generation.
Every time I attempt to process a template using the host I get the following exception. I've spent the last several hours attempting to find an answer from Google, but apparently my Google Fu just isn't working today.
An exception was thrown while trying to compile the transformation code. The following Exception was thrown:
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, Boolean useLongPath, Boolean checkHost)
at System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share)
at Roslyn.Utilities.FileUtilities.OpenFileStream(String path)
at Microsoft.CodeAnalysis.MetadataReference.CreateFromFile(String path, MetadataReferenceProperties properties, DocumentationProvider documentation)
at Microsoft.VisualStudio.TextTemplating.CompilerBridge.<>c.<.ctor>b__15_0(String x)
at System.Linq.Enumerable.WhereSelectListIterator`2.MoveNext()
at System.Linq.Enumerable.<UnionIterator>d__66`1.MoveNext()
at System.Linq.Enumerable.<UnionIterator>d__66`1.MoveNext()
at System.Linq.Buffer`1..ctor(IEnumerable`1 source)
at System.Linq.Enumerable.ToArray[TSource](IEnumerable`1 source)
at System.Collections.Immutable.ImmutableArray.CreateRange[T](IEnumerable`1 items)
at Microsoft.CodeAnalysis.ImmutableArrayExtensions.AsImmutableOrEmpty[T](IEnumerable`1 items)
at Microsoft.CodeAnalysis.Compilation.ValidateReferences[T](IEnumerable`1 references)
at Microsoft.CodeAnalysis.CSharp.CSharpCompilation.WithReferences(IEnumerable`1 references)
at Microsoft.CodeAnalysis.CSharp.CSharpCompilation.CommonWithReferences(IEnumerable`1 newReferences)
at Microsoft.VisualStudio.TextTemplating.CompilerBridge.PrepareNewCompilation()
at Microsoft.VisualStudio.TextTemplating.CompilerBridge.Compile()
at Microsoft.VisualStudio.TextTemplating.TransformationRunner.Compile(String source, String inputFile, IEnumerable`1 references, Boolean debug, SupportedLanguage language, String compilerOptions)

System.Unauthorized Exception

*
Exception =System.Net.WebException: Access to the path
'c:\windows\system32\inetsrv\Dialer' is denied. --->
System.UnauthorizedAccessException: Access to the path
'c:\windows\system32\inetsrv\Dialer' is denied. at
System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath)
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, Boolean useLongPath) at
System.IO.FileStream..ctor(String path, FileMode mode, FileAccess
access, FileShare share, Int32 bufferSize, FileOptions options, String
msgPath, Boolean bFromProxy) at System.IO.FileStream..ctor(String
path, FileMode mode, FileAccess access, FileShare share) at
System.Net.FileWebStream..ctor(FileWebRequest request, String path,
FileMode mode, FileAccess access, FileShare sharing) at
System.Net.FileWebRequest.GetRequestStreamCallback(Object state)
--- End of inner exception stack trace --- at System.Net.WebClient.UploadDataInternal(Uri address, String method,
Byte[] data, WebRequest& request) at
System.Net.WebClient.UploadString(Uri address, String method, String
data)
*
Code where the exception is occurring
using (var writer = new StringWriter())
{
JsonSerializer.Create().Serialize(writer, payLoad);
var result =client.UploadString(commDialerApiUrl,writer.ToString());
return ((T)JsonConvert.DeserializeObject(result, typeof(T)));
}
I know the code above is not ideal, however I just need to understand why even an attempt to access is made. Can anyone provide some insight please?
All right, the issue was very simple. The code was trying to make a Restful post to a blank Uri. Since the Uri was blank webclient was trying to resolve the call to a file (having the same name as the service) in inetpub.
Since no such file path was invalid, thus the i/o error.

IIS 7 Access to the path is denied when trying to generate report

We are getting this error message upon trying to generate a report not accessing a file. We are using IIS 7.
XXX is located on a different server than the web server.
2014-10-24 11:04:13,287 [5] ERROR FOUNDATION [(null)] - Access to the path '\\XXX \file2022.pdf' is denied.
System.UnauthorizedAccessException: Access to the path '\\XXX\file.pdf' is denied.
at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath)
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, String msgPath, Boolean bFromProxy)
at System.IO.FileStream..ctor(String path, FileMode mode)
at CrystalDecisions.CrystalReports.Engine.FormatEngine.WriteStreamToFile(Stream stream, String filename)
at CrystalDecisions.CrystalReports.Engine.FormatEngine.ExportStreamToFile(Stream stream, DiskFileDestinationOptions options)
at CrystalDecisions.CrystalReports.Engine.FormatEngine.Export(ExportRequestContext reqContext)
at CrystalDecisions.CrystalReports.Engine.ReportDocument.ExportToDisk(ExportFormatType formatType, String fileName)
at GlobalFunctions.GenerateCrystalReport(String reportFileName, String outputFileName, List`1 params) in C:\NetProjects\branches\R7\GUIs\Foundation\App_Code\GlobalFunctions.vb:line 347
If you must access files like this make sure your Application Pool is running as a domain user with read permission to that location.

Write on Excel file while it is open by another user

I'm trying to edit an opened excel file (by another user in a shared network) in order to make some changes to it and save it back. But when I try to open the file I get the following error :
Message=The process cannot access the file
'Y:\COMMON\SharedFolders\Export.xlsx', because it is being used by another process.
Source=mscorlib
StackTrace:
à System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath)
à 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, Boolean useLongPath, Boolean checkHost)
à System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize, FileOptions options, String msgPath, Boolean bFromProxy, Boolean useLongPath, Boolean checkHost)
à System.IO.File.InternalReadAllBytes(String path, Boolean checkHost)
à System.IO.File.ReadAllBytes(String path)
à OfficeOpenXml.ExcelPackage.ReadFile()
à OfficeOpenXml.ExcelPackage.ConstructNewFile(Stream stream, String password)
à OfficeOpenXml.ExcelPackage..ctor(FileInfo newFile)
By the way, I'm using the EPPLUS library.
You can't. That being said you can look into some "source-safe" solution (like svn, sharepoint or google docs). You can also implement some "poor man" source safe yourself - i.e. small web application that will act as proxy over file system & will allow you to upload file, list folder, download files.
Other option would be to delay update to date when no-one will be accessing that file.
Google allow multiple edit at the same time... maybe you should try to move that file there if possible.

Hash character in path throws DirectoryNotFoundException

Consider the following code snippet
private void ProcessFile(string fullPath) {
XmlTextReader rdr = new XmlTextReader("file:\\\\" + fullPath);
while (rdr.Read()) {
//Do something
}
return;
}
Now, this functions fine when passed a path like:
"C:\Work Files\Technical Information\Dummy.xml"
But throws an error when passed
"C:\Work Files\#Technical Information\Dummy.xml"
Note that all folders and files specified exist and that the hash character is a valid character for paths. The error details are:
System.IO.DirectoryNotFoundException: Could not find a part of the path 'C:\Work Files\'.
at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath)
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)
at System.Xml.XmlDownloadManager.GetStream(Uri uri, ICredentials credentials)
at System.Xml.XmlUrlResolver.GetEntity(Uri absoluteUri, String role, Type ofObjectToReturn)
at System.Xml.XmlTextReaderImpl.OpenUrlDelegate(Object xmlResolver)
at System.Threading.CompressedStack.runTryCode(Object userData)
at System.Runtime.CompilerServices.RuntimeHelpers.ExecuteCodeWithGuaranteedCleanup(TryCode code, CleanupCode backoutCode, Object userData)
at System.Threading.CompressedStack.Run(CompressedStack compressedStack, ContextCallback callback, Object state)
at System.Xml.XmlTextReaderImpl.OpenUrl()
at System.Xml.XmlTextReaderImpl.Read()
at System.Xml.XmlTextReader.Read()
Anybody know what's going on?
Adding to Konrad's answer, if you are using the file:// protocol, you need to use %23 for # then it works fine
Try omitting the file:/// protocol prefix. It works for me without one. I believe .NET will truncate any part after the # if it believes this to be a URL. This is only a guess based on the error message but it seems logical considering that the part after the # character isn't processed by the server but rather by the client in other scenarios (e.g. web browsers).
Why don't you use
XmlTextReader rdr = new XmlTextReader(fullPath);

Categories

Resources