Jes Sie

Jes Sie

  • 704
  • 1.2k
  • 265.1k

Initializing null value

May 12 2017 2:28 AM
Once again I need your help. I have a webpage in my web application. On page load, it looks up the credit limit and the accounts receivable of an agent. Below is my code and works fine if the accounts receivable has value:
  1. private void GetCreditLimit()  
  2.         {  
  3.             using (SqlConnection con = DBConnection.GetDbCon())  
  4.             {  
  5.                 SqlCommand cmd = new SqlCommand("spGetAgentCreditLimitAndARBalance", con);  
  6.                 cmd.CommandType = CommandType.StoredProcedure;  
  7.                 cmd.Parameters.AddWithValue("@AgentID", lblAgentCode.Text);  
  8.                 using (SqlDataAdapter da = new SqlDataAdapter(cmd))  
  9.                 {  
  10.                     DataSet ds = new DataSet();  
  11.                     da.Fill(ds);  
  12.                     try  
  13.                     {  
  14.                         lblCreditLimit.Text = ds.Tables[0].Rows[0]["CreditLimit"].ToString();  
  15.                         lblARBalance.Text = ds.Tables[0].Rows[0]["ARBalance"].ToString();  
  16.   
  17.                     }  
  18.                     catch (Exception ex)  
  19.                     {  
  20.                         throw ex;  
  21.                     }  
  22.                 }  
  23.             }  
  24.         }  
 But when the accounts receivable has no value, 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.  
When I will deploy this program, all the accounts receivable will be set to null or zero. This means I will be facing a problem when a certain agent first time uses the program. Can someone advice or show me on how to deal with this error? 
 
Thanks for your help. 
 

Answers (6)