Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

How do I check for NULL DB Records in this C# Code?

Posted on 2013-11-08
5
Medium Priority
?
564 Views
Last Modified: 2013-11-15
The following code is a static function used to calculate the NEXT ID created by the database.  The code needs to perform a basic initial check to ensure that if there are no records in the table it can take the appropriate action.  eg Make the First row 1.  eg Try/Catch or IF/Else.

public static int SiteIDMax()
{                                                    
int SiteID;
SiteID = Convert.ToInt32(ew_ExecuteScalar("SELECT max(SiteID) FROM DMPDB.DMP_Sites"))+ 1;  
//HttpContext.Current.Response.Write(Convert.ToInt32(SiteID));
return SiteID;
}

Open in new window

0
Comment
Question by:XGIS
[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
5 Comments
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 39635137
Try:

object result = ew_ExecuteScalar("SELECT max(SiteID) FROM DMPDB.DMP_Sites");

if (result != DBNull.Value)
{
    SiteID = Convert.ToInt32(result);
}

Open in new window

0
 
LVL 7

Accepted Solution

by:
XGIS earned 0 total points
ID: 39635193
Solution:  For use in ASP.NET Maker 10.x

public static int SiteIDMax()
{                                                    
int SiteID = 0;
 try
 {    
       int count=Convert.ToInt32(ew_ExecuteScalar("SELECT max(SiteID) FROM DMPDB.DMP_Sites"));

       if(count==0)
       {
     
       }    
 }
 catch(Exception ex)
 {      
 return 1;
 }   
 
 SiteID = Convert.ToInt32(ew_ExecuteScalar("SELECT max(SiteID) FROM DMPDB.DMP_Sites"))+1;  
 return SiteID;
}

Open in new window

0
 
LVL 10

Expert Comment

by:joriszwaenepoel
ID: 39641026
For a SQL Server database, you can modify the query so it never returns NULL, like this:

SELECT ISNULL(max(SiteID),1) FROM DMPDB.DMP_Sites

Other database servers have similar functions.  I think for Oracle the function name is NVL.
0
 
LVL 7

Author Closing Comment

by:XGIS
ID: 39644100
Hello Kaufmed.. thankyou for your time and logic... unfortunately it was a little bit more tricky working inside another application's implementation of C#.  The solution resolves the null issue and correctly assigns folders based on subsequent dependent code. I am sure this solution could be tidier..but "works" is more important than "tidy" for me.
0
 
LVL 7

Author Comment

by:XGIS
ID: 39652195
Hello  joriszwaenepoel... good call.. I have used this approach before and it is good based on the fact it eliminates messy codes.. I will give it a test, although my problem has been resolved. Cheers
0

Featured Post

 [eBook] Windows Nano Server

Download this FREE eBook and learn all you need to get started with Windows Nano Server, including deployment options, remote management
and troubleshooting tips and tricks

Question has a verified solution.

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

Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…
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.…

618 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