Re: Convert Null to DBNull
- From: "scott ocamb" <socamb@xxxxxxxxx>
- Date: Sat, 17 Feb 2007 19:06:57 -0500
thanks,
you said it was simple, it is only simple once you know how,
i learned something today, thanks : )
"Göran Andersson" <guffa@xxxxxxxxx> wrote in message
news:%23Ej3U7tUHHA.5068@xxxxxxxxxxxxxxxxxxxxxxx
Right. The compiler can't find any common type for the operands, so you
have to specify it:
cmd.Parameters.Add("@FirstName", SqlDbType.VarChar, 30).Value = FirstName
?? (object)DBNull.Value;
By specifying one operand as object, it will see that the other operand
also can be cast to that type.
scott ocamb wrote:
Thanks for your help, but i get compile errors.
cmd.Parameters.Add("@FirstName", SqlDbType.VarChar, 30).Value = FirstName
?? DBNull.Value;
Error 1 ) expected C:\KI\Kinesis\Code2\CoachwareEntities\Athlete.cs 176
27 CoachwareEntities
seems to be some sort of casting error.
cmd.Parameters.Add("@FirstName", SqlDbType.VarChar, 30).Value =
(FirstName ?? DBNull.Value);
Error 3 Operator '??' cannot be applied to operands of type 'string' and
'System.DBNull' C:\KI\Kinesis\Code2\CoachwareDAL\Person.cs 94 87
CoachwareDAL
"Göran Andersson" <guffa@xxxxxxxxx> wrote in message
news:%23voj7mrUHHA.4764@xxxxxxxxxxxxxxxxxxxxxxx
Ignacio Machin ( .NET/ C# MVP ) wrote:
Hi,Or even easier:
"scott ocamb" <socamb@xxxxxxxxx> wrote in message
news:%23Z2%233KjUHHA.4188@xxxxxxxxxxxxxxxxxxxxxxx
Hello,
I have a function that expects the following parms. How can I handle
null values. For example, if FirstName comes through as null, how can
i propogate that null value to the stored procedure.
Easily:
cmd.Parameters.Add("@FirstName", SqlDbType.VarChar, 30).Value =
FirstName == null? DBNull.Value: FirstName;
cmd.Parameters.Add("@FirstName", SqlDbType.VarChar, 30).Value =
FirstName ?? DBNull.Value;
:)
--
Göran Andersson
_____
http://www.guffa.com
--
Göran Andersson
_____
http://www.guffa.com
.
- References:
- Convert Null to DBNull
- From: scott ocamb
- Re: Convert Null to DBNull
- From: Ignacio Machin \( .NET/ C# MVP \)
- Re: Convert Null to DBNull
- From: Göran Andersson
- Re: Convert Null to DBNull
- From: scott ocamb
- Re: Convert Null to DBNull
- From: Göran Andersson
- Convert Null to DBNull
- Prev by Date: getting fit with .net - version 0.2 is online
- Next by Date: Re: TimesTen with C# or .NET Framework
- Previous by thread: Re: Convert Null to DBNull
- Next by thread: where is the JSharp newsgroup??
- Index(es):
Relevant Pages
|