2

I keep getting the following error: "Index was out of range. Must be non-negative and less than the size of the collection", while executing the following code:

 try
        {
            string connectionstring = "server=localhost;user id=root;database=itemdb;password=root";
            MySqlConnection conn = new MySqlConnection(connectionstring);
            conn.Open();
            MySqlCommand cmd = new MySqlCommand("SELECT item.item_name,location.city,time.month,SUM(unit_sales) FROM sales_fact,time,item,location WHERE(sales_fact.item_key = item.item_key AND sales_fact.location_key = location.location_key AND sales_fact.time_key = time.time_key) GROUP BY item.item_name", conn);
            MySqlDataReader dr = cmd.ExecuteReader();
            int c = 0;
            List<Cube1> cubelist = new List<Cube1>();
            while (dr.Read())
            {
                //i++;
                cubelist[c].itemname = dr.GetValue(0).ToString();
                cubelist[c].city = dr.GetValue(1).ToString();
                cubelist[c].month = dr.GetValue(2).ToString();
                cubelist[c].totalsales = (int)dr.GetValue(3);
                MessageBox.Show(cubelist[0].totalsales.ToString());
                c++;
            }
            dr.Close();
            MySqlDataAdapter da = new MySqlDataAdapter(connectionstring, conn);
            DataSet dt = new DataSet();
            da.Fill(dt);
            dataGridView1.DataSource = dt;
            conn.Close();
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }

Where I'm going wrong?

2
  • 1
    You should be doing cubelist.Add(). Commented Apr 27, 2014 at 21:18
  • Min mod: change //i++; to cubelist.Add(new Cube1()); Commented Apr 27, 2014 at 21:18

2 Answers 2

2

Your list is empty, but you are trying to access it's elements.You should use List<T>.Add method first. In your code:

new List<Cube1>(100);

100 only specifies the Capacity, it isn't the size of your list.It is used to allocate an array with specified length to avoid re-allocation every time you add a new item to your list.

cubelist.Add(new Cube());
cubelist[c].itemname = dr.GetValue(0).ToString();
cubelist[c].city = dr.GetValue(1).ToString();
...
Sign up to request clarification or add additional context in comments.

2 Comments

The Poster has changed its code now, but not the title :-)
@Steve yea you right but still the same exception remains. :) the indexer of List<T> throws exception, in order to get nullreference you should have at least a null item :)
2

You create a new (empty) List<Cube1> and then try to assign to properties of elements that don't exist because the list is empty. Instead of using cubelist[c], you need to be doing var cube = new Cube1() to instantiate a new Cube1, then assigning the values to its properties, then adding that to the list using cubelist.Add(cube).

List<Cube1> cubelist = new List<Cube1>();
while (dr.Read())
{
    // Create a new Cube1 instance
    var cube = new Cube1();

    // Set its properties
    cube.itemname = dr.GetValue(0).ToString();
    cube.city = dr.GetValue(1).ToString();
    cube.month = dr.GetValue(2).ToString();
    cube.totalsales = (int)dr.GetValue(3);

    // Add it to the list
    cubelist.Add(cube);

    MessageBox.Show(cube.totalsales.ToString());
}

You don't need your c variable at all.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.