Reputation: 5271
I have an object of type Employee which has a Guid property. I know if I want to set to null I must to define my type property as nullable Nullable<Guid>
prop or Guid? prop.
But in my case I'm not able to change the type of the prop, so it will remains as Guid type and my colleague and I we don't want to use the Guid.Empty.
Is there a way to set my property as null or string.empty in order to restablish the field in the database as null.
I have a mechanism to transform from string.empty to null but I will change many things if the would change to accept a empty guid to null.
Any help please!
Upvotes: 44
Views: 185730
Reputation: 1
If you are using a .Net SqlCommand that calls a stored procedure, you can set the parameter to NULL using code like that below.
SqlCommand cmdCust = new SqlCommand("MyProc", myConn);
cmdCust.Parameters.Add("@CustID", SqlDbType.UniqueIdentifier).Value = DBNull.Value;
Upvotes: 0
Reputation: 1500535
Is there a way to set my property as null or string.empty in order to restablish the field in the database as null.
No. Because it's non-nullable. If you want it to be nullable, you have to use Nullable<Guid>
- if you didn't, there'd be no point in having Nullable<T>
to start with. You've got a fundamental issue here - which you actually know, given your first paragraph. You've said, "I know if I want to achieve A, I must do B - but I want to achieve A without doing B." That's impossible by definition.
The closest you can get is to use one specific GUID to stand in for a null value - Guid.Empty
(also available as default(Guid)
where appropriate, e.g. for the default value of an optional parameter) being the obvious candidate, but one you've rejected for unspecified reasons.
Upvotes: 90
Reputation: 359
I think this is the correct way:
Guid filed = Guid.Empty;
Upvotes: 0
Reputation: 359
extrac Guid values from database functions:
#region GUID
public static Guid GGuid(SqlDataReader reader, string field)
{
try
{
return reader[field] == DBNull.Value ? Guid.Empty : (Guid)reader[field];
}
catch { return Guid.Empty; }
}
public static Guid GGuid(SqlDataReader reader, int ordinal = 0)
{
try
{
return reader[ordinal] == DBNull.Value ? Guid.Empty : (Guid)reader[ordinal];
}
catch { return Guid.Empty; }
}
public static Guid? NGuid(SqlDataReader reader, string field)
{
try
{
if (reader[field] == DBNull.Value) return (Guid?)null; else return (Guid)reader[field];
}
catch { return (Guid?)null; }
}
public static Guid? NGuid(SqlDataReader reader, int ordinal = 0)
{
try
{
if (reader[ordinal] == DBNull.Value) return (Guid?)null; else return (Guid)reader[ordinal];
}
catch { return (Guid?)null; }
}
#endregion
Upvotes: 0
Reputation: 59
you can make guid variable to accept null first using ? operator then you use Guid.Empty or typecast it to null using (Guid?)null;
eg:
Guid? id = Guid.Empty;
or
Guid? id = (Guid?)null;
Upvotes: 3
Reputation: 131
You can use typeof(Guid), "00000000-0000-0000-0000-000000000000"
for DefaultValue of the property.
Upvotes: 8
Reputation: 399
Guid? myGuidVar = (Guid?)null;
It could be. Unnecessary casting not required.
Guid? myGuidVar = null;
Upvotes: 39
Reputation: 152556
Choose your poison - if you can't change the type of the property to be nullable then you're going to have to use a "magic" value to represent NULL. Guid.Empty
seems as good as any unless you have some specific reason for not wanting to use it. A second choice would be Guid.Parse("ffffffff-ffff-ffff-ffff-ffffffffffff")
but that's a lot uglier IMHO.
Upvotes: 9
Reputation: 278
Since "Guid" is not nullable, use "Guid.Empty" as default value.
Upvotes: 24