Jes Sie

Jes Sie

  • 702
  • 1.2k
  • 264.2k

How to set sum to 0 if no data is found from the Database

Sep 12 2017 4:22 AM
Hello DBA's once again I needed help. 
 
I have this query which will give me the sum of all sold insurance.
  1. select e.AgentID, sum(isnull(e.TotalPremium,0)) from tblMotorInsurance_eCI e where e.AgentID=@AgentID group by e.AgentID;  
 I give me the answer I needed if the agent has a sale. but if the agent doesn't have any record yet, it gives me this error:
 
  1. Server Error in '/' Application.  
  2.   
  3. There is no row at position 0.  
  4.   
  5. Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.   
  6.   
  7. Exception Details: System.IndexOutOfRangeException: There is no row at position 0.  
  8.   
  9. Source Error:   
  10.   
  11.   
  12. Line 102:                    catch (Exception ex)  
  13. Line 103:                    {  
  14. Line 104:                        throw ex;  
  15. Line 105:                    }  
  16. Line 106:                }  
 I wanted to show me a sale = 0 if the record doesn't exist. Thanks for any help.

Answers (3)