?
Solved

How to tell if this field is null

Posted on 2010-01-11
6
Medium Priority
?
188 Views
Last Modified: 2012-05-08
In the following statement, there is sometimes nothing in that field, which causes an exception.

How do I tell in if the field is valid so as to not cause the exception?

while (dr.Read()) {
    decimal workingPrice = dr.GetDecimal(5);
}

Open in new window

0
Comment
Question by:rmmarsh
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
6 Comments
 
LVL 30

Expert Comment

by:anarki_jimbel
ID: 26289035
Call IsDBNull to check for null values before calling this method.

See:

http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqldatareader.getdecimal.aspx
0
 
LVL 30

Accepted Solution

by:
anarki_jimbel earned 1500 total points
ID: 26289110
Just in case, if not clear:

if(!dr.isDBNull){
  decimal workingPrice = dr.GetDecimal(5);
}


I can also make a suggestion to use nullable types. But it depends on what you need. Have a look:

http://www.c-sharpcorner.com/UploadFile/mosessaur/nullabletypes08222006164135PM/nullabletypes.aspx

But in any case before using GetDecimal you need to check for DBNull.
0
 

Author Comment

by:rmmarsh
ID: 26289325
Your syntax is wrong for IsDBNull... it only works on 'int', not decimal.  Thanks anyway!
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 

Author Comment

by:rmmarsh
ID: 26289394
Correct syntax is:                      if (!dr.IsDBNull(5))

Thanks anyway...
0
 

Author Closing Comment

by:rmmarsh
ID: 31675835
Answer was 'close', but not correct.  He gave me something to work with, tho'
0
 
LVL 30

Expert Comment

by:anarki_jimbel
ID: 26289482
Of course, experts also do mistakes. Especially if are not using VS at the moment :)

By the way, excellent practice to give RIGHT solutions as you did. For those who will search for solutions in future. Thanks.
0

Featured Post

On Demand Webinar: Networking for the Cloud Era

Ready to improve network connectivity? Watch this webinar to learn how SD-WANs and a one-click instant connect tool can boost provisions, deployment, and management of your cloud connection.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Extention Methods in C# 3.0 by Ivo Stoykov C# 3.0 offers extension methods. They allow extending existing classes without changing the class's source code or relying on inheritance. These are static methods invoked as instance method. This…
This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question