Click here to Skip to main content
15,887,683 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:

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

If you're getting it from a database query:

C#
if (IsDbNull(myDecimalValue))
{
    // entry is null
}
 
Share this answer
 
v2
Comments
arindamrudra 6-May-11 7:48am    
My +5 because, both of the cases are given.
Sergey Alexandrovich Kryukov 6-May-11 11:09am    
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

C#
decimal? d;
if (!d.HasValue)
{
//d is null 
}
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 6-May-11 11:12am    
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
 
Share this answer
 
You can use the answer of John Simmons or you can use the tryparse() function.
 
Share this answer
 

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



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