Mental Jetsam

By Peter Finch

  •  

    November 2009
    M T W T F S S
    « Oct    
     1
    2345678
    9101112131415
    16171819202122
    23242526272829
    30  
  • My del.icio.us links

  • Flickr Photos

    S5001187

    S5001169

    S5001136

    More Photos
  • Wordpress Stuff

Internet Explorer does not open application/rtf mimetype

Posted by pcfinch on February 5, 2009

Unknown File Type Dialog

Unknown File Type Dialog

I had an odd problem lately where Internet Explorer (IE 7 & 8 ) stopped opening RTF documents served up by our website. The RTF files were served up by a CGI, so they did not have a “rtf” extension on the URL. IE would present a dialog saying that it did not know what the file type was “Unknown File Type”. All the other browsers (Firefox, Safari, etc) worked fine.

On further investigation I found the server was responding with a mime-type of “application/rtf” and it seems IE no longer knows what this is and expects a mime-type of “application/msword”. I found 2 solutions.

  1. The best solution, if you control the server, is to change the mime-type of the documents to “application/msword” ().
  2. If you can not change the server, then try adding the following registry entry to define the “application/rtf” mime-type

    [HKEY_CLASSES_ROOT\MIME\Database\Content Type\application/rtf]
    "Extension"=".rtf"

Posted in HTML | Leave a Comment »

Sorting an array in C#

Posted by pcfinch on January 14, 2009

The following code is a simple and quick way to sort and arbitrary array of objects in C#. I like using a delegate in this way as you can pick any attribute or member of the object to sort on at the time you need to perform the sort in the code.

class Customer {
	internal String sName ;
	internal String sAddress ;
} ;

Customer[] customers = new Customer[20] ;
/* Load array ...*/

Array.Sort<Customer>(customers, new Comparison<Customer>(delegate(Customer l, Customer r)
{
	return (l.sName.CompareTo(r.sName));
})) ;

Make sure the array is full, i.e. no null entries, otherwise you will have to check for null values in l and r.

Posted in C#.NET | Leave a Comment »

Dynamically creating C# class instances using reflection.

Posted by pcfinch on November 27, 2008

Reflection, the ability for a program to dynamically inspect itself, is possibly one of the most powerful features of modern programming languages like C# and Java. The following is a simple example of how to interrogate the current C# assembly, locate all the classes that are derived from a base class, and then dynamically created and executed an instance of the class.

I wrote this code for a set of tests I wanted to implement. The idea was to make it simple to add new tests to the project by just adding new class derived from the base “Test” class. The program would then automatically find all the tests and run them.

  1. First, create the “Test” base class.
    abstract class Test {
      protected String m_sName ;
      public String name { get { return (m_sName) ; } }
      protected Test(String sName) { m_sName = sName; }
      abstract public void run() ;
    }

  2. Derive the actual tests from the “Test” base class.
    class UsersTest : Test {
      public UsersTest() : base("Test user names and passwords") { }
      override public void run() {
        // Test code does here
      }
    }

  3. To run the tests, first get the current Assembly, and find all the Types that are “Classes” and are derived from the class “Test”. Once you have the type you need to Dynamically create and instance of it. This method looks for the default constructor (the one with no parameters) and uses that, however, the code could look for any constructor if required. It then calls the contractor to
    create an instance of the class, and finally executes the classes run() method.
    Assembly asm = Assembly.GetExecutingAssembly();
    foreach (Type type in asm.GetTypes()) {
      if (type.IsSubclassOf(typeof(Test)) && type.IsClass) {
        ConstructorInfo ci = type.GetConstructor(new Type[] { });
        Test t = (Test)ci.Invoke(new Object[] { }) ;
        Console.Out.WriteLine ("Running Test - " + t.name);
        t.run();
      }
    }

This is a very simple example, and there are other ways to runs tests, but it is easy to see how this design could be extended and used in all sorts of situations.

Posted in C#.NET, Programming | 3 Comments »

Loading an SqlDataReader into a DataGridView

Posted by pcfinch on November 20, 2008

This is a simple example of how to load the results from an arbitrary SQL Query, contained in a SqlDataReader, into a DataGridView on a Windows Form (C# Client application). This is an example of a customised view and not an automatically generated one using the AutoGenerateColumns feature and it also demonstrates calling a SQL Server Stored Procedure from C#.

  1. Create the DataGridView object e.g. dgvTrials
  2. Add the Column names to the DataGridView using Visual Studio. For each of the columns set the DataPropertyName to the column name returned in the SQL query result set.
    Add Column to DataGridView
  3. Create the SQL Server stored procedure.
    IF OBJECT_ID('sys.sp_getTrialsByUser') IS NOT NULL
     DROP PROCEDURE [sys].sp_getTrialsByUser
    GO
    CREATE PROCEDURE [sys].sp_getTrialsByUser (@username varchar(50)) AS
    select distinct TRIAL_ID, START_DATE, END_DATE, BOOKACRONYM
        from [sys].TRIALS t
    	where t.USERNAME = @username ;
  4. Code up the SQL Query using a SqlConnection, SqlCommand and SqlDataReader and load the SqlDataReader results into a DataTable. Then bind the DataTable to the DataGridView.
    using (SqlConnection connection = new SqlConnection(sConnectString))
    {
      connection.Open();
      using (SqlCommand command = connection.CreateCommand())
      {
        command.CommandText = "[sys].sp_getTrialsByUser";
        command.CommandType = CommandType.StoredProcedure;
        command.Parameters.Add("@USERNAME", SqlDbType.VarChar, 50);
        command.Prepare();
        command.Parameters["@USERNAME"].Value = tbUserName.Text;
        SqlDataReader reader = command.ExecuteReader();
        using (reader)
        {
          DataTable table = new DataTable();
          table.Load(reader);
          dgvTrials.DataSource = table;
        }
      }
    }

Posted in C#.NET, Programming | Leave a Comment »