IndexOutOfRangeException

0

The error marks me when it enters the Reader of my while and causes "who"

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Data.OleDb;

namespace tts
{
    public class conexion
    {
        public DataTable connect()
        {
            string myConnectionString = @"C:\Users\gutiece\Desktop\database\" + "Database1.accdb";

            DataTable SBTable = new DataTable();

            OleDbConnection connection = new OleDbConnection();
            OleDbCommand command = new OleDbCommand();
            OleDbDataAdapter adapter = new OleDbDataAdapter();
            DataSet dataset = new DataSet();

            connection.ConnectionString = @"Provider=Microsoft.ACE.OLEDB.12.0; Data source= " + myConnectionString;
            bool ok = System.IO.File.Exists(myConnectionString);
            String qry = "SELECT *  FROM users";
            command.Connection = connection;
            command.CommandText = qry;

            adapter.SelectCommand = command;

            command.Connection.Open();
            OleDbDataReader reader = command.ExecuteReader(); // close conn after 
            //adapter.Fill(SBTable);

            List<datos> Resultado = new List<datos>();

            while (reader.Read())
            {
                Resultado.Add(new datos(

                    reader["who"].ToString(),

                    int.Parse(reader["payroll_number"].ToString()),

                    reader["name"].ToString(),

                    reader["ou"].ToString(),

                    int.Parse(reader["pool"].ToString()),

                    int.Parse(reader["team"].ToString()),

                    int.Parse(reader["rol_id"].ToString())
                    ));
            }

            if (!reader.IsClosed)
            {
                reader.Close();
            }

            return SBTable;
        }
    }
}

This is my other class with which you communicate

public class datos
    {
        public String WHO { get; set; }
        public int PAYROLL_NUMBER { get; set; }
        public String NAME { get; set; }
        public String OU { get; set; }
        public int POOL { get; set; }
        public int TEAM { get; set; }
        public int ROL_ID { get; set; }

        public datos(
            String who,
            int payroll_number,
            String name,
            String ou,
            int pool,
            int team,
            int rol_id
            )
        {
            this.WHO = who;
            this.PAYROLL_NUMBER = payroll_number;
            this.NAME = name;
            this.OU = ou;
            this.POOL = pool;
            this.TEAM = team;
            this.ROL_ID = rol_id;
        }
        private datos() { }
    }
    
asked by Cesar Gutierrez Davalos 03.05.2017 в 14:17
source

1 answer

0

Instead of:

 SELECT *  FROM users

Do the search by the columns of the database, in your case:

Select who, PAYROLL_NUMBER,.... from users
    
answered by 03.05.2017 / 14:22
source