[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

How to test for DBNull when using ExecuteScalar?

Posted on 2008-11-13
4
Medium Priority
?
1,564 Views
Last Modified: 2013-12-17
I have the code below.  It returns DBNull and crashes.  How do I test for DBNull using ExecuteScalar?
string selectStatement = "select sum(cast(NbrOfCopies as integer)) from tBooks";
            FbCommand cmd = new FbCommand(selectStatement, bookConn);
            if (bookConn.State == ConnectionState.Closed)
                bookConn.Open();
            int Count = Convert.ToInt32(cmd.ExecuteScalar());  //  <-- crashes here!
            lblTotalCount.Text = "Total In Inventory:    " + Count.ToString();

Open in new window

0
Comment
Question by:rmmarsh
  • 2
4 Comments
 
LVL 6

Expert Comment

by:matthewrhoades
ID: 22951741
I would add another line for the conversion.

Sorry for the sloppy looking formatting.
ing selectStatement = "select sum(cast(NbrOfCopies as integer)) from tBooks";
            FbCommand cmd = new FbCommand(selectStatement, bookConn);
            if (bookConn.State == ConnectionState.Closed)
                bookConn.Open();
            int Count = cmd.ExecuteScalar();  //  <-- crashes here!
if isDBNull(Count) = False then
myCount = ToInt32.Convert(Count)
            lblTotalCount.Text = "Total In Inventory:    " + myCount.ToString();

Open in new window

0
 
LVL 6

Accepted Solution

by:
matthewrhoades earned 2000 total points
ID: 22951756
Rather...
ing selectStatement = "select sum(cast(NbrOfCopies as integer)) from tBooks";
            FbCommand cmd = new FbCommand(selectStatement, bookConn);
            if (bookConn.State == ConnectionState.Closed)
                bookConn.Open();
            int Count = cmd.ExecuteScalar();  //  <-- crashes here!
            If IsDBNull(Count) = False Then
                myCount = ToInt32.Convert(Count)
            End If
            lblTotalCount.Text = "Total In Inventory:    " + myCount.ToString();
            myInternalConnection.Close()

Open in new window

0
 

Author Comment

by:rmmarsh
ID: 22952283
I found a better way:  

            int Count = cmd.ExecuteScalar() is DBNull ? 0 : Convert.ToInt32(cmd.ExecuteScalar());



But thank you for putting me on the right track...
0
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 22952648
mmarsh: your "better" method risks to run the command object 2 times!!!!

hence:


object result = cmd.ExecuteScalar();
int Count = (result == DBNull.Value ? 0 : Convert.ToInt32(result));

Open in new window

0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
In real business world data are crucial and sometimes data are shared among different information systems. Hence, an agreeable file transfer protocol need to be established.
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…
Is your OST file inaccessible, Need to transfer OST file from one computer to another? Want to convert OST file to PST? If the answer to any of the above question is yes, then look no further. With the help of Stellar OST to PST Converter, you can e…
Suggested Courses
Course of the Month17 days, 21 hours left to enroll

830 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