title: Invalid Data in Excel File. Index was outside the bounds of the array.C#
body: <p>Cant find answer in any of the suggestions.I am trying to upload an excel file to the postgre Db.</p>
  
  <p><strong>Here is the C# code :</strong></p>
  
  <pre><code> public static List&lt;Common.Transactions.TransactionDetail&gt; GetTransactionDetails(string path)
      {
          if (!File.Exists(path))
          {
              return null;
          }
          var transactionDetails = new List&lt;Common.Transactions.TransactionDetail&gt;();
          foreach (var sheet in Workbook.Worksheets(path))
          {
              int officeId = 0;
              foreach (var row in sheet.Rows.Skip(1))
              {
                  var td = new Common.Transactions.TransactionDetail
                  {
                      GlAccountId = Core.GlAccounts.GetGlAcccountId(row.Cells[1]?.Text),
                      AccountNumberId = Deposit.AccountHolders.GetAccountNumberId(row.Cells[2]?.Text),
                      ShareAccountId = Core.ShareAccounts.GetShareAccountId(row.Cells[3]?.Text),
                      LoanId = Loan.LoanAccounts.GetLoanId(row.Cells[4]?.Text),
                      Debit = row.Cells[5]?.Text.ToDecimal(),
                      Credit = row.Cells[6]?.Text.ToDecimal(),
                      StatementReference = row.Cells[7]?.Text
                  };
  
  
                  if (row.Cells[7] != null)
                  {
                      td.OfficeId = Office.Offices.GetOfficeIdByCode(row.Cells[7]?.Text);
                      officeId = Office.Offices.GetOfficeIdByCode(row.Cells[7]?.Text);
                  }
  
  
  
                  if (td.AccountNumberId == 0)
                  {
                      td.AccountNumberId = null;
                  }
                  if (td.LoanId == 0)
                  {
                      td.LoanId = null;
                  }
                  if (td.ShareAccountId == 0)
                  {
                      td.ShareAccountId = null;
                  }
  
                  if (row.Cells[0] != null)
                  {
                      td.AccountName = row.Cells[0].Text;
                  }
                  if (row.Cells[1] != null &amp;&amp; string.IsNullOrWhiteSpace(td.AccountName))
                  {
                      td.AccountName = row.Cells[1].Text;
                  }
                  if (row.Cells[2] != null &amp;&amp; string.IsNullOrWhiteSpace(td.AccountName))
                  {
                      td.AccountName = row.Cells[2].Text;
                  }
                  if (row.Cells[3] != null &amp;&amp; string.IsNullOrWhiteSpace(td.AccountName))
                  {
                      td.AccountName = row.Cells[3].Text;
                  }
  
                  #region OfficeId
                  if (td.AccountNumberId != null)
                  {
                      officeId = Deposit.AccountHolders.GetAccountOfficeId(td.AccountNumberId.ToLong());
                  }
                  if (td.LoanId != null)
                  {
                      officeId = Loan.LoanGrant.GetAccountOfficeId(td.LoanId.ToLong());
                  }
                  if (td.ShareAccountId != null)
                  {
                      officeId = Core.ShareAccounts.GetAccountOfficeId(td.ShareAccountId.ToLong());
                  }
  
                  #endregion
  
                  td.OfficeId = officeId != 0 ? officeId : SessionHelper.GetOfficeId();
                  td.OfficeCode = Office.Offices.GetOfficeCode(officeId);
  
                  transactionDetails.Add(td);
              }
          }
          foreach (var detail in transactionDetails)
          {
              if (detail.Debit.ToDecimal() &gt; 0 &amp;&amp; detail.Credit &gt; 0)
              {
                  throw new TransactionDetailException(
                      $@"Invalid transaction. Either debit or credit should be null for '{detail.AccountName}'.");
              }
              if (detail.AccountNumberId &gt; 0)
              {
                  if (detail.Debit.ToDecimal() &gt; 0)
                  {
                      if (detail.Debit &gt; Deposit.AccountHolders.GetDepositAccountBalance(detail.AccountNumberId.ToLong()))
                      {
                          throw new TransactionDetailException(
                              $@"Insufficient balance in account '{detail.AccountName}'.");
                      }
                  }
                  detail.GlAccountId = Deposit.AccountHolders.GetGlAccountId(detail.AccountNumberId);
              }
  
              if (detail.ShareAccountId &gt; 0)
              {
                  if (detail.Debit.ToDecimal() &gt; 0)
                  {
                      if (detail.Debit &gt; Core.ShareAccounts.GetShareBalance(detail.ShareAccountId.ToLong()))
                      {
                          throw new TransactionDetailException(
                              $@"Insufficient balance in account '{detail.AccountName}'.");
                      }
                  }
                  detail.GlAccountId = Core.ShareAccounts.GetGlAccountId(detail.ShareAccountId.ToLong());
              }
  
              if (detail.LoanId &gt; 0)
              {
                  if (detail.Credit.ToDecimal() &gt; 0)
                  {
                      if (detail.Credit &gt; Loan.LoanAccounts.GetLoanCollectionBalance(detail.LoanId.ToLong(), Core.DateConversion.GetCurrentServerDate()))
                      {
                          throw new TransactionDetailException(
                              $@"Insufficient balance in account '{detail.AccountName}'.");
                      }
                  }
                  detail.GlAccountId = Loan.LoanGrant.GetLoanTransactionGLAccountId(detail.LoanId.ToLong());
              }
  
              detail.AuditUserId = Common.Helpers.SessionHelper.GetUserId();
              detail.AccountNumberId = detail.AccountNumberId &lt;= 0 ? null : detail.AccountNumberId;
              detail.LoanId = detail.LoanId &lt;= 0 ? null : detail.LoanId;
              detail.ShareAccountId = detail.ShareAccountId &lt;= 0 ? null : detail.ShareAccountId;
              detail.EnableDeleteButton = true;
  
              if (detail.GlAccountId == 0)
              {
                  throw new TransactionDetailException($@"{detail.AccountName} is not a valid account.");
              }
          }
          return transactionDetails;
      }
  }
  </code></pre>
  
  <p>I get error Index was outside the bounds of array.Invalid excel data.</p>
  
  <p><strong>My excel data</strong> </p>
  
  <pre><code>Gl Account  Deposit A/C Share A/C   Loan A/C    Debit   Credit  Statement Reference
  </code></pre>
  
  <p>Loss Recovery Fund              0   1000    uploaded from excel
  Risk Coverage Fund              0   1106    uploaded from excel</p>