Wednesday, 15 July 2009

SQL - Dynamic Order By clause

declare @orderBy nvarchar(max)
set @orderBy = 'columnName'

-- 0 Descending
-- 1 Acsending
declare @orderByDirection int
set @orderByDirection = 0

select *
from tableName
order by
case when @orderBy = 'columnName' and @orderByDirection = 0
then columnName end desc,
case when @orderBy = 'columnName' and @orderByDirection = 1
then columnName end

Monday, 13 July 2009

ASP.NET MVC - UserControls and IPrincipal

This is my first post on ASP.NET MVC so I'm gonna keep it short and sweet (I'm sure they'll be plenty more to come :-D).

On a MVC View Page, you can access the user that has been previously authenticated with ease:

<%= User.Identity.Name %>

Unfortunately, you can't do the same in an MVC View User Control because they don't have a property called User. Instead, you have to use the ViewContext property which is available on any MVC View:

<%= ViewContext.HttpContext.User.Identity.Name %>

Friday, 20 February 2009

To serialize, or not to serialize - that is the question

When serializing objects, you sometimes want to exclude properties that don't meet a certain criteria. This validation could be a simple check for default/null values or a more complicated scenario.

This example shows how to prevent a DateTime property being serialized if it hasn't been set, meaning it's value will be DateTime.MinValue. You'll notice there is the property for date of birth and a method called ShouldSerializeDateOfBirth that has no parameters and returns a boolean value. If this returns true, serialize, and vice versa. How does it get called I hear you ask. I don't actually know exactly but the key to invoking is in the name - make sure it starts with "ShouldSerialize" followed by the name of the property (in this case, "DateOfBirth").

public bool ShouldSerializeDateOfBirth() { return DateOfBirth != DateTime.MinValue; }

[XmlElement("dateofbirth")]
public DateTime DateOfBirth { get; set; }

Monday, 9 February 2009

How do I create the ASPState database?

After installing the Microsoft .NET framework 2.0, launch a command window and run:

aspnet_regsql.exe -ssadd -sstype p -S {Server} -U {Username} -P {Password}

Friday, 30 January 2009

CSS "or" separator

<div style="background-color: white; margin-left: -10px; margin-top: 40px; padding: 5px; position: absolute;">or</div>
<div style="border-left: solid 1px #999; height: 8em; margin:auto; width: 1px;"></div>

Example:

or




* Copied from Stack Overflow

Monday, 26 January 2009

0x80048820

I recently kept getting an error after trying to sign into Windows Live Messenger or sync Windows Live Mail. My laptop had been unplugged at the mains for over a week and the date/time settings were scrambled. I noticed the time was wrong but I failed to spot the year was 2002. I simply changed it to 2009 and hey presto, problem solved.

Friday, 16 January 2009

How do I clean up my log files?

Create a scheduled task that will execute the following command:

C:\WINDOWS\system32\forfiles.exe /p C:\logs /s /m *.log /d -7 /c "CMD /C del @FILE"

This example recursively deletes "*.log" files older than 7 days. If you simply wanted to move the files, change the comannd parameter - this provides a nice way to archive.

For further information, see the Forfiles documentation.