Tuesday, April 27, 2010

Case Sensitive or Insensitive SQL Query

Hello World!

So today was one of those days - you know when you see a ton of mails saying everything that was working yesterday is not working today! But somehow you believe that you'd be able to make it all work again - almost magically!

Well the problem today was as follows: I work with a .NET application that has the following components:

1) A windows NT service
2) A set of UI tools (desktop apps) to administer the behaviour of the service

Now what happened is that one of our QA was trying to set up an environment from scratch using the App - installer (an MSI) and the database scripts provided. The database had to be created first followed by running all scripts in order to create all objects and insert default data. The rest of the configuration had to be done by using the UI. The application uses strongly typed DataSet to access the DB layer. After setup, the app started throwing an unhandled exception while trying to start. The exception stack trace referred to a "column not found" whilst the table had the column when I checked the database. I just happened to check the query being executed by the application normally by using the SQL server management studio - and lo - it indeed complained of the missing column! I then realized that if I spelled the column name in the same case as being shown in Object Explorer, the query did succeed.

On further investigation, when I checked the server properties ( Right Click the server node in object explorer -> Properties; Click "General" under Select a Page pane; Look for the property named "Server Collation"), the collation was listed as "Latin1_General_CS_AI". When I googled for this, I got to know that the "CS" stands for case sensitive. To change it to case-insensitive, one must use "CI"

You can change the collation at the database level as follows:

1. Right click on the database name in object explorer and click properties
2. Under "select a page" (right pane) click on "Options"
3. Change the collation drop down to the desired collation
4. click Ok.

You can also accomplish the same by running the script from the master database:

ALTER DATABASE COLLATE

NOTE: The database needs to be exclusively locked to perform this operation. This, in plain English, means that there should not be any connections to the database while doing this.

The following link demonstrates how to do this at a query level:

Steven Smith : Case Sensitive or Insensitive SQL Query

If you use a collation with each query, you needn't worry about DB collation!

All the best!

Thursday, April 15, 2010

How to find out if a user exists on a domain or system?

So here I was, trying to grope through the System.DirectoryServices namesapce trying to find a way to check if a given username exists in the domain. What I wanted to do is this: If you have ever opened your computer management console, you would have noticed that when you attempt to add a user to a group, the dialogue would check if the username exists. I was trying to achieve the same functionality programmatically.

My application was required to do the following:

1. Present the users with an input box to type in a username to add and a drop down to select the group in which to add them
2. Check if the username is valid
2.1 If the username is valid, check if it already exists as a member of the group selected
2.1.1 If the username is valid AND it does not exist in the group selected, add to the group

I was able to (with help from support.microsoft.com KB article) figure out how to check if the user is member of a group but was having trouble trying to figure out how to check if the user actually exists in the domain(Active Directory) or system (WinNT)

I landed on the following article from Stackoverflow.com:

Faster way to find out if a user exists on a system? - Stack Overflow

I'm quoting the code snippet for quick reference:

using System.DirectoryServices.AccountManagement; //Add System.DirectoryServices.AccountManagement in Project Reference

public bool IsUserFound(String UserName)
{
bool UserExists;

using (PrincipalContext pc = new PrincipalContext(ContextType.Domain))
{
UserPrincipal up = UserPrincipal.FindByIdentity(pc, IdentityType.SamAccountName, UserName);
UserExists = (up != null);
}
return UserExists;
}
NOTE: ContextType enum contains three values: Machine (to check on local machine), Domain (To verify user on a domain) and ApplicationDirectory that represents AD LDS store.
If you are running this code from a machine which is not in the domain (or in a domain which does not have a trust relationship with user's domain) you would have to launch this thread as a security principal (that is a user) that has access to the target domain. The following MSDN link should get you started in doing that.

Wednesday, April 7, 2010

Uninstalling a .NET service

Hello World,

Well, this should be pretty simple: InstallUtil.exe is provided with the .NET framework for manual install/uninstall as described here: MSDN Link

Basically what I like to do when running a command like this is to add it to the "Path" variable under my environment variables.
For example, if you are working with .NET framework 3.5, on a Windows server 2003 (32-bit Standard edition), InstallUtil.exe can be found at the following path:
C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727

So, to be able to launch the utility without having to type the entire path, add the path (in italics above, based on your system configuration) to your "Path" environment variable.
For instructions on how to edit your environment variables, refer THIS link. Once done, to install a .NET service, open up a command prompt (Start -> Run -> cmd) and
browse to the path of your service executable. e.g. C:\Projects\MyWinService\MyWinService.exe and issue the command InstallUtil MyWinService.exe

What I managed to do (don't ask me why!) is delete the registry key HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\MyWinService. After that all my attempts to uninstall the service were unsuccessful. Needless to mention, the service could not be installed again as Windows complained that "The service is already installed"
Then, I thought, let me try this: from a backup, I copied over the service executable to the folder where it should have been. Then, I issued the command
InstallUtil /u MyWinService.exe
[/u uninstalls the service]

and, as expected, the utility successfully un-installed the service!

[By the way, for .NET services, the old style -service command does not install them to the service controller. Neither does /uninstall remove them.]

Best Of luck!

Tuesday, April 6, 2010

VS 2010 and .NET 4 Series - ScottGu's Blog

Nice, informative series by Scott Guthrie:

Uninstalling Visual Studio 2008 (all versions)

Uninstalling Visual Studio 2008 (All Versions)

I was struggling to repair an installation of VS 2008 Team System (Development edition) and found this on the blog by Victor Chen

The following link should take you the download page for the uninstall tool:

Best of Luck!

adaptive path » ajax: a new approach to web applications

The guy who coined the term Ajax (As in Asynchronous JavaScript and XML for the un-initiated) : now a buzzword or shall I say indispensable for any web developer worth his salt (or sugar if you will!)

C# Using Reflection to list object properties programatically

Hello World!

Today a fellow colleague asked me if it is possible to programmatically access the fields of a custom type and check if they are null or empty and take some action based on the test.
I knew this had to be solved by using reflection by determining the runtime "Type" and then enlist members. On research, found this useful link:


There are several (working!!) examples here that demonstrate the technique.

All the best!

Visual Studio Setup - projects and custom actions

Hello World!

The following article from Simple Talk gives detailed procedures of adding custom actions to a Visual Studio setup project (.vdproj)


All The Best!