ExecuteScalar and null

Can anyone explain why this doesn’t work:

command.CommandText = "SELECT [Token] FROM Clients WHERE
    [Token] ='" + client1.Token + "'";
Guid result = new Guid(command.ExecuteScalar() as string);

While this does work:

command.CommandText = "SELECT [Token] FROM Clients WHERE
    [Token] ='" + client1.Token + "'";
Guid result = new Guid(command.ExecuteScalar().ToString());

The first snippet returns null. The second returns the requested Guid. The only difference is as string and .ToString(). This seems to only happen on uniqueidentifier columns in .NET 2.0.

(And don’t wonder why the code doesn’t seem to have a purpose, it is a bit from a larger a unit test.)

Tags: , , , , ,

Viewing 7 Comments

 

Trackbacks

(Trackback URL)

close Reblog this comment
blog comments powered by Disqus