Find Path of a Command Line Tool

Many times you work on different machines, execute a command line tool but often wonder where that tool is actually installed. One way to figure this out is to look at all environment PATH variables and search them manually in same order as Windows does. But you don’t have to because luckily there is a little known built-in command called WHERE that does that for you:

This is similar to Unix commands like WHICH and WHEREIS.

Selecting Random Row From SQL Server Table

It is important to make sure your automated tests covers various real-world data combinations (for instance, some columns could be null or some rows could be duplicate). For perf testing you want to reduce effects of caching by not firing same SQL over and over. In these cases, ability to select a random row for your test could come in handy and here’s neat little trick to do it:

select top 1 * from table order by newid()

What’s in a name?

When you want to store the name of a person a typical design starts out by creating two fields (in database or class):

Person

First Name
Last Name

Soon you realize lot of people have middle name, especially, when name change occurs after marriages. So you go and add one more field:

Person

First Name
Middle Name 
Last Name

This is all good… until you encounter people in countries such as Spain and Cuba who have custom to have two last names. Both are equally important and both are required in any official document (including ones your website or app may print out). So you go in and add one more field while thinking this ought to do it once and for all:

Person

First Name
Middle Name 
Last Name
2nd Last Name

Not so fast… Lot of people from Hong Kong and few other places in Asia actually carry two first names. One of these first names is traditional while other is typically a Western/Roman name. Both first names are important and often many people will know only Western/Roman first name of a person although official documents would only refer to traditional names.

For example, consider name of Hong Kong’s Chief Secretary Anson Chan Fang On Sang. Here Anson is English given name, On Sang is Chinese given name, Chan is husband’s surname and Fang is her own surname.

So time to add few more field so we can store everybody’s names on planet without loss of semantics:

Person

Traditional Given Name
English Given Name
Middle Name 
Last Name
2nd Last Name

Ok… so are we done now? Well, almost! We are still missing at least two critical pieces of information: Salutation and Suffix.

Example of common salutations are Dr, Mr, Mrs, Mr. While salutations are quickly falling out of fashion it might be still required, for example, if you are printing out an official letter to your customer and don’t want to make it look very casual.

Example of common suffixes are Jr, Sr, III, IV etc. These are required in official/legal communication to avoid confusion with other family members of a person.

Person

Salutation
Traditional Given Name
English Given Name
Middle Name 
Last Name
2nd Last Name
Suffix

Now we have covered most of the globe. There are still two more nice-to-have fields if you want to make your customers happy: Phonetic Given Name and Phonetic Last Name. Remember the times when you call customer support and each time you have a guy struggling to say your name? These two fields would avoid those moments:

Person

Salutation
Traditional Given Name
English Given Name
Middle Name 
Last Name
2nd Last Name
Phonetic Given Name
Phonetic Last Name
Suffix

So there you have it. A structure that can store almost anybody’s name on planet while maintaining semantics of each component of a name.

Most applications won’t need to go to this extreme because it’s OK to just have one first name and one last name that correctly identifies a person for its purpose even if it’s culturally incorrectly and technically incomplete. However if you are in a business where legal implications are high or if any information loss about your customer is not tolerable then it’s good to think about these possibilities.

There are probably better solutions than giant structure like above just to store name of a person. Instead of having all these different fields you can simply have one free form field, say, Full Name and another field called Full Name Style which takes values indicating how different components of names are arranged:

Person

Full Name
Full Name Style

This structure will make searches for specific components of a name little difficult but it would extend well as your application grows around the planet.

Twitter Dishing Out 417 - Expectation Failed to .Net Clients

My little Twitter app was broke since past few days with error 417 - Expectation Failed. Infect most .Net apps calling Twitter APIs would be broken right now so I thought to write this up.

This error is seemingly because Twitter servers have started rejecting Expect HTTP header with value "!00-Continue". I'm not sure if this was planned event or enough warnings were issued to developers but it would be guaranteed to drive you nuts.

The error is because of default behavior in HttpWebRequest object that adds an HTTP header called Expect with value "100-Continue" to almost every outgoing POST request. This header basically tells the server that it's going to send all the data in form in the next request instead of current request so that if server has redirects or auth then it doesn't have to resend it all over again. This is a good thing if your web form has lots of data or if you are on low latency network or most servers in the word have either redirects or auth when submitting forms but a bad thing for server performance because now it gets hit twice for each request. I think performance might be the reason Twitter has turned off support for such two partter POST requests which unfortunately happens to be the default for HttpWebRequest.

