Category Archives: Samples

Insert File Plugin

Insert File PluginI’m thinking it’s about time I start providing downloadable sample code along with my posts.  Rather than copying and pasting code snippets into your own solution, it might be nice to alternatively download a compressed sample project. 

The Insert File Plugin for Windows Live Writer has been around for a while.  Here’s my attempt at trying it out.  It’s documented to be tested with WordPress.  Wish me luck… 

Download Compressed Insert File Plugin Sample Project: InsertFilePluginTest.zip

Update: It worked like a charm.  I wonder if it will still work with a post update from WLW?

Update: Yes, it does.  :)

C# Cache Helper Class

Do you need a quick and cache wrapper class? Here’s a static class which I included in my more recent C# web application.  You’ll notice the class uses generics to allow for some, umm, generic functionality. 

public static class CacheHelper
{
    /// <summary>
    /// Insert value into the cache using
    /// appropriate name/value pairs
    /// </summary>
    /// <typeparam name="T">Type of cached item</typeparam>
    /// <param name="o">Item to be cached</param>
    /// <param name="key">Name of item</param>
    public static void Add<T>(T o, string key) where T : class
    {
        // NOTE: Apply expiration parameters as you see fit.
        // In this example, I want an absolute 
        // timeout so changes will always be reflected 
        // at that time. Hence, the NoSlidingExpiration.  
        HttpContext.Current.Cache.Insert(
            key, 
            o, 
            null,
            DateTime.Now.AddMinutes(
                ConfigurationHelper.CacheExpirationMinutes),
            System.Web.Caching.Cache.NoSlidingExpiration);
    }

    /// <summary>
    /// Remove item from cache 
    /// </summary>
    /// <param name="key">Name of cached item</param>
    public static void Clear(string key)
    {
        HttpContext.Current.Cache.Remove(key);
    }

    /// <summary>
    /// Check for item in cache
    /// </summary>
    /// <param name="key">Name of cached item</param>
    /// <returns></returns>
    public static bool Exists(string key)
    {
        return HttpContext.Current.Cache[key] != null;
    }

    /// <summary>
    /// Retrieve cached item
    /// </summary>
    /// <typeparam name="T">Type of cached item</typeparam>
    /// <param name="key">Name of cached item</param>
    /// <returns>Cached item as type</returns>
    public static T Get<T>(string key) where T : class
    {
        try
        {
            return (T) HttpContext.Current.Cache[key];
        }
        catch
        {
            return null;
        }
    }
}

And here is a relatively standard sample usage of the library. 

public override List<Employee> GetEmployeeList()
{
    string key = ConfigurationHelper.CacheKeyEmployeeList;

    List<Employee> employees = CacheHelper.Get<List<Employee>>(key);

    if (employees == null)
    {
        employees = instance.GetEmployeeList();
        CacheHelper.Add(employees, key);
    }

    return employees;
}

Notice how I’m grabbing the cached value, storing it in a local variable and then checking if it is equal to null rather than using the CacheHelper.Exists() method.  If I used the CacheHelper.Exists() method, the cached object could expire between the time I check its existence and the time I get its value through the CacheHelper.Get() method.  Therefore, the above approach is the safest strategy to use when retrieving cached values.  CacheHelper.Exists() should really only be used for quick existence checks which are unrelated to the fetch.

But if you want to use the code CORRECTLY there’s a catch.  Did you notice the “class” constraint on the CacheHelper.Get() and CacheHelper.Add() methods?   I did this because you can’t always return null from a generic method.  If the return type were always a reference type it would be fine, but comparing a non-nullable value type to null would throw a runtime exception or would always evaluate to false.  Therefore, I’ve constrainted CacheHelper which limits its functionality but unsure safe use of the cache.  If you’re feeling dangerous, you’re welcome to remove the constraints.

I’ll update the library once I come up with a good work around.  Speaking of, any suggestions?

Let me know if you have any questions and/or this type of post is helpful.  Thanks.

UPDATE 12/10:

After further review, I’ve removed the “class” constraint from the CacheHelper.  The Get() method now follows the common Try(x, out y) pattern where x is what you wish to operate on, y is the resulting value and the method return success or failure.  Here’s a sample call:

string key = "EmployeeList";
List<Employee> employees;

if (!CacheHelper.Get(key, out employees))
{
    employees = DataAccess.GetEmployeeList();
    CacheHelper.Add(employees, key);
    Message.Text =
        "Employees not found but retrieved and added to cache for next lookup.";
}
else
{
    Message.Text = "Employees pulled from cache.";
}

And here’s the updated class.  Note, I’ve also provided a sample project for download.  Thanks for your comments!

using System;
using System.Web;

public static class CacheHelper
{
    /// <summary>
    /// Insert value into the cache using
    /// appropriate name/value pairs
    /// </summary>
    /// <typeparam name="T">Type of cached item</typeparam>
    /// <param name="o">Item to be cached</param>
    /// <param name="key">Name of item</param>
    public static void Add<T>(T o, string key) 
    {
        // NOTE: Apply expiration parameters as you see fit.
        // I typically pull from configuration file.

        // In this example, I want an absolute
        // timeout so changes will always be reflected
        // at that time. Hence, the NoSlidingExpiration.
        HttpContext.Current.Cache.Insert(
            key,
            o,
            null,
            DateTime.Now.AddMinutes(1440),
            System.Web.Caching.Cache.NoSlidingExpiration);
    }

    /// <summary>
    /// Remove item from cache
    /// </summary>
    /// <param name="key">Name of cached item</param>
    public static void Clear(string key)
    {
        HttpContext.Current.Cache.Remove(key);
    }

    /// <summary>
    /// Check for item in cache
    /// </summary>
    /// <param name="key">Name of cached item</param>
    /// <returns></returns>
    public static bool Exists(string key)
    {
        return HttpContext.Current.Cache[key] != null;
    }

    /// <summary>
    /// Retrieve cached item
    /// </summary>
    /// <typeparam name="T">Type of cached item</typeparam>
    /// <param name="key">Name of cached item</param>
    /// <param name="value">Cached value. Default(T) if 
    /// item doesn't exist.</param>
    /// <returns>Cached item as type</returns>
    public static bool Get<T>(string key, out T value) 
    {
        try
        {
            if (!Exists(key))
            {
                value = default(T);
                return false;
            }

            value =  (T) HttpContext.Current.Cache[key];
        }
        catch
        {
            value = default(T);
            return false;
        }

        return true;
    }
}

Download CacheHelper Sample Project:CacheHelperClass.zip

 

Guarding Against Multiple Empty Strings

Many of my C# methods include what is referred to as a guard clause.  It isn’t a complicated concept.  Simply the first few statements of a routine validates passed-in parameters and/or state of the object and immediately returns an error or gracefully exits to the function is constraints aren’t met.  If I’m not mistaken, this plays nicely with the Design by Contract approach to software design, but I no expect on the subject.  The bottom line is, a guard clause can really tighten and clean up your code because it will undoubtedly eliminate any number of nested conditional statements.

Consider the following sample taken directly from c2.com:

public Foo merge(Foo a, Foo b)
{
    Foo result;
    if (a != null)
    {
        if (b != null)
        {
            // complicated merge code goes here.
        }
        else
        {
            result = a;
        }
    }
    else
    {
        result = b;
    }
    return result;
}

Now with the guard clause…

public Foo merge(Foo a, Foo b)
{
    if (a == null) return b;
    if (b == null) return a;
    // complicated merge code goes here.
}

Much cleaner, eh? And that’s only two conditionals! Back to my point.  Many of my method include guard clauses.  Today I needed to validate that all three string parameters had a value.  In other words, each string’s length was greater than 0.

I started with my faithful Is-Not-Null-Or-Empty check:

string a = "a";
string b = "b";
string c = "";

if (string.IsNullOrEmpty(a) ||
    string.IsNullOrEmpty(b) ||
    string.IsNullOrEmpty(c))
    return;

But then I settled on this:

string a = "a";
string b = "b";
string c = "";

