-2

im trying to test if my list is collecting data from a database but when i try to get a message box to print a postcode from the list it gives me the exeption `System.ArgumentOutOfRangeException: 'Index was out of range. Must be non-negative and less than the size of the collection.

Parameter name: index'`

here are the methods which i have written and am using

        private List<string> GetPostcodes(string table)

    {

        connect = new MySqlConnection(connectionString);

        connect.Open();

        string selectString = "select postcode from " + table;



        MySqlCommand cmd = new MySqlCommand(selectString,connect);

       reader = cmd.ExecuteReader();

        while (reader.Read())

        {

            postcodes.Add(reader.GetOrdinal("postcode").ToString());

        }

        connect.Close();



        return postcodes;

    }

the list postcodes is defined earlier in my code like this List<string> postcodes = new List<string>();

and here is how im trying to test the collection of the postcodes

        private void Button_Click1(object sender, RoutedEventArgs e)

    {

        string test1 = postcodes[1];

        MessageBox.Show(test1);

    }
Sourav Ghosh
  • 133,132
  • 16
  • 183
  • 261
  • This is not [tag:c]. – Sourav Ghosh Dec 10 '19 at 14:33
  • @SouravGhosh thanks m8 – Fawcett Fawcett Dec 10 '19 at 14:34
  • If you target index 1; `postcodes[1]`, you'll actually targetting the 2nd element. are you aware of that? ( use `postcodes[0]` for the first ) – Stefan Dec 10 '19 at 14:35
  • Side note: use `using` statements to dispose of objects that implement `IDisposable`... – Trevor Dec 10 '19 at 14:37
  • We tried the first index `postcodes[0]` and that also gave the same error. about the index is out of bounds. is there a problem with the way that the postcodes are being retrived in the `GetPostcodes` method which we cannot see? – kian5749 Dec 10 '19 at 14:42
  • FYI `GetOrdinal` will return the index of the column name and not the actual data from that column. So, you're filling up your list with the same number over and over again. – juharr Dec 10 '19 at 14:46
  • we also just tried `GetString` but that also did not work, we no longer get the error but the `MessageBox.Show()` just now shows an empty message box – kian5749 Dec 10 '19 at 14:51
  • Have you tried stepping through the code to see if you get any records returned. Put a breakpoint in your button Click event & see how many elements in the list. – PaulF Dec 10 '19 at 15:11

1 Answers1

0

Presuming that string test1 = postcodes[1]; is the line causing the issue, clearly index 1 does not exist in the postcodes list.

Remember that index 1 refers to the second element in C#. If you only have one postcode, or you meant to refer to the first element, you need to use:

string test1 = postcodes[0];

You could also check to see whether the postcode at index 1 exists:

if (postcodes.Count >= 2)
    // postcodes[1] definitely exists
    ...
Martin
  • 16,093
  • 1
  • 29
  • 48