Problem with recursion when integrating files

1

I'm doing a recursive integration of several files, when I run with only one file it integrates normal, but when I try to integrate multiple files it gives this error.

    {"An error occurred while updating the entries. See the inner exception for details."}

Looking at the Inner Exception I find this

    {"Parameter value '99999,999' is out of range."}

but the same file being integrated alone does not display the error below is my code

private void ProcessarCEP(int i,FileInfo file,string [] Lines)
    {
        string vNomeArquivo = string.Empty;
        DBCEPEntities DBCEP = new DBCEPEntities();
        DirectoryInfo info = new DirectoryInfo(file.DirectoryName);
        FileInfo[] files = info.GetFiles("*.txt", SearchOption.AllDirectories);
        int vTamanhoBase = DBCEP.CAD_CEP.Where(t => t.UF == file.Name.Substring(15,2)).Count();

        for (int a = vTamanhoBase; a < i + vTamanhoBase; a++)
            {
                if (a >= Lines.Count()) break;

                CAD_CEP CEP = new CAD_CEP();
                string[] Registros = Lines[a].Split('@');

                CEP.CEP = Convert.ToDecimal(Registros[7]);
                CEP.UF = Registros[1];
                CEP.LOGRADOURO = Registros[8];
                CEP.ENDERECO = Registros[5];
                if (Registros[2] != string.Empty)
                {
                    decimal CodLocal = Convert.ToDecimal(Registros[2]);
                    var local = DBCEP.CAD_CIDADE.Where(c => c.CODLOCAL == CodLocal).FirstOrDefault();
                    CEP.LOCAL = local.LOCAL;
                }
                if (Registros[3] != string.Empty)
                {
                    decimal CodBairro = Convert.ToDecimal(Registros[3]);
                    var bairro = DBCEP.CAD_BAIRRO.Where(c => c.CODBAIRRO == CodBairro).FirstOrDefault();
                    CEP.BAIRRO = bairro.BAIRRO;
                }
                vNomeArquivo = ConfigurationManager.AppSettings["NomeArquivoSequencia"].ToString();
                if (files.Where(f => f.Name.ToUpper() == vNomeArquivo.ToUpper()).Count() > 0)
                {
                    if (Registros[0] != string.Empty)
                    {
                        string[] LinesSec = File.ReadAllLines(files.Where(f => f.Name.ToUpper().StartsWith(vNomeArquivo.ToUpper())).FirstOrDefault().FullName.ToString(), Encoding.Default);
                        string vLinhaSec = LinesSec.Where(l => l.StartsWith(Registros[0] + "@")).FirstOrDefault();
                        if (vLinhaSec != null)
                        {
                            string[] vLinhaSplit = vLinhaSec.Split('@');

                            if (vLinhaSplit[1] != string.Empty)
                            {
                                CEP.DE = Convert.ToDecimal(vLinhaSplit[1]);
                            }
                            if (vLinhaSplit[2] != string.Empty)
                            {
                                CEP.ATE = Convert.ToDecimal(vLinhaSplit[2]);
                            }
                            CEP.FL_PAR_IMPAR = vLinhaSplit[3];
                        }
                        Registros = null;
                        LinesSec = null;
                    }
                }
                DBCEP.CAD_CEP.Add(CEP);
            }
            DBCEP.SaveChanges();
            vTamanhoBase = DBCEP.CAD_CEP.Where(t => t.UF == file.Name.Substring(15, 2)).Count();
            DBCEP.Dispose();
            GC.WaitForPendingFinalizers();
            GC.Collect();

            if (Lines.Count() <= i + vTamanhoBase)
            {
               if(vTamanhoBase != Lines.Count())
               {
                   ProcessarCEP(Lines.Count() - i, file, Lines);
               }                    
            }
            else
            {
                if(vTamanhoBase != Lines.Count())
                {
                    ProcessarCEP(i, file, Lines);
                }
            }
    }
    
asked by anonymous 15.09.2015 / 21:34

2 answers

0

"Out of range" your "for" is trying to access a position of the vector that does not exist. Remember that count returns the quantity, not the position. Example

var t = Lista.Count // vai me retornar a quantidade de elementos
for(var i = 1; i <= t; i++)
'{'
   'var elemento = lista[i] - 1;'
'}'
    
15.09.2015 / 21:42
0

I discovered the reason when it is integrating more than one file into the file it refers to in that part

       vNomeArquivo = ConfigurationManager.AppSettings["NomeArquivoSequencia"].ToString();
            if (files.Where(f => f.Name.ToUpper() == vNomeArquivo.ToUpper()).Count() > 0)
            {
                if (Registros[0] != string.Empty)
                {
                    string[] LinesSec = File.ReadAllLines(files.Where(f => f.Name.ToUpper().StartsWith(vNomeArquivo.ToUpper())).FirstOrDefault().FullName.ToString(), Encoding.Default);
                    string vLinhaSec = LinesSec.Where(l => l.StartsWith(Registros[0] + "@")).FirstOrDefault();
                    if (vLinhaSec != null)
                    {
                        string[] vLinhaSplit = vLinhaSec.Split('@');

                        if (vLinhaSplit[1] != string.Empty)
                        {
                            CEP.DE = Convert.ToDecimal(vLinhaSplit[1]);
                        }
                        if (vLinhaSplit[2] != string.Empty)
                        {
                            CEP.ATE = Convert.ToDecimal(vLinhaSplit[2]);
                        }
                        CEP.FL_PAR_IMPAR = vLinhaSplit[3];
                    }

This is with values that populate the field in the sql database so the error is

      {"Parameter value '99999,999' is out of range."}
    
16.09.2015 / 18:38