Click here to Skip to main content
14,695,609 members
Please Sign up or sign in to vote.
3.67/5 (2 votes)
See more:
how to check whether the enterd decimal is null or not?
Posted
Comments
Oshtri Deka 6-May-11 7:44am
   
Which control do you use for input?

What do you mean by "entered decimal"? If you're talking about a textbox, you could do this to check for null:

if (string.isNullOrEmpty(textbox.Text))
{
    // entry is null
}

If you're getting it from a database query:

if (IsDbNull(myDecimalValue))
{
    // entry is null
}
   
v2
Comments
arindamrudra 6-May-11 7:48am
   
My +5 because, both of the cases are given.
   
Even though the check is correct, you cannot enter null string in text box.
What is myDecimalValue type?
--SA
If you are using a nullable decimal (decimal?) you can check the HasValue property

decimal? d;
if (!d.HasValue)
{
//d is null 
}
   
Comments
   
Correct, my 5, but... a bit incomplete.
Please see my answer for additional notes.
--SA
Unfortunately (:-)), you cannot enter null decimal in any ways of entering I know. If you really want to enter the value of null for nullable type you can do it artificially by adding some check box "Is null". I don't think you need it. In this way, in all other cases you never need to check it. See the solution by Jeremy on how to work with nullable types. One mode note: you can also check "if (d == null)", assign "d = null;", and so on.

—SA
   
You can use the answer of John Simmons or you can use the tryparse() function.
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900