Recursive problem when integrating files

Asked

Viewed 69 times

1

I am doing a recursive integration of several files,when running with only one file it integrates normal, but when I try to integrate several 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 that

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

but the same file being integrated alone does not present the error follows below 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);
                }
            }
    }
  • Ai. It was scary!!!

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 and 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;`
`}`
  • It cannot be that because when I integrate only this file it goes smoothly. If it were the vector it would not integrate either alone or accompanied. The error is not Index Out of Range

  • @Rafaelarnosti refatora your code, creates support methods to facilitate debug.

0

I discovered the reason when he is integrating more than one file in the file that he references in this 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 with values that pop the field in sql base so the error is

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

Browser other questions tagged

You are not signed in. Login or sign up in order to post.