Recent Posts

PresentationFontCache issue – IndexOutOfRangeException when running WPF application

March 20, 2011

A few days back, I came across this IndexOutOfRangeException with PresentationFontCache when running our WPF application on a Windows 7 machine. I tried disabling and enabling .NET framework from Add/Remove components but nothing seemed to fix this.

PresentationFontCache service optimizes performance of WPF applications by caching commonly used font data.

One of the following two fixes seem to work for different peoples.

  1. PresentationFontCache data is stored at [Windows]\ServiceProfiles\LocalService\AppData\Local\FontCache*.dat. Some of the guys have fixed this problem after deleting this file from here and restarting Windows Presentation Font Cache service. Disabling Windows Presentation Font Cache service worked for me. I don’t know why but just stopping the service did not work for me.

Refer to following thread for some more discussions -

http://social.msdn.microsoft.com/Forums/en/wpf/thread/2e47b80a-1423-466f-873a-8536a64ebe3c

Large file transfer in WCF in IIS 7 - There was no endpoint listening at xxx that could accept the message

March 10, 2011

I had hosted a WCF web service on IIS 7 that needed to transfer large files (~100 MB) to and from the service. I had all of **maxReceivedMessageSize, maxBufferSize, maxItemsInObjectGraph, maxRequestLength **configured to sufficiently large values, but still i was getting above mentioned exception when transferring greater than 30 MB files.

In IIS 7, there is one more setting called maxAllowedContentLength which is by default 30000000 byte (just over 28 MB). You need to tweak this value according to your need. Following snippet shows where it should be placed in the web.config file.


  
    
      
        
      
    
  

  This setting is not directly available from IIS manager, so if you need to change this using GUI tool you can download IIS 7.0 Admin Pack. You will find this setting from selecting your website and then going to Request Filtering –> Edit Feature Settings –> Maximum allowed content length (Bytes).

ThreadAbortException:Thread was being aborted in IIS 7

March 06, 2011

Recently i was developing a WCF web service in IIS 7 on Windows Server 2008 and started getting this exception quite weirdly. Weirdly in the sense that the exception was not consistently being reported at the same call stack.

My web service was using App_Data to store some application specific data which was also being added/modified/deleted during runtime.

After doing a lot of searching and study, the behavior of writing to the App_Data turned out to be the culprit. It is really hard to guess what may be wrong. In fact, I learnt that heavily modifying application subdirectory shuts down the appdomain.

Following are two references that explain this thing -

http://blogs.msdn.com/b/toddca/archive/2005/12/01/499144.aspx

http://forums.iis.net/t/1121400.aspx

It seems that it has nothing to do with IIS 7 though and can happen even with IIS 6.

What Firebird assemblies to ship with embedded Firebird .NET App?

January 30, 2011

For a .NET application that uses embedded Firebird, I would suggest to ship all of the following with you .NET app.

  1. All the *.dll in the embedded Firebird directory. Downloaded Firebird embedded zip file from Firebirdsql.org if you have not already downloaded. To be precise, following are the assemblies with respect to v2.5 of Firebird.
  2. **intl **and **udf **directory. Both of these directory should be there in Firebird embedded zip file you downloaded.

Note that all of these files/folders mentioned above should be at the exe location of your .NET application. Some of these assemblies might not be used at all in the application, but i found issues when i only shipped fbembedd.dll. Shipping all of the stuff mentioned above has been quite stable.

Adding custom display value to Enum fields

January 15, 2011

Most of the times, display value for our custom enums are different from actual field value. We usually want to have different display string than actual field of the enum.

Thanks to extension method, we can extend Enum to add functionality to it. You can now add methods to an enum which was previously not possible. Here i am going to show two approaches of extending Enums to support display value for Enum fields.

Following is the 1st approach.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace SampleWinApplication
{
    public enum LoginStatus
    {
        LoggedIn,
        LoggedOut,
        Idle,
        Away
    }

    public static class LoginStatusExtensions
    {
        public static string GetDisplayName(this LoginStatus loginStatus)
        {
            switch (loginStatus)
            {
                case LoginStatus.Away:
                    return "Away";
                case LoginStatus.Idle:
                    return "Idle";
                case LoginStatus.LoggedIn:
                    return "Logged in";
                case LoginStatus.LoggedOut:
                    return "Logged out";
            }
            return loginStatus.ToString();
        }
    }

    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();

            // this will "Logged out"
            MessageBox.Show(LoginStatus.LoggedOut.GetDisplayName());
        }
    }

    
}

Here we add an extension method GetDisplayName to enum LoginStatus. The method returns display string depending on the given value of LoginStatus. Note that here you have full control over what display value to have for the given LoginStatus. This also has the advantage that you have consistent display value for LoginStatus throughout you application. There is one problem with this approach that you have to add an extension method for every enum that you want to add custom display string to. Following approach is a more generic solution to this problem using Attributes and extension methods.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace SampleWinApplication
{
    [Serializable]
    [AttributeUsage(AttributeTargets.Field, Inherited = false)]
    public class EnumDisplayInfoAttribute : Attribute
    {
        public string DisplayString { get; private set; }

        public EnumDisplayInfoAttribute(string displayString)
        {
            this.DisplayString = displayString;
        }
    }

    public enum LoginStatus
    {
        [EnumDisplayInfo("Logged in")]
        LoggedIn,

        [EnumDisplayInfo("Logged out")]
        LoggedOut,

        [EnumDisplayInfo("Idle")]
        Idle,

        [EnumDisplayInfo("Away")]
        Away
    }

    public static class EnumExtensions
    {
        public static string GetDisplayName(this Enum targetEnumValue)
        {
            object displayInfoAttribute = targetEnumValue.GetType().GetField(targetEnumValue.ToString()).GetCustomAttributes(typeof(EnumDisplayInfoAttribute), false).FirstOrDefault();
            if (displayInfoAttribute != null)
            {
                return (displayInfoAttribute as EnumDisplayInfoAttribute).DisplayString;
            }
            return targetEnumValue.ToString();
        }
    }

    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();

            // this will "Logged out"
            MessageBox.Show(LoginStatus.LoggedOut.GetDisplayName());
        }
    }

    
}

