6th
February
2008
How often do you see DataReader code that looks like this?
using (IDataReader dr = cmd.ExecuteNonQuery())
{
while (dr.Read())
{
int orderId = dr.GetInt32(0);
string customerId = dr.GetString(1);
int employeeId = dr.GetInt32(2);
DateTime orderDate = dr.GetDateTime(3);
double freight = dr.GetDouble(4);
// do stuff
}
dr.Close();
}
If that looks like your code, you are not alone
. Try searching codesearch.google.com for the following:
lang:c# (reader|dr)\.GetInt32\(
With the above code, anytime the order of columns in the SQL statement or stored procedure changes, the code is broken. And if you have lots of columns to read from, it’s a real nightmare to maintain the indexes.
The next time you write another DataReader loop, consider doing it this way instead:
using (IDataReader dr = cmd.ExecuteNonQuery())
{
int ORDER_ID = dr.GetOrdinal("OrderID");
int CUSTOMER_ID = dr.GetOrdinal("CustomerID");
int EMPLOYEE_ID = dr.GetOrdinal("EmployeeID");
int ORDER_DATE = dr.GetOrdinal("OrderDate");
int FREIGHT = dr.GetDecimal("Freight");
while (dr.Read())
{
int orderId = dr.GetInt32(ORDER_ID);
string customerId = dr.GetString(CUSTOMER_ID);
int employeeId = dr.GetInt32(EMPLOYEE_ID);
DateTime orderDate = dr.GetDateTime(ORDER_DATE);
double freight = dr.GetDouble(FREIGHT);
// do stuff
}
dr.Close();
}
Using GetOrdinal makes the code much more readable and maintainable. You are calling GetOrdinal just once for each column, any performance penalty is insignificant compared to the benefits. Be careful not to put the GetOrdinal code inside the while block as that will unnecessarily slow you down (about 3% according to this article).

posted in Dotnet/.NET - C#, Programming, Tips |
7th
January
2008
I think this is useful and not well known enough to warrant its own blog post.
Did you know that there is a Visual Studio command called File.OpenContainingFolder? Asmita A Wankhede wrote about it, but he left a few important details out.
By default, this command does not have a shortcut, so you would have to assign one to it (try CTRL+SHIFT+ALT+O). Also, the "item" that this command works on is the currently opened item in the editor, not the selected item in the Solution Explorer. See my Visual Studio tips article for instructions on how to create new shortcuts (section 3 – Make New Shortcuts).
This works with both Visual Studio 2005 and 2008.

posted in Dotnet/.NET - C#, Programming, Tips |
6th
January
2008
It’s 2008. Happy New Year!
Tips and Tricks
- I can’t believe I didn’t know about this Visual Studio command before: File.OpenContainingFolder. Asmita A Wankhede mentioned it, but he left a few important details out. By default, this command does not have a shortcut, so you would have to assign one to it (try CTRL+SHIFT+ALT+O). Also, the "item" that this command works on is the currently opened item in the editor, not the selected item in the Solution Explorer. See my Visual Studio tips article for instructions on how to create new shortcuts (section 3 – Make New Shortcuts).
- Aaron Lerch shared a tip on how to use Powershell to perform search-and-replace on an entire folder hierarchy.
.NET, C#, Programming
Software And Tools
- Did you know that something called Robocopy (short for Robust File Copy, not Robot Copy), is the new XCOPY? It’s a standard tool in Windows Vista and is also available as part of the Windows Resource Kit. Via Don Box’s Spoutlet on Pluralsight.
- I recently tried and liked GhostDoc very much. It’s a free Visual Studio add-in to help write XML documentation comments. Roland Weigelt wrote a nice intro article on GhostDoc on DotnetSlackers here.
- SyncBackSE is a great folder synchronization utility. It has tons of features… maybe even a little bit on the bloated side. It costs $30 for a single license. If you just want something simple, Microsoft’s free SyncToy may do the trick for you.
Gadgets
- From CES, it looks like Blu-ray will be the winner of the HD format war. No, the war is not completely over, but this was the landing at Normandy… so to speak. The loss is just too great for the HD-DVD camp to recover.
And Now, Something Different
posted in Dotnet/.NET - C#, PowerShell, Programming, Software/tools, Tips |
18th
December
2007
Did you know Microsoft Outlook has supported type-ahead since Outlook 2003?
I didn’t. I upgrade my home PC to Outlook 2007 a few days ago and was pleasantly surprised to find type-ahead working. I fired up the old copy of Outlook 2003 and type-ahead worked in that version too. I am pretty sure it didn’t work in versions prior to 2003.
Type-ahead is great for using with the Move to Folder or Copy to Folder features. If you use Outlook to get your Gmail via IMAP, you’ll appreciate the type-ahead feature since you need to move messages a lot.
To see type-ahead working, in the Move Items dialog box (with a message selected, press CTRL+SHIFT+V), type the first few letters of any folder name. The current folder selection will move to the folder that matches what you type.

posted in Software/tools, Technology, Tips |