System.Guid and SQL's uniqueidentifier
This was interesting - I had a uniqueidentifier as an output parameter from a sproc and wanted to put it into a Guid.
Guid guid = (Guid)sqlCmd.Parameters["@UserID"].Value;
No. That throws System.InvalidCastException : Invalid cast from 'System.String' to 'System.Guid'.
Ah, so it's a string, ok.
Guid guid = new Guid((string)sqlCmd.Parameters["@UserID"].Value);
Er, no. That throws System.InvalidCastException : Unable to cast object of type 'System.Guid' to type 'System.String'
Excuse me?? Is it a string or a Guid?
Well, apparently Guid's aren't your normal kind of bear. It's a class, it's a structure, it's a reference type depending on where you look. I couldn't be bothered to find the "real answer", but here's how I did the deed - kinda obvious NOW of course.
Guid guid = new Guid(sqlCmd.Parameters["@UserID"].Value.ToString());
The parameter value is a Guid type already you see, it's just that you cannot assign a Guid to a Guid. IMO that was a dumb first error message to show me ("can't cast string to Guid"), but all's well that ends well.