Software Development
Blogs and Discussion
developer.*
Books Articles Blogs Subscribe d.* Gear About Home

SSIS OnError Event Handler - How to Retrieve Error Information (Number, Description, Source)

Now that I've figured it out it seems obvious, but I had to struggle a little when creating my first OnError event handler in SQL Server 2005 SSIS. I couldn't figure out how to access the error information that triggered the event. Here's how:

Method A: Expose With the Script Task Editor

You can use the ReadOnlyVariables field on the Script Task Editor to expose these three variables:

ErrorCode
ErrorDescription
SourceName

Then you can read these values from the Dts.Variables collection. Don't use the @ symbol in front of the variable name. To list multiple variables for ReadOnlyVariables, separate them by a comma.

Method B: Get them through code

Some people may prefer to not us ReadOnlyVariables since it creates an dependency external to the script code itself. You can retrieve this variables programatically, but like my LockOneForWrite example it takes some verbose code. Here's some example code I'm using, which uses an extra procedure in the script to retrieve the system variables:

Public Sub Main()
'Assume success
Dts.TaskResult = Dts.Results.Success

Try
Dim errNumber As Integer
Dim errDescription As String
Dim errSource As String

GetErrorValues(errNumber, errDescription, errSource)
System.Windows.Forms.MessageBox.Show( _
errNumber.ToString() + vbNewLine + vbNewLine + _
errDescription + vbNewLine + vbNewLine + _
errSource)
Catch ex As Exception
' Displaying the error since this is an example.
System.Windows.Forms.MessageBox.Show(ex.ToString())
End Try
End Sub

Private Sub GetErrorValues(ByRef errNumber As Integer, _
ByRef errDescription As String, ByRef errSource As String)

Try
Dim vars As Variables

Dts.VariableDispenser.LockForRead("ErrorCode")
Dts.VariableDispenser.LockForRead("ErrorDescription")
Dts.VariableDispenser.LockForRead("SourceName")
Dts.VariableDispenser.GetVariables(vars)
Try
errNumber = CType(vars("ErrorCode").Value, Integer)
errDescription = vars("ErrorDescription").Value.ToString()
errSource = vars("SourceName").Value.ToString()
Catch ex As Exception
Throw ex
Finally
vars.Unlock()
End Try
Catch ex As SystemException
Throw ex
End Try
End Sub

Notice how the GetErrorValues() procedure returns the error information via ByRef arguments. Feel free to copy the code.

I recommend this page on Microsoft.com for a list of all SSIS system variables and when they are available.

Comments welcome.

Dan

SSIS OnError Event Handler - How to Retrieve Error Information

Thanks! This was of great assistance.

Retrieve Error information

Thanks, this was very useful tip.

Capturing task error status

Thanks so much for your help! I struggled with this one for a bit. I was so close! Your example really helped.

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.

Recent comments

User login

About our advertising.

Atom Feed

developer.* Blogs also has an Atom feed, located at this url.

Click here for more information about Atom.

A Jolt Award Finalist
Software Creativity 2.0
Foreword by Tom DeMarco

Recent Posters

Based on most recent 60 days, sorted by # of posts and name.

Google
Web developer.*

Who's online

There are currently 1 user and 24 guests online.

Online users

Syndicate

Syndicate content
All views expressed by authors, bloggers, and commentors are their own and do not necessarily reflect the views of developer.* or its proprietors.
Click to read the Copyright Notice.

All content copyright ©2000-2005 by the individual specified authors (and where not specified, copyright by Read Media, LLC). Reprint or redistribute only with written permission from the author and/or developer.*.

www.developerdotstar.com