Solved

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

Posted on 2013-11-08
5
547 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

MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

Question has a verified solution.

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

More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
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.
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…

733 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