locked
Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: index RRS feed

  • Question

  • Hello everyone, I am trying to retrieve integers or Foreign Keys from a table with a select query and put them into an int list however I keep getting the error in the header. 

     

     

     if (intCaseID != 0)
        {
         List<int> lstOffenseIDs = new List<int>();
         string strSelectOffenseIDs = "SELECT OffenseID FROM tblJunctionOffenseCase WHERE CaseID = ?";
         OleDbCommand getOffenseIDs = new OleDbCommand(strSelectOffenseIDs, Launch);
    
         getOffenseIDs.Parameters.Add("@CaseID", OleDbType.Integer).Value = intCaseID;
    
         Launch.Open();
         OleDbDataReader reader;
    
         try
         {
          int count = 0;
          reader = getOffenseIDs.ExecuteReader();
          while (reader.Read())
          {
           lstOffenseIDs[count] = reader.GetInt32(2);
           count++;
          }
         }
    
         catch (OleDbException E)
         {
          MessageBox.Show("Failed to insert OffenseID's into list at edit/view offense trigger.");
          Debug("Failed to insert OffenseID's into list at edit/view offense trigger.");
          bolListReader = false;
         }
    
         Launch.Close();
    
         if (bolListReader == true)
         {
          EditViewOffenses editoffense = new EditViewOffenses((Juvenile_Database_Round_2.mdiMain)MdiParent, lstOffenseIDs);
          editoffense.Show();
         }
        }
    
        else
        {
         MessageBox.Show("Select Query Failed at view/edit offenses. intCaseID == 0");
         Debug("Select Query Failed at view/edit offenses. intCaseID == 0");
        }
       }
      }
    
    


     


    Wednesday, August 24, 2011 8:44 PM

Answers

  • you Read from dataBase twice, this is not needed. I would better use a list<T> to insert values from dataBase in the run time ansd only one reading loop will be needed:

    using(OleDbDataReader reader = getOffenseIDs.ExecuteReader())
    {
       List<int> list = new List<int>();
       try
       {   
         while (reader.Read())
            list.Add((int)reader[0]); //same as yours   
       }
       catch (Exception ex)
       {
          MessageBox.Show(ex.Message);
       }
    }
    

    Its more preetier to look this code? And its a way shorter and slimmer.

    Use the list now, which holds your values from db.

    Btw, I use using keyword which wraps the DataReader class which is IDisposable too, so you dont need to use Close() or Dispose() method. It disposes by it self, when leaving rhe using block.


    Mitja
    • Proposed as answer by Shweta Jain (Lodha) Thursday, August 25, 2011 5:01 AM
    • Marked as answer by Vamp10988 Thursday, August 25, 2011 4:51 PM
    Wednesday, August 24, 2011 11:58 PM
  • to the best of my knowledge, lists do not need to have a definate value declared. 

    You can call Add() to add values to a list. You can use the indexer [] only on already existing indexes.

    Your initial code can work by rewriting your try block as:

    try
    {
       reader = getOffenseIDs.ExecuteReader();
       while (reader.Read())
       {
          lstOffenseIDs.Add(reader.GetInt32(0));
    
       }
    }
    

     

    • Marked as answer by Vamp10988 Thursday, August 25, 2011 4:51 PM
    Thursday, August 25, 2011 10:00 AM

All replies

  • You are only selecting one column but you try to read the third column.

    Change 

    lstOffenseIDs[count] = reader.GetInt32(2);

    to

    lstOffenseIDs[count] = reader.GetInt32(0);

    Wednesday, August 24, 2011 9:07 PM
  • hello andreas. I tried that and I still got the same error. I am trying to retrieve a list of values from the third column of that table which is why I originally had the two in there. Is my logic and syntax correct at least? Because I am thinking it might have something to do with my actual database.
    Wednesday, August 24, 2011 9:13 PM
  • For anyone that might be wondering how to solve this I figured it out. I am not quite sure as to why my original method did not work considering that to the best of my knowledge, lists do not need to have a definate value declared. 

     

      OleDbDataReader reader;
              int[] test;
    
              try
              {
                int count = 0;
                reader = getOffenseIDs.ExecuteReader();
                
                while (reader.Read())
                {
                  
                  count++;
                }
    
                test = new int[count];
                reader.Close();
    
                reader = getOffenseIDs.ExecuteReader();
    
                for (int x = 0; x < count; x++)
                {
                  reader.Read();
                  test[x] = reader.GetInt32(0);
                }
              }
    


    Wednesday, August 24, 2011 10:13 PM
  • you Read from dataBase twice, this is not needed. I would better use a list<T> to insert values from dataBase in the run time ansd only one reading loop will be needed:

    using(OleDbDataReader reader = getOffenseIDs.ExecuteReader())
    {
       List<int> list = new List<int>();
       try
       {   
         while (reader.Read())
            list.Add((int)reader[0]); //same as yours   
       }
       catch (Exception ex)
       {
          MessageBox.Show(ex.Message);
       }
    }
    

    Its more preetier to look this code? And its a way shorter and slimmer.

    Use the list now, which holds your values from db.

    Btw, I use using keyword which wraps the DataReader class which is IDisposable too, so you dont need to use Close() or Dispose() method. It disposes by it self, when leaving rhe using block.


    Mitja
    • Proposed as answer by Shweta Jain (Lodha) Thursday, August 25, 2011 5:01 AM
    • Marked as answer by Vamp10988 Thursday, August 25, 2011 4:51 PM
    Wednesday, August 24, 2011 11:58 PM
  • to the best of my knowledge, lists do not need to have a definate value declared. 

    You can call Add() to add values to a list. You can use the indexer [] only on already existing indexes.

    Your initial code can work by rewriting your try block as:

    try
    {
       reader = getOffenseIDs.ExecuteReader();
       while (reader.Read())
       {
          lstOffenseIDs.Add(reader.GetInt32(0));
    
       }
    }
    

     

    • Marked as answer by Vamp10988 Thursday, August 25, 2011 4:51 PM
    Thursday, August 25, 2011 10:00 AM
  • Thats what I was getting wrong! I wasn't calling ADD duhh. i feel so dumb Im still so used to working with arrays that I tried to use my list like an array.
    Thursday, August 25, 2011 4:52 PM