IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

C# Discussion :

index was outside of the bounds of the array [Débutant]


Sujet :

C#

  1. #1
    Membre habitué
    Inscrit en
    Novembre 2006
    Messages
    262
    Détails du profil
    Informations forums :
    Inscription : Novembre 2006
    Messages : 262
    Points : 163
    Points
    163
    Par défaut index was outside of the bounds of the array
    bonjour a tous,

    soit le code suivant :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    for (int i = 1; i <=nbLigne; i++)
                                    {
                                        int numEnga = 0;
                                        decimal sold = 0;
                                        decimal montant_Designation = 0;
                                        int CodeN = 0;
                                        int NumF = 0;
     
                                        req = "select sold,montant_tot_design,CodeN,NumF,Num_Enga from tab_engagement where Exercice=@ExerciceCurrent and Code_SC=@CodeSCCurrent and Code_A = @CodeACurrent and NumF=@NumFCurrent";
                                        connexion.AddParametre("@ExerciceCurrent", SqlDbType.Int, Engagement.Exercice);
                                        connexion.AddParametre("@CodeSCCurrent", SqlDbType.Int, Engagement.CodeSC);
                                        connexion.AddParametre("@CodeACurrent", SqlDbType.Int, Engagement.CodeA);
                                        connexion.AddParametre("@NumFCurrent", SqlDbType.Int, Engagement.NFiche + 1); // Engagement.NFiche+1
                                        connexion.ExecuteReader();
     
                                        if (connexion.rdr.HasRows)
                                        {
                                            while (connexion.rdr.Read())
                                            {
                                                   sold = Convert.ToDecimal(connexion.rdr.GetValue(0));
                                                   montant_Designation = Convert.ToDecimal(connexion.rdr.GetValue(1)); // ici j'ai le message d'erreur.
                                                   CodeN = Convert.ToInt32(connexion.rdr.GetValue(2));
                                                  NumF = Convert.ToInt32(connexion.rdr.GetValue(3));
                                                numEnga = Convert.ToInt32(connexion.rdr.GetValue(4)); 
     
                                            }
                                        }
                                     }
    en executant le bout de code pas à pas, j'ai le message d'erreur suivant :
    Index was outside the bounds of the array
    l'exception est générée au niveau de la ligne :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    montant_Designation = Convert.ToDecimal(connexion.rdr.GetValue(1));
    .

    il est a noté que lorsque je teste directement la requête sur sqlServer elle me retourne le résultat correcte.

    quelqu'un pourrait il me dire ou se situe le problème.

    Merci.

  2. #2
    Membre habitué
    Inscrit en
    Novembre 2006
    Messages
    262
    Détails du profil
    Informations forums :
    Inscription : Novembre 2006
    Messages : 262
    Points : 163
    Points
    163
    Par défaut
    ah excusez moi, j'avais juste oublié d'initialiser ma requêtes comme suit :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    connexion.req = req;
              connexion.CreateReq();
    tel que :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    connectionsql connexion = new connectionsql();
    et :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
     
     class connectionsql
         {
           SqlConnection con;
           public SqlDataReader rdr;
           SqlCommand  cmd;
           public string req;
           string strcon = ConfigurationManager.ConnectionStrings["connection"].ConnectionString; 
     
     
           public connectionsql()
            {
               con=new SqlConnection(strcon);
               con.Open();
               cmd = new SqlCommand();
     
            }
     
           public void CreateReq()
           {
               cmd.CommandText = req;
               cmd.Connection = con;
           }
    }
    Voila

+ Répondre à la discussion
Cette discussion est résolue.

Discussions similaires

  1. Réponses: 0
    Dernier message: 25/01/2012, 15h34
  2. Réponses: 1
    Dernier message: 22/04/2010, 12h24
  3. ERREUR : Index was outside the bounds of the array
    Par maxwel56 dans le forum Linq
    Réponses: 1
    Dernier message: 06/02/2010, 10h40
  4. Index was outside the bounds of the array
    Par richton95 dans le forum Windows Presentation Foundation
    Réponses: 16
    Dernier message: 01/09/2009, 16h07
  5. [VB.NET] Tableau "index was outside of bound array"
    Par guillaume1998 dans le forum VB.NET
    Réponses: 1
    Dernier message: 11/11/2005, 14h07

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo