Ice
Ice

Reputation: 479

SSIS script task error message

I received the following error message when running SSIS package. The Script Task is using Microsoft Visual C# 2008. Can you please help me to fix the problem?

Thank you very much! I also attach error message:

Error: 2015-12-22 02:58:08.28
   Code: 0x00000001
   Source: Script Task 
   Description: System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. ---> System.InvalidOperationException: Showing a modal dialog box or form when the application is not running in UserInteractive mode is not a valid operation. Specify the ServiceNotification or DefaultDesktopOnly style to display a notification from a service application.
   at System.Windows.Forms.MessageBox.ShowCore(IWin32Window owner, String text, String caption, MessageBoxButtons buttons, MessageBoxIcon icon, MessageBoxDefaultButton defaultButton, MessageBoxOptions options, Boolean showHelp)
   at System.Windows.Forms.MessageBox.Show(String text)
   at ST_d27b216cd7d64713b54c81f6ac28d805.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()
End Error
DTExec: The package execution returned DTSER_FAILURE (1).

C# code:

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

namespace ST_d27b216cd7d64713b54c81f6ac28d805.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()
        {
            // TODO: Add your code here
            System.IO.FileInfo fi;
            String FilePath = null;

            DateTime ModifiedTime = (DateTime)Dts.Variables["File_Modified"].Value;


            DateTime LoadDate = (DateTime)Dts.Variables["File_Last_Load_Date"].Value;

            Dts.Variables["isModified"].Value = false;


            FilePath = Dts.Variables["SourceFolder"].Value.ToString();
            ModifiedTime = System.IO.File.GetLastWriteTime(FilePath);

            Dts.Variables["File_Modified"].Value = ModifiedTime; 
            // fi.LastWriteTime;
            int result = DateTime.Compare(ModifiedTime, LoadDate);

            if (result > 0)
            {
                  MessageBox.Show("File Modified after last load in staging");
                Dts.Variables["isModified"].Value = true;
            }
            else
            {
               MessageBox.Show("file is not modified since last load");
                Dts.Variables["isModified"].Value = false;
            }

            Dts.TaskResult = (int)ScriptResults.Success;
        }
    }
}

Upvotes: 1

Views: 3846

Answers (2)

GarethD
GarethD

Reputation: 69819

The error message extracted from your stack trace is:

Showing a modal dialog box or form when the application is not running in UserInteractive mode is not a valid operation. Specify the ServiceNotification or DefaultDesktopOnly style to display a notification from a service application.

You have to remember that although when you are debugging your SSIS package you have a nice UI (BIDS or SQL Server Tools shells depending on your environment) but really it is not designed to be have a UI. What would you expect to happen when this package is deployed to a server and called by a SQL Job? i.e. Where would the message box show? Who would click "OK" to allow the thread to resume?

You probably want to just fire an information event if you are looking to post feedback, something like:

bool fireAgain = false;
Dts.Events.FireInformation(0, "Script Task", "File Modified after last load in staging", String.Empty, 0, ref fireAgain);

Upvotes: 1

Alex
Alex

Reputation: 21766

The error is raised because your script task is trying to display a message box and showing a modal dialog box or form when the application is not running in UserInteractive mode is not a valid operation. Therefore if you want to output a message, you could use Dts.Log instead, see the MSDN documentation for further details.

Upvotes: 0

Related Questions