Here we add extension method to the base Enum class and thus it is available to every Enum. We also define an Attribute EnumDisplayInfoAttribute that you need to apply to enum fields that you want to support display string for. Note that for any new enums that you add in your application, you just need to decorate its fields with EnumDisplayInfoAttribute and they will start supporting display string feature. Also, if there is no attribute applied to given enum field, we just return the ToString(). You can use either of the two approaches. I personally find 2nd approach more elegant and readable because the display string is defined just where the enum is.

Get all the columns of a given table in Firebird database

January 07, 2011

Following sql script gives all the columns of a given table (‘Employee’ in this example) in a Firebird database.

SELECT Fields.RDB$FIELD_NAME "Column Name" FROM RDB$RELATION_FIELDS Fields
WHERE Fields.RDB$RELATION_NAME = 'EMPLOYEE' and Fields.RDB$SYSTEM_FLAG = 0
ORDER BY Fields.RDB$FIELD_POSITION

Drop all stored procedures in Firebird database

December 27, 2010

In my last post, I gave the SQL script to delete all stored procedures from a SQL Server database. Following is the link to post -

http://www.sanjaysingh.net/2010/12/drop-all-stored-procedures-from-sql.html

Now, following SQL script can be used to delete all the stored procedures from a Firebird database.

SET TERM ^ ;
EXECUTE BLOCK
AS
DECLARE procName varchar(100);
begin

FOR SELECT rdb$Procedure_name FROM rdb$procedures WHERE rdb$system_flag IS NULL OR rdb$system_flag = 0 INTO :procName
    DO
    begin
        EXECUTE STATEMENT 'DROP PROCEDURE ' || procName;
    end
end^

SET TERM ; ^

Drop all stored procedures from SQL Server database

December 23, 2010

Following SQL script drops all user stored procedures from a SQL Server database.

USE TestDatabase

DECLARE @CurrStoredProcedureName SYSNAME
DECLARE StoredProceduresCursor CURSOR FOR
    SELECT name FROM sys.objects WHERE type = 'P'

OPEN StoredProceduresCursor
FETCH NEXT FROM StoredProceduresCursor INTO @CurrStoredProcedureName
WHILE @@FETCH_STATUS = 0
BEGIN
    EXECUTE('DROP PROCEDURE ' + @CurrStoredProcedureName)
    FETCH NEXT FROM StoredProceduresCursor INTO @CurrStoredProcedureName
END

CLOSE StoredProceduresCursor
DEALLOCATE StoredProceduresCursor

GO

Replace database name with your database name in the above script. Mainly we are doing following two things:

  1. Get all the user stored procedures in a CURSOR.
  2. Loop through the cursor, build a DROP PROCEDURE query for each stored procedure and execute it.

Check if given table or stored procedure exists in Firebird

December 20, 2010

Following SQL checks to see if table named Employee exists in the database and if it does, drops it.

SET TERM ^ ;
EXECUTE BLOCK AS BEGIN
if (exists(select 1 from rdb$relations where rdb$relation_name = 'EMPLOYEE')) then 
execute statement 'drop table employee;';
END^
SET TERM ; ^

Note that conditional operator IF cannot be used outside of PSQL so we have to place this call inside BLOCK as shown above. Also we cannot put DDL statements inside PSQL, so above thing cannot even be placed inside stored procedure.In the same way, following SQL checks to see if stored procedure Sel_Employee exists and drops it if it does.

SET TERM ^ ;
EXECUTE BLOCK AS BEGIN
if (exists(select 1 from RDB$PROCEDURES where rdb$Procedure_name = 'SEL_EMPLOYEE')) then 
execute statement 'drop procedure SEL_EMPLOYEE;';
END^
SET TERM ; ^

Password Manager

December 19, 2010

Often times we store our passwords in one or other kind of files on our personal computers. When you have too many passwords it becomes increasingly difficult to manage them. At the same time if you write them to a txt file, you also have the risk of it being read by someone else.

I made a very simple Password Manager C# .NET windows app that stores the password in encrypted format on your local file system. Its really simple. Following is summary of what it does.

  • When you run the application first time, it will ask you to configure your master password.
  • All the passwords are stored in encrypted format (using Rijndael symmetric algorithm) using your master password as the key in a file Password.dat at the exe location. Note that this file can only decrypted with your master password and so is secure even if someone gets his hand on your password data file.
  • It has a a Form that shows your password in a grid and gives you a search text box which show real time search results as you type.

Source and executable can be downloaded from following locations:

http://sourceforge.net/projects/managepassword/files/