Reputation: 4520
I am trying to pass a uniqueidentifier parameter to a stored procedure using the following code:
myCommand.Parameters.Add("@BlogID", SqlDbType.UniqueIdentifier).Value = "96d5b379-7e1d-4dac-a6ba-1e50db561b04";
I keep getting an error however saying that the program was unable to convert from string to GUID. Am I passing the value incorrectly?
Upvotes: 8
Views: 33941
Reputation: 1
One thing to check is that you are not comparing the uniqueidentifier to any string in the database.
When I ran into this, I found a section where I had the following line:
IF @MyGuid IS NULL OR @MyGuid = '' BEGIN...
The { @MyGuid = '' } part will cause the error you described.
Upvotes: 0
Reputation: 29664
A unique identifier is a GUID. so it's a different object type to your string.
You need
myCommand.Parameters.Add("@BlogID", SqlDbType.UniqueIdentifier).Value =
new Guid("96d5b379-7e1d-4dac-a6ba-1e50db561b04");
Upvotes: 8
Reputation: 190907
Try this
myCommand.Parameters.Add("@BlogID", SqlDbType.UniqueIdentifier).Value = new Guid("96d5b379-7e1d-4dac-a6ba-1e50db561b04");
Upvotes: 34