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