Ahmed Khalil
Ahmed Khalil

Reputation: 85

issue in retrieve all Rows of the DataTable to a List

I want retrieve data from DataTable to a List i have used two methods

the first one is :

SqlDataAdapter dataadapter;
        DataSet dataset = new DataSet();
        public List<Groupe> displayGrp()
        {
            DataTable data = new DataTable();

            Requete = "select * from Groupe ";
            dataadapter = new SqlDataAdapter(Requete, cnx.cnx);
            dataadapter.Fill(dataset, "TGroupe");
         data = dataset.Tables["TGroupe"];

         for (int i = 0; i <data.Rows.Count; i++)
         {
             Program.listgrp[i].Codegroupe = int.Parse(data.Rows[i][0].ToString());
             Program.listgrp[i].Nom = data.Rows[i][1].ToString();
             Program.listgrp[i].Année = int.Parse(data.Rows[i][2].ToString());


         }

         return Program.listgrp;
        }

but i get error when i try to call this methode the error is :

Index was out of range. It must not be negative and must be less than the size of the collection. Parameter name: index

but when i use foreach methods like that :

 SqlDataAdapter dataadapter;
        DataSet dataset = new DataSet();
        public List<Groupe> displayGrp()
        {
            DataTable data = new DataTable();

            Requete = "select * from Groupe ";
            dataadapter = new SqlDataAdapter(Requete, cnx.cnx);
            dataadapter.Fill(dataset, "TGroupe");
         data = dataset.Tables["TGroupe"];
         foreach (DataRow row in data.Rows)
         {
             Program.listgrp.Add(new Groupe { Nom = (row["Nom"].ToString()), Codegroupe = int.Parse(row["Codegroupe"].ToString())
                 , Année = int.Parse(row["Année"].ToString()) });
         }


         return Program.listgrp;
        }

All is fine i want to know where is the probleme why the first methode don't success

the class of program :

using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;

namespace Gestion_stagiaire
{
    static class Program
    {
        /// <summary>
        /// Point d'entrée principal de l'application.
        /// </summary>
        /// 
        public static List<Groupe> listgrp = new List<Groupe>();
        public static List<Stagiaire> liststagiaire = new List<Stagiaire>();
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new Form1());
        }
    }
}

Upvotes: 0

Views: 515

Answers (1)

Ant P
Ant P

Reputation: 25221

You're trying to assign to a list by index but there's no evidence that you have any items in your list, so the assignment is bound to fail.

When you try to assign to listgrp[i], there is no listgrp[i]. You need to instantiate a new Groupe and add it to the list.

for (int i = 0; i <data.Rows.Count; i++)
{
    // Create a new Groupe
    var item = new Groupe();

    // Set the properties
    item.Codegroupe = int.Parse(data.Rows[i][0].ToString());
    item.Nom = data.Rows[i][1].ToString();
    item.Année = int.Parse(data.Rows[i][2].ToString());

    // Add it to the list.
    listgrp.Add(item);
}

Upvotes: 1

Related Questions