TheGateKeeper
TheGateKeeper

Reputation: 4520

Passing uniqueidentifier parameter to Stored Procedure

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

Answers (3)

Brad McCord
Brad McCord

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

Liam
Liam

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

Daniel A. White
Daniel A. White

Reputation: 190907

Try this

myCommand.Parameters.Add("@BlogID", SqlDbType.UniqueIdentifier).Value = new Guid("96d5b379-7e1d-4dac-a6ba-1e50db561b04");

Upvotes: 34

Related Questions