if (a.Length * b.Length * c.Length == 0) return;

What do you think?  Pretty hokey, right?

 

Export GridView to Excel within an UpdatePanel

There’s a ton of information online about exporting a DataGrid or GridView to Excel, but most variations do not consider the GridView may reside within an UpdatePanel.  It goes without saying, but I was disappointed when I recently dusted off my “Export GridView to Excel” code snippet and encountered a number of exceptions.  So I revisited a number of links and I collected a working solution.  If you need a “simple” way to export your GridView to Excel and you are using an UpdatePanel, I hope the following code finds you well:

protected void btnExport_Click(object sender, EventArgs e)
{
    // Reference your own GridView here
    if (AccountGrid.Rows.Count > 65535)
    {
        DisplayError("Export to Excel is not allowed" +
            "due to excessive number of rows.");
        return;
    }

    string filename = String.Format("Results_{0}_{1}.xls",
        DateTime.Today.Month.ToString(), DateTime.Today.Year.ToString());

    Response.Clear();
    Response.AddHeader("Content-Disposition", "attachment;filename=" + filename);
    Response.Charset = "";

    // SetCacheability doesn't seem to make a difference (see update)
    Response.Cache.SetCacheability(System.Web.HttpCacheability.NoCache);  

    Response.ContentType = "application/vnd.xls";

    System.IO.StringWriter stringWriter = new System.IO.StringWriter();
    System.Web.UI.HtmlTextWriter htmlWriter = new HtmlTextWriter(stringWriter);

    // Replace all gridview controls with literals
    ClearControls(AccountGrid);

    // Throws exception: Control 'ComputerGrid' of type 'GridView'
    // must be placed inside a form tag with runat=server.
    // ComputerGrid.RenderControl(htmlWrite);

    // Alternate to ComputerGrid.RenderControl above
    System.Web.UI.HtmlControls.HtmlForm form
        = new System.Web.UI.HtmlControls.HtmlForm();
    Controls.Add(form);
    form.Controls.Add(AccountGrid);
    form.RenderControl(htmlWriter);

    Response.Write(stringWriter.ToString());
    Response.End();
}

private void ClearControls(Control control)
{
    for (int i = control.Controls.Count - 1; i >= 0; i--)
    {
        ClearControls(control.Controls[i]);
    }

    if (!(control is TableCell))
    {
        if (control.GetType().GetProperty("SelectedItem") != null)
        {
            LiteralControl literal = new LiteralControl();
            control.Parent.Controls.Add(literal);
            try
            {
                literal.Text =
                    (string)control.GetType().GetProperty("SelectedItem").
                        GetValue(control, null);
            }
            catch
            {}
            control.Parent.Controls.Remove(control);
        }
        else if (control.GetType().GetProperty("Text") != null)
        {
            LiteralControl literal = new LiteralControl();
            control.Parent.Controls.Add(literal);
            literal.Text =
                (string)control.GetType().GetProperty("Text").
                    GetValue(control, null);
            control.Parent.Controls.Remove(control);
        }
    }
    return;
}
 

Update: 7/30/2008

I previously noted that SetCacheability doesn’t seem to make a difference.  Well, I was right…until I deployed my code to a site behind SSL.  As it turns out, in order for Internet Explorer to open documents in Office (or any out-of-process, ActiveX document server), Internet Explorer must save the file to the local cache directory and ask the associated application to load the file by using IPersistFile::Load.

http://support.microsoft.com/default.aspx?scid=KB;EN-US;q316431&

If the file is not stored to disk, this operation fails. When Internet Explorer communicates with a secure Web site through SSL, Internet Explorer enforces any no-cache request. If the header or headers are present, Internet Explorer does not cache the file. Consequently, Office cannot open the file.

RESOLUTION: Web sites that want to allow this type of operation should remove the no-cache header or headers. In other words, comment out the following line of code particularly if you are running under SSL:

Response.Cache.SetCacheability(System.Web.HttpCacheability.NoCache); 

 

Additional Comments

