Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 Development Tools
 ASP.NET
 Come on, Developer can't handle null?

Author  Topic 

X002548
Not Just a Number

15586 Posts

Posted - 2007-10-22 : 16:28:57
How do you handle null?

Not like this I hope


private decimal replaceNullZero(decimal dec)
{
if (dec == null)
{
return 0;
}
else
{
return dec;
}
}




Brett

8-)

Hint: Want your questions answered fast? Follow the direction in this link
http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx

Add yourself!
http://www.frappr.com/sqlteam



spirit1
Cybernetic Yak Master

11752 Posts

Posted - 2007-10-22 : 16:34:20
usually this is handled in the get property.
of course there can be oter variations


public decimal MyProperty
{
get
{
if (dr["MyProperty"] == null)
return 0
return (decimal)dr["MyProperty"]
}
}


_______________________________________________
Causing trouble since 1980
blog: http://weblogs.sqlteam.com/mladenp
SSMS Add-in that does a few things: www.ssmstoolspack.com
Go to Top of Page

X002548
Not Just a Number

15586 Posts

Posted - 2007-10-22 : 16:47:47
Am I being smoked?


Actually used this code:

decimal dADLeaseHold = 0;
decimal dADComm = 0;
decimal dADFurn = 0;
if (mepExpenseSummary.ADLeaseHold.HasValue)
{
dADLeaseHold = (decimal)mepExpenseSummary.ADLeaseHold;
}
if (mepExpenseSummary.ADComm.HasValue)
{
dADComm = (decimal)mepExpenseSummary.ADComm;
}
if (mepExpenseSummary.ADFurn.HasValue)
{
dADFurn = (decimal)mepExpenseSummary.ADFurn;
}

decimal aggregate = (decimal)mepExpenseRep.continuedad +
(decimal)mepExpenseRep.sum_annualoccupancy +
(decimal)mepExpenseSummary.CapExpensedItems +
dADLeaseHold +
dADComm +
dADFurn;

We check for null in many-many places, but probably not in all. Some derived fields will always have values and it is handled in sproc, etc.



Brett

8-)

Hint: Want your questions answered fast? Follow the direction in this link
http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx

Add yourself!
http://www.frappr.com/sqlteam



Go to Top of Page

spirit1
Cybernetic Yak Master

11752 Posts

Posted - 2007-10-22 : 17:11:25
emm... huh?? you check for nulls in many many places???

null conversion is done only in one place: the DB Layer.
you set the business rule of what you want null to represent for each datatype and you convert it there.
datetime is the perfect example
if DateTime.MinValue is meant to be null then you use that in the DAL.
and vice versa when you get DateTime.MinValue back to the DAL you convert it to null.

of course maybe your busines requirements need these checks in many many places... but i doubt it.



_______________________________________________
Causing trouble since 1980
blog: http://weblogs.sqlteam.com/mladenp
SSMS Add-in that does a few things: www.ssmstoolspack.com
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2007-10-23 : 02:07:29
Is there not ISNULL function in ASP.NET?

In VB6,

IIF(ISNULL(dec)="true",0,dec)

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page

Kristen
Test

22859 Posts

Posted - 2007-10-23 : 03:45:27
"How do you handle null?"

I have functions (inspired by something some Microsoftie wrote - Code Complete, or one of those titles) called "Safe".

So StringToNumberSafe(SomeString) will never fail, and will return a "safe" value. This avoids embarrassing application errors, and an empty string can probably safely be converted to zero. NULL to in all probability. And a non-numeric-string. The Function can log "unexpected" values, so that developers can know that there are duff values "in the wild" (and in Dev Mode they can fail with suitable error message so that inaccurate code is cleaned up.

In all probability I would have optional parameters for return values for non-number, empty, NULL, whatever. Then the application can choose to have, say, -1 returned for unexpected values.

Kristen
Go to Top of Page

jezemine
Master Smack Fu Yak Hacker

2886 Posts

Posted - 2007-10-23 : 10:07:20
another common way to handle null in C# is to use Nullable<T>. it basically wraps value types in an object with some useful properties like HasValue and Value.


elsasoft.org
Go to Top of Page

X002548
Not Just a Number

15586 Posts

Posted - 2007-10-23 : 11:28:22
Thanks guys



Brett

8-)

Hint: Want your questions answered fast? Follow the direction in this link
http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx

Add yourself!
http://www.frappr.com/sqlteam



Go to Top of Page
   

- Advertisement -