SQL: Find last week date range

The other day we needed to write a report on online sales for the last week. SQL doesn’t offer developers many predefined functions to work with date ranges unlike the C# programming language. Here is a little example how to query SQL for some data between the dates for…

enum to friendly string extension method

We use enums quite extensively in our application as they are great for representing integral values in a strongly typed way using symbolic names eg. if (Status == UserStatus.NotLoggedInAYear) ArchiveRecord(); is a lot more clearer and meaningful making it easier to maintain than if (Status == 3) ArchiveRecord() The enum names…

Object reference not set to an instance of an object

“Object reference not set to an instance of an object” is probably the most common run-time exception message spat-out by the .Net framework, and most programmers will probably encounter this more often than any other framework exception type. This exception, or more specifically a “System.NullReferenceException” is always caused when…

SCOPE_IDENTITY() return the id from the database on insert

As a .NET programmer most of my time is spent coding in C# and I try to avoid writing SQL where possible, which is great as we have dba's that can help with the more complicated queries. Why is it so complicated to write a loop without turning the database…

Select Text or Value From DropDownList Extension Method

Here is a cool way to select an item from the DropDownList using it's value. ddlCountries.Items.FindByValue(value).Selected = true; However I keep forgetting how to do it and end up spending ages searching for it, so here is a very usefull extension method that allows you to select…

Enum TryParse Extension Method

TryParse method is very helpful if you need to convert string representation of a value to a value itself. TryParse is better than Parse method because it doesn’t throw any exceptions and just returns a Boolean value to indicate weather the parsing was successful. Surprisingly there is no TryParse…