question

Jackson1990-7147 avatar image
0 Votes"
Jackson1990-7147 asked SimpleSamples commented

Error in code

Hi,
I've got issue
Unhandled Exception: System.IndexOutOfRangeException: Index was outside the bounds of the array.

when running code below against this file.
https://1drv.ms/t/s!Ai8CrEskdewXvwCNSDxrFcm60_D-?e=uYhjTP
Why?

using System;
using System.IO;
using System.IO.Compression;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.UI;
using System.IO.Compression;
using System.Text.RegularExpressions;
using System.Xml;
using System.Globalization;
//using Ionic.Zip;
using System.Net;
using System.Net.Http;
using System.Collections.Generic;
using System.Data.OleDb;
//using Microsoft.VisualBasic;
using System.Text; // This was needed to make ASCIIEncoding function
using System.Security.Cryptography; // This was needed to make everything related to MD5 function
using Outlook = Microsoft.Office.Interop.Outlook;

namespace Validate_File1
{
class Program
{
static void Main(string[] args)
{
string Email = "", s_Partner = "", File0, File1, File1b, File2, File3, F4, Call_Sign, Line0= "", Line1 = "", Line2 = "", Line3 = "", Line_Hold = "", ftx_Line1 = "", eqd_Line1 = "", cta_Line1 = "", Ver0 = "", line = "", Str0, Str1 = "";
int Pos0, Pos1, Pos2, Pos3, Pos4, Pos5, Pos6, Pos7, Pos8, Pos9, Pos10 = 0, Pos11 = 0, Pos12, Pos13, Pos14, Pos15, Pos_Hold0, Pos_Hold1, Sep_Count, Len0, UNT_pos, Tot_line, Total_Line1 = 0, eqd2_line_cnt = 0;
bool UNB_Exist, UNZ_Exist, UNT_Exist, NAD_Exist = false, Voy_Ref0, Voy_Ref1, gid_Line, ftx_Line, eqd_Line, eqd2_line = false, eqd3_line = false, cta_Line, cta_OK, codeco = false, Found0, BGM_line = false, TDT_line = false, LOC_line = false;
bool allowappend = true;
Outlook.MailItem mailItem = null;

          File0 = "C:/cmp3g/CODECO_ERROR_a/" + args[0] + "/" + args[1];
          //File1 = "C:/cmp3g/CODECO_ERROR_a/Error" + Format(Now, "mmss") + ".txt"
          File1 = "C:/cmp3g/CODECO_ERROR_a/Error.txt";
          File1b = "C:/cmp3g/CODECO_ERROR_a/Error2.txt";
          File2 = "C:/cmp3g/CODECO_ERROR_a/Temp.txt";
          File3 = "C:/cmp3g/CODECO_ERROR_a/List0.txt";
          F4 = "C:/cmp3g/CODECO_ERROR_a/List7.txt";
          List<string> l7 = new List<string>();
          using (StreamReader sr2 = new StreamReader(@"C:/cmp3g/CODECO_ERROR_a/List7.txt", Encoding.Default))
              ////using (StreamReader sr2 = new StreamReader(@"e:/Upd_China_city.sql", Encoding.Default))
              //{
              while ((line = sr2.ReadLine()) != null)
              {
                  //        //if (line.Trim() == "")
                  //        //{
                  //        //}
                  //        //else
                  //        {
                  //            //str.Add(line.Trim().Substring(6));
                  l7.Add(line);
                  //            //str.Add((line+"").Substring(5));
                  //        }
              }
          Outlook.Application outlookApp = new Outlook.Application();
          if (File.Exists(@File2))
          {
              File.Delete(@File2);
          }
          File.Copy(@File0, @File2);
          Sep_Count = 0; UNT_pos = 0; Tot_line = 0;
          UNB_Exist = false; UNT_Exist = false; UNZ_Exist = false; Voy_Ref0 = false; Voy_Ref1 = false; gid_Line = false; ftx_Line = false; eqd_Line = false; cta_Line = false; cta_OK = false;
          try
          {
              using (StreamReader sr = new StreamReader(@File1, Encoding.Default))
              {
                  while ((Line0 = sr.ReadLine()) != null)
                  {
                      Total_Line1++;
                      Pos0 = Line0.IndexOf("for partner code '"); Pos1 = Line0.IndexOf("'", Pos0 + 18);
                      if (Pos0 > 0 && Pos1 > 0)
                      {
                          if (Pos1 - Pos0 - 17 > 1)
                          {
                              if (Pos1 - Pos0 - 19 > 0)
                              {
                                  Str0 = Line0.Substring(Pos0 + 18, Pos1 - Pos0 - 19); Pos2 = Str0.Trim().Length;
                              }
                              else
                              {
                                  Str0 = "";Pos2 = 1;
                              }
                              for (int j = 0; j < l7.Count(); j++)
                              {
                                  if (l7[j].Trim().Length >= Str0.Trim().Length)
                                  {
                                      if (l7[j].Substring(0, Pos2) == Str0)
                                      {
                                          Pos3 = l7[j].IndexOf(",,"); Pos4 = l7[j].Trim().Length;
                                          if (Pos3 > 0)
                                          {
                                              mailItem = (Outlook.MailItem)outlookApp.CreateItem(Outlook.OlItemType.olMailItem);
                                              if (Pos4 - 3 - Pos3 > 0)
                                                  Email = l7[j].Substring(Pos3 + 2, Pos4 - 3 - Pos3);
                                              if (mailItem.EntryID != null)
                                              {
                                                  mailItem.Subject = "Coarri Codeco Issue ";
                                                  //mailItem.Send();
                                              }
                                          }
                                      }
                                  }
                              }
                          }
                      }
                      else
                      {
                          if (mailItem == null)
                              mailItem = (Outlook.MailItem)outlookApp.CreateItem(Outlook.OlItemType.olMailItem);
                          if (mailItem.EntryID != null)
                          {
                              mailItem.HTMLBody = mailItem.HTMLBody + Line0;
                              //mailItem.Send();
                          }
                      }
                      //using (StreamWriter sw = new StreamWriter(@File1, allowappend, Encoding.Unicode))
                      //{
                      //    sw.WriteLine("xxx");
                      //    sw.WriteLine(Line0);
                      //    sw.WriteLine("");
                      //}
                      Pos1 = Line0.IndexOf("'", Pos0 + 1);
                      Pos_Hold0 = Line0.IndexOf("'");
                      Pos_Hold1 = Line0.IndexOf("'", Pos_Hold0 + 1);
                      Line_Hold = Line0;
                  }
              }
          }
          catch (Exception e)
          {
              using (StreamWriter sw = new StreamWriter(@File1b, allowappend, Encoding.Unicode))
              {
                  sw.WriteLine("Error happens due to file - '" + File0 + "' with detail:  Error with Stacktrace --> " + e.Message + " " + e.StackTrace+Line0);
                  sw.WriteLine("");
              }
          }
          finally
          {
          }
      }
  }

}

dotnet-csharpdotnet-runtimedotnet-standard
· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

It would make it easier for us if you were to remove irrelevant comments from the code. Also, format the code properly.

And please, please, please tell us what line causes the error.

You are nearly guaranteed a good answer if you can create a sample program that recreates the problem. That might seem like a lot of work but it is something I have done in the past to get answers.

0 Votes 0 ·

1 Answer

vb2ae avatar image
0 Votes"
vb2ae answered

I suspect the error is happening on one of the Substrings. I would put a check to make sure the string is long enough to access the position you are looking for. (ie make sure you are trying to get data from position 10 of a 9 character long string)

I would also recommend you name the variables a little better if you look at your code 6 months from now a name like Str0 might not make as much sense to you as it does now.

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.