This is just a random Tip.
It seems like recently some people I know started to have problems passing a Guid objects as a parameter of a SqlCommand object. I already had this problem so I decided to post about it in case someone else is trying the same thing. Apparently it doesn't work if you pass the Guid directly as shown below:
Guid theGuid = new Guid();
SqlCommand command = new SqlCommand("select * from someTable where SomeField = @GuidParameter", someConnection);
command.Parameters.Add("@GuidParameter", SqlDbType.UniqueIdentifier).Value = theGuid;
When the Guid object is passed directly, it's not parsed and Sql Server cannot handle it. The correct way to pass a Guid object as a SqlParameter would be slightly different:
Guid theGuid = new Guid();
SqlCommand command = new SqlCommand("select * from someTable where SomeField = @GuidParameter", connection);
command.Parameters.Add("@GuidParameter", SqlDbType.UniqueIdentifier).Value = System.Data.SqlTypes.SqlGuid.Parse(theGuid.ToString());
Note that now, when passing the value, I'm converting the Guid to a string value and using SqlTypes.SqlGuid.Parse to perform the actual parse. The method parse returns a SqlGuid object, which is the right match for SqlDbType.UniqueIdentifier.
The alternative to Parse, would be creating a new instance of SqlGuid and pass this instance to the Value property of the SqlParameter, but then you would create one more object to do the exact same thing. In this case it would be something like this:
Guid theGuid = new Guid();
System.Data.SqlTypes.SqlGuid theSqlGuid = new System.Data.SqlTypes.SqlGuid(theGuid);
SqlCommand command = new SqlCommand("select * from someTable where SomeField = @GuidParameter", connection);
command.Parameters.Add("@GuidParameter", SqlDbType.UniqueIdentifier).Value = theSqlGuid;
Well, that's it. I hope it helps someone out there. Cheers.