In any case, it turns out that HttpWebRequest does all these thing under the hood so to get rid of this error you will need to set a static flag in ServicePointManager class like this:

System.Net.ServicePointManager.Expect100Continue = false;

Above statement will cause elimination of HTTP Expect header from your calls to Twitter and it will be happy again.

I'm using Yedda's C# wrapper for Twitter APIs for QckTwit so above line goes in to start of ExecutePostCommand method.

PS: If you are new to Twitter try out free simple lightweight app QckTwit. It just sits in your system tray, asks you about what you are doing at reminder interval you set, updates the Twitter and gets out of your way!

Why would you still get "Strong name validation failed"?

There are not many web pages mentioning this so I would just post this so it comes up in search. Having personally spent 4 hours tracking this little thing down, I would want anyone else to go through same :).

So... if you are using delay signing, you will need to run the following command so you can still debug from Visual Studio.Net:

sn -Vr *,[public key token]

Apparently if you are using Vista 64-bit it just won't work! You will still keep getting error something like,

Could not load file or assembly '[Your file], Version=2.0.0.0, Culture=neutral, PublicKeyToken=[public ket token]' or one of its dependencies. Strong name validation failed. (Exception from HRESULT: 0x8013141A)

You can try viewing Fusion log, cleaning solution, rebooting machine, watch FileMon, run Process Explorer, rebuild everything 10 times... but it just won't work. Infect if you try removing signing and if your app is WPF 3.5 then you might even get even more weird errors like

Could not create an instance of type 'StaticExtension'

The solution is hidden in a one liner in Dan Wahlin's blog:

If you're running a 64-bit installation of Vista you'll need to use the sn.exe located at C:\Program Files\Microsoft SDKs\Windows\v6.0A\Bin\x64\sn.exe

I'm pretty sure tons of developers adopting shiny 64-bit OS are/would run in to this. The root cause here is sn.exe designed for 32-bit doesn't error out instead it happily lets you know that "Verification entry added for assembly '*,*'" successfully! It's not! So I also filed a bug in out Connect web site. Please vote to make 64-bit world a better place!

Exception Code Template For CodeRush

How CodeRush could miss this template? Anyway you can copy and paste this in CodeRush Options (Expert mode) to have your own!

[System.Serializable]
public class InvalidDataException
{
    public InvalidDataException()
    {
    } 

    public InvalidDataException(string message) : base( message )
    {
    } 

    public InvalidDataException (string message, System.Exception inner): base( message, inner )
    {
    } 

    public InvalidDataException (System.Runtime.Serialization.SerializationInfo info, System.Runtime.Serialization.StreamingContext context) : base(info, context)
    {
    }
}

My New CodeProject Article On Equation Rendering

I just finished my new article on CodeProject. The mission on MimeTeX was started about couple of months ago when in a weekend I just got attracted to MimeTeX's C code like a magnet ;). Now I've built ASP.Net handler, caching, admin etc on the top of it and its looking great! Enabling scientific content on web seems to be my new obsession. So if you take pride in delighting your users with every new release, here's your brand new feature! Go ahead, download it, use it! If you run in to any problem, I'll be glad to offer you my help.

Interesting Headers

The response headers returns with Google's logo looks like this:

Content-Type: image/gif
Last-Modified: Mon, 25 Apr 2005 21:06:18 GMT
Expires: Sun, 17 Jan 2038 19:14:07 GMT
Server: GWS/2.1
Content-Length: 8558
Date: Sat, 16 Jul 2005 01:52:05 GMT

And for some gif served by IIS on Win 2003 Server looks like this:

Server: Microsoft-IIS/5.0
X-Powered-By: ASP.NET
Date: Sat, 16 Jul 2005 02:08:59 GMT
Content-Type: image/gif
Accept-Ranges: bytes
Last-Modified: Fri, 04 Mar 2005 09:12:29 GMT
Etag: "20bb944a9a20c51:acc"
Content-Length: 3779

You can get this by using Web Developer Toolbar's Information > Reponse Header button in Firefox.

Interesting things are:

  1. Google is not using Cache-Control: max-age=xxxx header which tells browser to cach the image for a while and not re-request from the server again. This is surprising because you would think Google would use every possible way out there to reduce the load on their server.
  2. Google has named their custom web server as Google Web Server, not surprisingly.