?
Solved

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

Posted on 2013-11-08
5
Medium Priority
?
561 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

Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
Suggested Courses

765 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