Per Jinath Blog, if you are using an UpdatePanel, you may get a System.WebForms.PageRequestManagerParserErrorException exception.  The solution is to add a PostBackTrigger and give it’s ControlID as the excel export button’s ID or you can move your excel export button out side of the update panel.  I verified both options and they work great.  I ultimately went with the former option as such:

    ...
    </ContentTemplate>
    <Triggers>
        <asp:PostBackTrigger ControlID="btnExport" />
    </Triggers>
</asp:UpdatePanel>
...

Per ASPAlliance, you may encounter issues a number of issues which require the following solution.  Check out the link (and the comments) for more details if you get stuck.  I only encountered #1 on the list.

  1. You may get an exception which states your Control ‘Grid’ of type ‘GridView’ must be placed inside a form tag with runat=server.  I overcame this by dynamically adding a form to the page and then the GridView to the form before RenderContent().  This solution came per the aforementioned post’s comments.
  2. You may need to included the following page directive: EnableEventValidation=”false”.  I didn’t need to include this directive.
  3. You may need Override the VerifyRenderingInServerForm Method.  I didn’t need to do so because I added my GridView control to a “mocked” form.
     

Per Dipal Choksi, one can format the spreadsheet results in a generic manner by replacing all controls within the GridView with Literals.  This is reflected in the ClearControls() method above.  My prior implementation merely cleaned up the links associated with the sort functionality tied to the sortable headers.  This solution tackles all cells.

Additional Reference: GridViewGuy 

TSQL – Self Update

This morning I needed to compose a very simple SQL routine and it took me around five compiles until I got the syntax right.  All I needed to do was transfer an active status from one entity (in my case a computer) to another.  I decided to implement this by updating the same table in which I was selecting.  I think the syntax associated with “self update” type queries is tricky so this post is so I personally don’t lose this code snippet and future cycles.  I hope it might help you as well.

This example transfers the status of the source computer to the destination computer.  Nothing fancy…

DECLARE @SourceID INT; SET @SourceID = 2
DECLARE @DestinationID INT; SET @DestinationID = 1

DECLARE @Computer TABLE (ComputerID INT, StatusID INT)

INSERT INTO @Computer(ComputerID, StatusID)
SELECT @DestinationID, 1 UNION SELECT @SourceID, 2

SELECT * FROM @Computer

UPDATE A
SET A.StatusID = B.StatusID
FROM @Computer A, @Computer B
WHERE B.ComputerID = @SourceID
AND A.ComputerID = @DestinationID

SELECT * FROM @Computer

(2 row(s) affected)
ComputerID  StatusID
----------- -----------
1           1
2           2

(2 row(s) affected)

(1 row(s) affected)

ComputerID  StatusID
----------- -----------
1           2
2           2

(2 row(s) affected)

 

C# Encryption / Decryption Helper Class

I have collected a reasonably good size library of C# helper files over the years.  The EncryptionHelper below is one of many which I plan to share.

using System;
using System.Security.Cryptography;
using System.Text;

namespace Common
{
    public static class EncryptionHelper
    {
        private const string cryptoKey = "cryptoKey";

        // The Initialization Vector for the DES encryption routine
        private static readonly byte[] IV =
            new byte[8] { 240, 3, 45, 29, 0, 76, 173, 59 };

        /// <summary>
        /// Encrypts provided string parameter
        /// </summary>
        public static string Encrypt(string s)
        {
            if (s == null || s.Length == 0) return string.Empty;

            string result = string.Empty;

            try
            {
                byte[] buffer = Encoding.ASCII.GetBytes(s);

                TripleDESCryptoServiceProvider des =
                    new TripleDESCryptoServiceProvider();

                MD5CryptoServiceProvider MD5 =
                    new MD5CryptoServiceProvider();

                des.Key =
                    MD5.ComputeHash(ASCIIEncoding.ASCII.GetBytes(cryptoKey));

                des.IV = IV;
                result = Convert.ToBase64String(
                    des.CreateEncryptor().TransformFinalBlock(
                        buffer, 0, buffer.Length));
            }
            catch
            {
                throw;
            }

            return result;
        }

