Failed to convert parameter value from string to guid

user1404780 picture user1404780 · Oct 10, 2012 · Viewed 11.6k times · Source

I have a tiered application. The datalayer makes a call to the database by using a dataset which contains a tableadapter. In the database table there is a field called ID of type UniqueIdentifier. I then create the following query:

select * from tbl where ID=@ID

When i try to preview the results in the dataset designer i receive the error "Failed to convert parameter value from string to guid".

I tried adding single quotes around the value passed in but didnt work.

Where am i going wrong?

Answer

Aghilas Yakoub picture Aghilas Yakoub · Oct 10, 2012

Your column ID is Guid type in your database, but your parameter is a string.

You can convert your parameter to a SqlGuid:

command.Parameters.Add("@GuidParameter", SqlDbType.UniqueIdentifier).Value = new System.Data.SqlTypes.SqlGuid(YourGuid); //The value of @ID

Link : https://msdn.microsoft.com/library/system.data.sqltypes.sqlguid.aspx?f=255&MSPPError=-2147217396