Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to fix Index Out Of Range error

Tags:

c#

How can I fix this error:

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

In this code:

dataGridView1.AllowUserToAddRows = false;
dataGridView1.Columns.Add("ID", "ID");
dataGridView1.Columns.Add("Firstname", "Firstname");
dataGridView1.Columns.Add("MI", "MI");
dataGridView1.Columns.Add("Lastname", "Lastname");
dataGridView1.Columns.Add("Username", "Username");
dataGridView1.Columns.Add("Rights", "Rights");
c.Open();
OleDbCommand cmd = new OleDbCommand();
cmd.Connection = c;
cmd.CommandText = "SELECT * From Account";
OleDbDataReader reader = cmd.ExecuteReader();
while (reader.Read())
{
    dataGridView1.Rows[dataGridView1.Rows.Count - 1].Cells["ID"].Value = reader[0].ToString();
    dataGridView1.Rows[dataGridView1.Rows.Count - 1].Cells["Firstname"].Value = reader[1].ToString();
    dataGridView1.Rows[dataGridView1.Rows.Count - 1].Cells["MI"].Value = reader[2].ToString();
    dataGridView1.Rows[dataGridView1.Rows.Count - 1].Cells["Lastname"].Value = reader[3].ToString();
    dataGridView1.Rows[dataGridView1.Rows.Count -    1].Cells["Username"].Value = reader[7].ToString();
    dataGridView1.Rows[dataGridView1.Rows.Count - 1].Cells["Rights"].Value      = reader[9].ToString();
}
c.Close();
like image 914
YOUn'Me Avatar asked Mar 04 '13 00:03

YOUn'Me


People also ask

What does the index out of range error mean?

You'll get the Indexerror: list index out of range error when you try and access an item using a value that is out of the index range of the list and does not exist. This is quite common when you try to access the last item of a list, or the first one if you're using negative indexing.

How do I stop list assignment index out of range?

The Python "IndexError: list assignment index out of range" occurs when we try to assign a value at an index that doesn't exist in the list. To solve the error, use the append() method to add an item to the end of the list, e.g. my_list.

How do you stop a loop from going out of range?

We can resolve this by simply changing the operator a less than symbol, < . This prevents the loop from looping over the index from going out of range.


2 Answers

Are you trying to display data from database to datagridview? Why not use databind?

See my sample code:

string connStr = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=the directory or the path of your database";
string query = "SELECT * From Table Name";
using (OleDbConnection conn = new OleDbConnection(connStr))
{
    using (OleDbDataAdapter adapter = new OleDbDataAdapter(query, conn))
    {
        DataSet ds = new DataSet();
        adapter.Fill(ds);
        dataGridView1.DataSource = ds.Tables[0];
    }
    conn.Close();
}
like image 104
lexter Avatar answered Sep 22 '22 02:09

lexter


The Rows collection has an 'Add' method that takes an object array. It is definitely a simpler and more straightforward approach that the code sample in your question:

http://msdn.microsoft.com/en-us/library/fbs04kbx.aspx

like image 28
Glenn Ferrie Avatar answered Sep 20 '22 02:09

Glenn Ferrie