How to Create a Quick Table Backup using T-SQL

–OPTIONALLY CHECK FOR AND DROP THE BACKUP TABLE IF ALREADY EXISTS:
IF EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[MY_TABLE_BACKUP_20130710]’) AND type in (N’U’))
DROP TABLE [MY_TABLE_BACKUP_20130710]
GO

–ONE LINE TABLE BACKUP:
SELECT * INTO [MY_TABLE_BACKUP_20130710] FROM [MY_ORG_TABLE]
GO
–NOTE THIS COPIES JUST DATA, NOT CONSTRAINTS OR RELATIONS

Umbraco SQl to Find Nodes using a Certain Document Type

SELECT * FROM cmsContent C
INNER JOIN cmsContentType CT ON C.contentType = CT.nodeId
INNER JOIN umbracoNode N ON C.nodeId = N.id
WHERE CT.alias = ‘yourDocumentTypeAliasHere’

–use the node path to locate nodes in Umbraco

.NET (C#) – format decimal as price – with no culture specific information and two decimal points

This is one of those where there’s so many ways of doing something simple – you end up going “will someone please just tell me how the…” etc etc :0)
The below is a simple basic way, which ignores all culture specific information (you add the currency symbol yourself), and gives you two decimal points.

		decimal decimalValue = 12.99M;
		string test = String.Format("Item costs £{0:N} only", decimalValue);
		//"Item costs £12.99 only"
		//This will also round correctly - e.g. 12.995 to "13.00", and 12.994 to "12.99".

And if you need to inject the currency symbol based on some logic:

		string currencySymbol = "£";
		string test = String.Format("Item costs {0}{1:N} only", currencySymbol, decimalValue);
		//"Item costs £12.99 only"

And then of course finally we stick this into an extension method:

	public static class ExtDecimal
	{
		public static string ToMoney(this Decimal d, string currencySymbol)
		{
			return String.Format("{0}{1:N}", currencySymbol, d);
		}
	}

	...

	string test = decimalValue.ToMoney("£");
	//"£12.99" - and above example:
	test = String.Format("Item costs {0} only", decimalValue.ToMoney("£"));
	//"Item costs £12.99 only"

ASP.NET Session State IsNew – or is it?

A new session ID is generated for each request in applications that do not store data in the session dictionary.

Yes it’s amazing, but true. Logically you’d expect a new ASP.NET session object to be created when the user arrives at your site, and then expire 20 mins (or whatever you configure) after they leave, but in fact a new instance is created for each request, until (if ever) your code sticks some data into session state.

The reason for this is performance – at the end of the request life cycle any data placed in Session state is serialised and persisted using your chosen provider. If there’s no data, then ASP.NET skips the taks of maintaining the session ID and loading this again next time.

If you’re using the default InProc mode then the performance gain might be negligable, but if you’re for instance using SQL server then the job of adding an empty record to the session state tables, and reading this again (for no purpose) is very sensibly skipped.

What this does mean however is that the Session IsNew property will return true until you use Session State – so don’t use this flag to check if a user has just arrived at your site. If you need to check this you could place your own cookie to expire with the session (i.e. don’t set the cookie’s Expire property).

Key thing is – Session State might be new, but that does not mean your user’s session on the webserver is.

PowerShell – Passing Arguments to a Function – Don’t Use Commas!

Recently picked up PowerShell for some automated deployment scripts – when passing more than one argument to a function I seemed to end up with one strange object, and any subsequent arguments were simply missing.
The problem was that within PowerShell you must separate arguments to a function by spaces rather than commas – if you use commas you are actually creating a list (of objects) and passing that as the first argument.

So say you have a function myFunction defined somewhere – the below call will dynamically create a list (the parentheses are simply ignored) and pass that to myFunction:

    myFunction($arg1, $arg2, $arg3);
    #myFunction will receive ONE argument from this call, a list with three elements
    #The second and third parameter will be null.

The correct way to pass three separate arguments is with spaces:

    myFunction $arg1 $arg2 $arg3;
    # my function will receive THREE separate arguments from this call. 

I mainly code in C# – and I think the main reason this caught me out, is that PowerShell scripting language is otherwise very similar to C#, especially when you code in a nice IDE like Power GUI.
At the time I calling a function inside a foreach loop with a XmlElement, and it’s just odd that you then have to “switch back” to this kind of classic command line passing of params.. but there you go – it’s a shell, albeit a powerful one!

This is also covered along with some other great PowerShell gotchas in the excellent article here http://www.johndcook.com/powershell_gotchas.html.

Umbraco Upgrade hangs at 35% “Upgrading Database Tables”

This is an issue that seems to apply to the install wizard for Umbraco 4.6 and 4.7, when upgrading from older versions.
At the database step you get to 35% with the status “upgrading database tables” – and nothing else happens.

This is caused by a bug in the install code – the format of the code makes it hard to spot – but below reformatted section from \umbraco\presentation\install\utills\p.aspx.cs shows the problem – the first block is incorrect and should be removed:

Howver, at the point where you see the “35% Updatings database tables…” the code has already checked that’s it’s able to connect to and upgrade your database. The Install() call (above) is a quick set of SQL statements – only takes a couple of seconds to run – and the RefreshContent() is async (will run separately in another thread), so if you have been looking at the “35 upgrading tables” for a little while it’s pretty safe to assume that this has completed without errors, and hitting F5 will then force a recheck of your database which should send you on to the next step.

I’ve also covered this issue in the Umbraco forums here: http://our.umbraco.org/forum/getting-started/installing-umbraco/17531-Upgrade-to-461-Updating-database-tables-Problem

A new Session ID for each request (until you use session)

Caught me out twice now this – watch it with the Session.IsNewSession property – a new session ID is generated for each request in applications that do not store data in the session dictionary.
http://msdn.microsoft.com/en-us/library/aa479041.aspx