none
UniqueIdentifier in SQL RRS feed

  • Question

  • I have a SP that takes two parameter say customername & userID.

    Here the userid is UniqueIdentifier. when I call the SQLComman.ExecuteScalar() it throws exception "Failed to convert string to GUID".

    Thursday, March 28, 2013 2:31 PM

Answers

  • Simply pass the GUID as string with GUID enclosed in curly braces.

    change the SP parameter type from GUID\Uniqeidentifier to NVARCHAR & do the conversion with the SP. This should work.

    • Marked as answer by Shah Bhavik Friday, March 29, 2013 11:22 AM
    Friday, March 29, 2013 11:22 AM

All replies

  • Simply pass the GUID as string with GUID enclosed in curly braces.

    change the SP parameter type from GUID\Uniqeidentifier to NVARCHAR & do the conversion with the SP. This should work.

    • Marked as answer by Shah Bhavik Friday, March 29, 2013 11:22 AM
    Friday, March 29, 2013 11:22 AM
  • Simply pass the GUID as string with GUID enclosed in curly braces.

    change the SP parameter type from GUID\Uniqeidentifier to NVARCHAR & do the conversion with the SP. This should work.

    Although you have answered your own question, a better way to pass a GUID parameter value as a System.Guid and a parameter type of SqlDbType.UniqueIdentifier (instead of a string):

    command.Parameters.Add("@MyGuid", SqlDbType.UniqueIdentifier).Value = new Guid("0BBAA81F-CCFF-4ED7-B6B1-9C7583B52C53");
     

    Note that the string overload of the System.Guid constructor will parse the specified string, with or without the curly braces.


    Dan Guzman, SQL Server MVP, http://www.dbdelta.com

    Friday, March 29, 2013 2:24 PM