Construct a comma delimited list from table column with SQL

How many times have you had to create a comma delimited list from a table column? Do you ever remember how to do it? I don’t. Here is a little snippet I find useful: -- this will be a comma delimited list of location ids DECLARE @LocationIds Varchar(Max…

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…