Re: Convert Null to DBNull

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance



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,

"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;

Or even easier:

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


.



Relevant Pages

  • Re: OSA-Express2 and SNA Pcomm - loss of connectivity
    ... And now for the comments on your VTAM definitions. ... I suggest that you adopt the standard of selecting the switched PU statement on the CP name, the CPNAME operand, rather than the IDBLK and IDNUM operands. ... The IRETRY operand of the switched PU statement is something of an anomaly - along with the PASSLIM operand which I am happy you and/or your predecessors did not think appropriate to specify. ... The OSAs are connected to a Cisco 6500 switch which in turn connects to a ...
    (bit.listserv.ibm-main)
  • Re: SMP/E problem?
    ... the GROUP operand, SMP/E does not try to restore the ... You have to specify both PTFs on the SELECT ... Kurt Quackenbush -- IBM, SMP/E Development ...
    (bit.listserv.ibm-main)
  • Re: mde, mdw, mdb - How are they known to one another?
    ... John, I'm not quite sure what you mean by ... the same directory as the frontend database. ... If you don't specify the /wrkgrp optional operand, ...
    (microsoft.public.access.modulesdaovba)
  • Re: clobbering of input operands in GCC
    ... "There is no way for you to specify that an input operand is modified ... But A must NOT be an output operand. ... Variables declared to live in specific registers, ... or output operands must have no part mentioned in the clobber description. ...
    (comp.lang.asm.x86)
  • Re: Was Create Stored Procedure wizard droped from SQL Server 2005
    ... In SQL server 2005 management studio, you can use the "Specify Values for ... connect to an instance of SQL Server 2005 Database ... Right-click Stored Procedures, and then click New Stored Procedure. ... click Specify Values for Template Parameters. ...
    (microsoft.public.sqlserver.tools)