Read a txt file index outside the matrix

1

I'm trying to read a txt file but I get the error "index out of the matrix"

if (nFile.Length > 0)
{
    StreamReader rd = new StreamReader(directorio + "\log.txt");

    while (rd.Peek() != -1)
    {
        string s = rd.ReadLine();
        string[] arr = s.Split(';');
        string[] arrLog = arr[0].Split(',');

        fw.mdbcmd.Parameters.Clear();
        fw.mdbcmd.CommandText = "GuardaEvento";
        fw.mdbcmd.CommandType = CommandType.StoredProcedure;
        fw.mdbcmd.Parameters.Add("@ID", OleDbType.VarChar, 50).Value = arrLog[0].ToString().Trim();
        fw.mdbcmd.Parameters.Add("@code", OleDbType.VarChar, 50).Value = arrLog[1].ToString().Trim();
        fw.mdbcmd.Parameters.Add("@bando", OleDbType.VarChar, 50).Value = arrLog[2].ToString().Trim();
        fw.mdbcmd.Parameters.Add("@observacion", OleDbType.VarChar, 100).Value = arrLog[3].ToString().Trim();
        fw.mdbcmd.Parameters.Add("@user", OleDbType.VarChar, 50).Value = arrLog[4].ToString().Trim();
        fw.mdbcmd.ExecuteNonQuery();
    }

    rd.Close();
    File.WriteAllText(directorio + "\log.txt", "");

}

This is what I keep in the log.txt file:

public void guardarLogTxt(string codigo, string numParte, string descripcion, string user)
  {
 string directorio = System.IO.Directory.GetCurrentDirectory();
StreamWriter escribirLog = File.AppendText(directorio + "\log.txt");
 String contenido = Properties.Settings.Default.id.ToString() + "," + code + "," + bando + "," + observacion + ","+ user + ";" + Environment.NewLine;
                escribirLog.Write(contenido.ToString());
                escribirLog.Flush();
                escribirLog.Close();
            }
    
asked by Ronald López 25.04.2018 в 17:18
source

1 answer

1

The error is due to the fact that the condition of doing split to 5 positions of the arrangement is not always fulfilled. I suggest that you use Linq in conjunction with a class to make a cleaner data mapping and also to ensure that you always get the expected result.

Class Datos :

public class Datos {
    public string ID { get; set; }
    public string code { get; set; }
    public string bando { get; set; }
    public string observacion { get; set; }
    public string user { get; set; }    
}

Code:

List<Datos> listaArchivo = new List<Datos>();

listaArchivo = (from p in File.ReadAllLines(directorio + "\log.txt")
        let parts = p.Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries) // <-- Aquí se hace el split
        where parts.Length == 5 // <-- Aquí siempre aseguras que se hará el split a 5 posiciones, las líneas del archivo que no cumplan con la condición se omiten
        select new Datos
        {
            ID = parts[0].ToString().Trim(),
            code = parts[1].ToString().Trim(),
            bando = parts[2].ToString().Trim(),
            obseracion = parts[3].ToString().Trim(),
            user = parts[4].ToString().Trim()
        }).ToList();

foreach(Datos item in listaArchivo) 
{
    fw.mdbcmd.Parameters.Clear();
    fw.mdbcmd.CommandText = "istl.SP_PRIMASYS_GuardaEvento";
    fw.mdbcmd.CommandType = CommandType.StoredProcedure;
    fw.mdbcmd.Parameters.Add("@ID", OleDbType.VarChar, 50).Value = item.ID;
    fw.mdbcmd.Parameters.Add("@code", OleDbType.VarChar, 50).Value = item.code;
    fw.mdbcmd.Parameters.Add("@bando", OleDbType.VarChar, 50).Value = item.bando;
    fw.mdbcmd.Parameters.Add("@observacion", OleDbType.VarChar, 100).Value = item.obsercacion;
    fw.mdbcmd.Parameters.Add("@user", OleDbType.VarChar, 50).Value = item.user;
    fw.mdbcmd.ExecuteNonQuery();
}
    
answered by 25.04.2018 / 17:33
source