        /// <summary>
        /// Decrypts provided string parameter
        /// </summary>
        public static string Decrypt(string s)
        {
            if (s == null || s.Length == 0) return string.Empty;

            string result = string.Empty;

            try
            {
                byte[] buffer = Convert.FromBase64String(s);

                TripleDESCryptoServiceProvider des =
                    new TripleDESCryptoServiceProvider();

                MD5CryptoServiceProvider MD5 =
                    new MD5CryptoServiceProvider();

                des.Key =
                    MD5.ComputeHash(ASCIIEncoding.ASCII.GetBytes(cryptoKey));

                des.IV = IV;

                result = Encoding.ASCII.GetString(
                    des.CreateDecryptor().TransformFinalBlock(
                    buffer, 0, buffer.Length));
            }
            catch
            {
                throw;
            }

            return result;
        }
    }
}

You may have noticed that my recent posts have been short and provide little more than featured code.   This is a new approach for me.  What do you think?  Should I continue to pepper in these quick code snippet posts with little commentary?

 

Check Status of Windows Service

In the same vein as my last post on how to programmatically update a service startup type, here’s how one might determine if a service is currently stopped in C#:

using System.Management;
/// <summary>
/// This routine checks if the provided service is stopped.
/// </summary>
/// <param name="serviceName">Name of the service to be checked</param>
/// <param name="errorMsg">If applicable, error message assoicated with exception</param>
/// <returns>Stopped = true else false</returns>
public static bool IsServiceStopped(string serviceName, out string errorMsg)
{
    bool isStopped = false;
    errorMsg = string.Empty;

    string filter =
        String.Format("SELECT * FROM Win32_Service WHERE Name = '{0}'", serviceName);

    ManagementObjectSearcher query = new ManagementObjectSearcher(filter);

    // No match = stopped
    if (query == null) return false;

    try
    {
        ManagementObjectCollection services = query.Get();

        foreach (ManagementObject service in services)
        {
            string currentStatus = Convert.ToString(service["State"]);
            isStopped = (currentStatus.ToLower() == "stopped");
        }
    }
    catch (Exception ex)
    {
        errorMsg = ex.Message;
        throw;
    }

    return isStopped;
}

Update Service Startup Type

For the past six years I’ve been focused on web applications.  My current project, however, has me splitting time between the web and the desktop.  Today, I needed to programmatically toggle the Startup Type of my service between Manual and Automatic based on business conditions.   Here’s how I did it:

/// <summary>
/// This routine updates the start mode of the provided service.
/// </summary>
/// <param name="serviceName">Name of the service to be updated</param>
/// <param name="startMode">Manual or Automatic. This parameter could probably use
/// an enum.</param>
/// <param name="errorMsg">If applicable, error message assoicated with exception</param>
/// <returns>Success or failure.  False is returned if service is not found.</returns>
public bool ServiceStartModeUpdate(string serviceName, string startMode,
    out string errorMsg)
{
    uint success = 1;
    errorMsg = string.Empty;

    string filter =
        String.Format("SELECT * FROM Win32_Service WHERE Name = '{0}'", serviceName);

    ManagementObjectSearcher query = new ManagementObjectSearcher(filter);

    // No match = failed condition
    if (query == null) return false; 

    try
    {
        ManagementObjectCollection services = query.Get();

        foreach (ManagementObject service in services)
        {
            ManagementBaseObject inParams =
                service.GetMethodParameters("ChangeStartMode");
            inParams["startmode"] = startMode;

            ManagementBaseObject outParams =
                service.InvokeMethod("ChangeStartMode", inParams, null);
            success = Convert.ToUInt16(outParams.Properties["ReturnValue"].Value);
        }
    }
    catch (Exception ex)
    {
        errorMsg = ex.Message;
        throw;
    }

    return (success == 0);
}

As I’ve been doing a lot with services and windows applications lately, don’t be surprised in up coming posts start moving down this track.  Oh yes, I’ve done a lot with custom installers which I’m sure to write about as well.