문제

I searched the entire web. I wonder why this easy task is so hard to accomplish in C#.

sqlcmd.Parameters.Add("@someid", System.Data.SqlDbType.UniqueIdentifier).Value =
string.IsNullOrEmpty(class.someid) ? System.Data.SqlTypes.SqlGuid.Null : new Guid(class.someid);

This returns an error :

Failed to convert parameter value from string to guid

My stored procedure updates a record in the table with the parameters relayed by C#.

What I want to do is to update a record and its specific column with null value by using a stored procedure which passes a guid parameter from C# with guid that can sometimes be null in value. Is this possible in C#?

도움이 되었습니까?

해결책

Could you try:

sqlcmd.Parameters.Add("@someid", System.Data.SqlDbType.UniqueIdentifier).Value =
string.IsNullOrEmpty(class.someid) ? System.Guid.Empty : new Guid(class.someid);

If the casting doesn't work. And in your table make sure if this column allows null. And class.someid has the correct format for the constructor. You can check on this link to have an overview.

다른 팁

If I am not wrong, your column ID is of Guid type in your database, but your parameter is string.

You can convert your parameter to SqlGuid

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

Link : http://msdn.microsoft.com/fr-fr/library/system.data.sqltypes.sqlguid.aspx

Also try the following in the stored procedure:

Declare @someid uniqueidentifier = null

Declaring nullable property:

public Guid? column1;

Assigning Null:

column1 = null;
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top