ArcGIS ClipShip geoprocessing
ArcGIS_ClipShip_Geoprocessing_CSharp\App_Code\Utility.cs
// Copyright 2011 ESRI
// 
// All rights reserved under the copyright laws of the United States
// and applicable international laws, treaties, and conventions.
// 
// You may freely redistribute and use this sample code, with or
// without modification, provided you include the original copyright
// notice and use restrictions.
// 
// See the use restrictions.
// 

public class Utility
{
    /// <summary>
    /// Constructs a callback result that will display a javascript alert with an error message 
    /// based on the passed-in exception
    /// </summary>
    /// <param name="exception">The exception from which the error message will be derived</param>
    /// <returns></returns>
    public static ESRI.ArcGIS.ADF.Web.UI.WebControls.CallbackResult CreateErrorCallbackResult(
        System.Exception exception)
    {
        // Create a callback result to display an error message
        string jsAlertErrorMessage = GetJavaScriptErrorString(exception);
        ESRI.ArcGIS.ADF.Web.UI.WebControls.CallbackResult alertCallbackResult =
            ESRI.ArcGIS.ADF.Web.UI.WebControls.CallbackResult.CreateJavaScript(jsAlertErrorMessage);
        return alertCallbackResult;
    }

    /// <summary>
    /// Constructs the syntax to display a javascript alert with an error message based on the 
    /// passed-in exception
    /// </summary>
    /// <param name="exception">The exception from which the error message will be derived</param>
    /// <returns></returns>
    public static string GetJavaScriptErrorString(System.Exception exception)
    {
        // Get the website's configuration file
        System.Configuration.Configuration webConfig =
        System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration(
            System.Web.HttpContext.Current.Request.ApplicationPath);

        // Get the "compilation" section of the config file
        System.Web.Configuration.CompilationSection compilationSection =
            webConfig.GetSection("system.web/compilation") as
            System.Web.Configuration.CompilationSection;

        // If the config file's compilation section specifies debug mode, include 
        // stack trace information in the error message.  Otherwise, just return 
        // the exception message.
        string errorMessage = null;
        if ((compilationSection != null) && (compilationSection.Debug))
        {
            string stackTrace = exception.StackTrace.Replace("\\", "\\\\");
            stackTrace = stackTrace.Replace("\n", "\\n");
            stackTrace = stackTrace.Replace("\r", "\\r");
            stackTrace = stackTrace.Replace("'", "\\'");
            errorMessage = exception.Message.Replace("\\", "\\\\");
            errorMessage = errorMessage.Replace("\n", "\\n");
            errorMessage = errorMessage.Replace("\r", "\\r");
            errorMessage = errorMessage.Replace("'", "\\'");

            errorMessage = errorMessage + "\\n\\n" + stackTrace.Trim();
        }
        else
            errorMessage = exception.Message;

        // Create a callback result to display an error message
        string jsAlertException = "alert('" + errorMessage + "')";
        return jsAlertException;
    }


    /// <summary>
    /// Finds the node having the passed-in name from among the input node or its child nodes. 
    /// Useful for searching a Toc with group layers.
    /// </summary>
    /// <param name="treeViewPlusNode"></param>
    /// <param name="nodeName"></param>
    /// <returns></returns>
    public static ESRI.ArcGIS.ADF.Web.UI.WebControls.TreeViewPlusNode FindNodeRecursive(
        ESRI.ArcGIS.ADF.Web.UI.WebControls.TreeViewPlusNode treeViewPlusNode, string nodeName)
    {
        // Check whether the text of the passed-in node matches the text sought.  Return the node if so.
        if (treeViewPlusNode.Text == nodeName)
            return treeViewPlusNode;

        // Iterate through the passed-in node's child nodes, calling this function on each.
        foreach (ESRI.ArcGIS.ADF.Web.UI.WebControls.TreeViewPlusNode childTreeViewPlusNode in treeViewPlusNode.Nodes)
        {
            ESRI.ArcGIS.ADF.Web.UI.WebControls.TreeViewPlusNode childNode =
                FindNodeRecursive(childTreeViewPlusNode, nodeName);
            if (childNode != null)
                return childNode;
        }

        // If the code reaches this point, no match was found. 
        return null;
    }
}