PC Review


Reply
Thread Tools Rate Thread

Convert Null to DBNull

 
 
scott ocamb
Guest
Posts: n/a
 
      17th Feb 2007
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.

public long InsertPerson(string FirstName, string LastName, string
MiddleName, string EMail, long PersonTypeId,
string HomePhone, string CellPhone, bool EmailOptOut, string City,
long StateId, string PostalCode, string Street, long CountryId, long
InsertUserId)
{
using (SqlConnection cn = new SqlConnection(this.ConnectionString))
{
SqlCommand cmd = new SqlCommand("ks_Person_InsertPerson", cn);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add("@FirstName", SqlDbType.VarChar, 30).Value =
FirstName;
cmd.Parameters.Add("@LastName", SqlDbType.VarChar, 30).Value =
LastName;


 
Reply With Quote
 
 
 
 
Otis Mukinfus
Guest
Posts: n/a
 
      17th Feb 2007
On Fri, 16 Feb 2007 20:25:08 -0500, "scott ocamb" <(E-Mail Removed)> wrote:

>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.
>
>public long InsertPerson(string FirstName, string LastName, string
>MiddleName, string EMail, long PersonTypeId,
> string HomePhone, string CellPhone, bool EmailOptOut, string City,
> long StateId, string PostalCode, string Street, long CountryId, long
>InsertUserId)
>{
> using (SqlConnection cn = new SqlConnection(this.ConnectionString))
> {
> SqlCommand cmd = new SqlCommand("ks_Person_InsertPerson", cn);
> cmd.CommandType = CommandType.StoredProcedure;
> cmd.Parameters.Add("@FirstName", SqlDbType.VarChar, 30).Value =
>FirstName;
> cmd.Parameters.Add("@LastName", SqlDbType.VarChar, 30).Value =
>LastName;
>


if(string.IsNullOrEmpty(FirstName))
{
cmd.Parameters.AddWithValue("@FirstName", DBNull.Value);
}
else
{
cmd.Parameters.AddWithValue("@FirstName", FirstName);
}

There may be typos here, but you get the idea...

Good luck with your project,

Otis Mukinfus

http://www.otismukinfus.com
http://www.arltex.com
http://www.tomchilders.com
http://www.n5ge.com
 
Reply With Quote
 
Ignacio Machin \( .NET/ C# MVP \)
Guest
Posts: n/a
 
      17th Feb 2007
Hi,

"scott ocamb" <(E-Mail Removed)> wrote in message
news:%23Z2%(E-Mail Removed)...
> 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;




 
Reply With Quote
 
=?ISO-8859-1?Q?G=F6ran_Andersson?=
Guest
Posts: n/a
 
      17th Feb 2007
Ignacio Machin ( .NET/ C# MVP ) wrote:
> Hi,
>
> "scott ocamb" <(E-Mail Removed)> wrote in message
> news:%23Z2%(E-Mail Removed)...
>> 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
 
Reply With Quote
 
scott ocamb
Guest
Posts: n/a
 
      17th Feb 2007
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" <(E-Mail Removed)> wrote in message
news:%(E-Mail Removed)...
> Ignacio Machin ( .NET/ C# MVP ) wrote:
>> Hi,
>>
>> "scott ocamb" <(E-Mail Removed)> wrote in message
>> news:%23Z2%(E-Mail Removed)...
>>> 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



 
Reply With Quote
 
=?ISO-8859-1?Q?G=F6ran_Andersson?=
Guest
Posts: n/a
 
      17th Feb 2007
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" <(E-Mail Removed)> wrote in message
> news:%(E-Mail Removed)...
>> Ignacio Machin ( .NET/ C# MVP ) wrote:
>>> Hi,
>>>
>>> "scott ocamb" <(E-Mail Removed)> wrote in message
>>> news:%23Z2%(E-Mail Removed)...
>>>> 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
 
Reply With Quote
 
scott ocamb
Guest
Posts: n/a
 
      18th Feb 2007
thanks,

you said it was simple, it is only simple once you know how,

i learned something today, thanks : )



"Göran Andersson" <(E-Mail Removed)> wrote in message
news:%(E-Mail Removed)...
> 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" <(E-Mail Removed)> wrote in message
>> news:%(E-Mail Removed)...
>>> Ignacio Machin ( .NET/ C# MVP ) wrote:
>>>> Hi,
>>>>
>>>> "scott ocamb" <(E-Mail Removed)> wrote in message
>>>> news:%23Z2%(E-Mail Removed)...
>>>>> 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



 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
DBNull vs null in .Net 2.0 =?Utf-8?B?Sm9lIEwuIEYu?= Microsoft ADO .NET 1 17th Apr 2006 03:42 PM
null or DbNull or what? Glenn Thimmes Microsoft ADO .NET 1 25th May 2005 01:43 AM
Null & DBNULL Problems MadCrazyNewbie Microsoft VB .NET 4 13th Aug 2004 12:03 PM
convert DBNull to SqlInt32.Null Vanja Andreev Microsoft ADO .NET 5 29th Jul 2004 08:20 AM
Null and DBNull.value Nathan Kovac Microsoft C# .NET 3 20th Mar 2004 05:47 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 08:08 AM.