Solved

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

Posted on 2013-11-08
5
529 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
  • 3
5 Comments
 
LVL 74

Expert Comment

by:käµfm³d 👽
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
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.
This video discusses moving either the default database or any database to a new volume.
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

762 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now