Wrox Programmer Forums
|
ASP.NET 2.0 Basics If you are new to ASP or ASP.NET programming with version 2.0, this is the forum to begin asking questions. Please also see the Visual Web Developer 2005 forum.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ASP.NET 2.0 Basics section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old August 14th, 2007, 01:33 AM
Registered User
 
Join Date: Aug 2007
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
Default null coalescing operator

I am stumped with this code. Having just discovered the nul coalescing operator i am trying to convert all my code to use the operator

I converted this

if (dRow["bookid"] != DBNull.Value)
{ BookID = (int?)dRow["bookid"]; }
        else
{ BookID = null; }

to this

BookID = (int?)dRow["bookid"] ?? null;

and now it gives a "Specified cast is not valid" error

How can i correctly convert the first code segment to use the null coalescing operator



www.xhydra.com
 
Old August 14th, 2007, 02:15 AM
Registered User
 
Join Date: Aug 2007
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Quote:
quote:Originally posted by nyasha_c
 I am stumped with this code. Having just discovered the nul coalescing operator i am trying to convert all my code to use the operator

I converted this

if (dRow["bookid"] != DBNull.Value)
{ BookID = (int?)dRow["bookid"]; }
        else
{ BookID = null; }

to this

BookID = (int?)dRow["bookid"] ?? null;

and now it gives a "Specified cast is not valid" error

How can i correctly convert the first code segment to use the null coalescing operator



www.xhydra.com

Sorry Guys. I figured it out.
There is no way you can use the null coalescing operator when testing for DBNull.

The thing is DBNull and null are two different things and the ?? operator only tests for null.

It could have been a good thing if the ?? operator worked with dbnull values because these values are ussually checked prior to assigning to some variable


www.xhydra.com





Similar Threads
Thread Thread Starter Forum Replies Last Post
how to use like operator thillaiarasu SQL Server 2000 4 April 23rd, 2010 09:47 AM
=== operator in c# surendraparashar C# 2005 8 November 8th, 2007 05:14 AM
Invalid operator for data type. Operator equals di Pusstiu SQL Server 2000 2 August 10th, 2007 04:51 AM
How to set Not Null constraint to Null Columns arasu Oracle 1 August 22nd, 2005 10:09 AM
? operator in PHP Tachyon Beginning PHP 2 May 24th, 2004 02:59 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.