AllianceMaterials
AllianceMaterials

Reputation: 35

c# and access database system resource exceeded

I have an access database and I have a refresh button that refreshes these list boxes from the database. in case some thing changes and every thing works great unless you click refresh to many times then it throws a system resource problem. i close the connection but is there like a garbage collector or some thing i am missing ?

listBox10.Items.Clear();
listBox6.Items.Clear();
listBox5.Items.Clear();
listBox4.Items.Clear();
listBox3.Items.Clear();
listBox11.Items.Clear();
listBox12.Items.Clear();
listBox13.Items.Clear();
listBox14.Items.Clear();
listBox15.Items.Clear();

progressBar1.Value = 0;
// progressBar1.Increment(1);
connection.Open();

for (int i = -10; i < 10; i++)
{
    try
    {
        OleDbCommand command = new OleDbCommand();
        command.Connection = connection;
        string query = $"select * from Table1 where Status='Arches - InProduction' AND Tamp={i}";
        command.CommandText = query;
        OleDbDataReader reader = command.ExecuteReader();
        progressBar1.Increment(1);
        while (reader.Read())
        {
            listBox15.Items.Add(reader["Description"].ToString());
        }
    }
    catch (Exception ex)
    { MessageBox.Show("error " + ex); }

    try
    {
        OleDbCommand command2 = new OleDbCommand();
        command2.Connection = connection;
        string query = $"select * from Table1 where Status='Arches - PreProduction' AND Tamp={i}";
        command2.CommandText = query;
        OleDbDataReader reader = command2.ExecuteReader();
        progressBar1.Increment(1);
        while (reader.Read())
        {
            listBox10.Items.Add(reader["Description"].ToString());
        }
    }
    catch (Exception ex)
    { MessageBox.Show("error " + ex); }
    progressBar1.Increment(1);

    try
    {
        OleDbCommand command3 = new OleDbCommand();
        command3.Connection = connection;
        string query = $"select * from Table1 where Status='Rock - PreProduction' AND Tamp={i}";
        command3.CommandText = query;
        OleDbDataReader reader = command3.ExecuteReader();
        progressBar1.Increment(1);
        while (reader.Read())
        {
            listBox6.Items.Add(reader["Description"].ToString());
        }
    }
    catch (Exception ex)
    { MessageBox.Show("error " + ex); }



    try
    {
        OleDbCommand command4 = new OleDbCommand();
        command4.Connection = connection;
        string query = $"select * from Table1 where Status='Rock - InProduction' AND Tamp={i}";
        command4.CommandText = query;
        OleDbDataReader reader = command4.ExecuteReader();
        progressBar1.Increment(1);
        while (reader.Read())
        {
            listBox14.Items.Add(reader["Description"].ToString());
        }
    }
    catch (Exception ex)
    { MessageBox.Show("error " + ex); }

    try
    {
        OleDbCommand command5 = new OleDbCommand();
        command5.Connection = connection;
        string query = $"select * from Table1 where Status='TV - PreProduction' AND Tamp={i}";
        command5.CommandText = query;
        OleDbDataReader reader = command5.ExecuteReader();
        progressBar1.Increment(1);
        while (reader.Read())
        {
            listBox5.Items.Add(reader["Description"].ToString());
        }
    }
    catch (Exception ex)
    { MessageBox.Show("error " + ex); }



    try
    {
        OleDbCommand command6 = new OleDbCommand();
        command6.Connection = connection;
        string query = $"select * from Table1 where Status='TV - InProduction' AND Tamp={i}";
        command6.CommandText = query;
        OleDbDataReader reader = command6.ExecuteReader();
        progressBar1.Increment(1);
        while (reader.Read())
        {
            listBox13.Items.Add(reader["Description"].ToString());
        }
    }
    catch (Exception ex)
    { MessageBox.Show("error " + ex); }

    try
    {
        OleDbCommand command7 = new OleDbCommand();
        command7.Connection = connection;
        string query = $"select * from Table1 where Status='Slab - PreProduction' AND Tamp={i}";
        command7.CommandText = query;
        OleDbDataReader reader = command7.ExecuteReader();
        progressBar1.Increment(1);
        while (reader.Read())
        {
            listBox4.Items.Add(reader["Description"].ToString());
        }
    }
    catch (Exception ex)
    { MessageBox.Show("error " + ex); }



    try
    {
        OleDbCommand command8 = new OleDbCommand();
        command8.Connection = connection;
        string query = $"select * from Table1 where Status='Slab - InProduction' AND Tamp={i}";
        command8.CommandText = query;
        OleDbDataReader reader = command8.ExecuteReader();
        progressBar1.Increment(1);
        while (reader.Read())
        {
            listBox12.Items.Add(reader["Description"].ToString());
        }
    }
    catch (Exception ex)
    { MessageBox.Show("error " + ex); }



    try
    {
        OleDbCommand command9 = new OleDbCommand();
        command9.Connection = connection;
        string query = $"select * from Table1 where Status='Cast Stone - PreProduction' AND Tamp={i}";
        command9.CommandText = query;
        OleDbDataReader reader = command9.ExecuteReader();
        progressBar1.Increment(1);
        while (reader.Read())
        {
            listBox3.Items.Add(reader["Description"].ToString());
        }
    }
    catch (Exception ex)
    { MessageBox.Show("error " + ex); }



    try
    {
        OleDbCommand command10 = new OleDbCommand();
        command10.Connection = connection;
        string query = $"select * from Table1 where Status='Cast Stone - InProduction' AND Tamp={i}";
        command10.CommandText = query;
        OleDbDataReader reader = command10.ExecuteReader();
        progressBar1.Increment(1);
        while (reader.Read())
        {
            listBox11.Items.Add(reader["Description"].ToString());
        }
    }
    catch (Exception ex)
    { MessageBox.Show("error " + ex); }



}

connection.Close();

ALso when i try to close it says expecting try

            OleDbCommand command = new OleDbCommand();
            OleDbDataReader reader = command.ExecuteReader();
            try
            {
                //OleDbCommand command = new OleDbCommand();
                command.Connection = connection;
                string query = $"select * from Table1 where      Status='Arches - InProduction' AND Tamp={i}";
                command.CommandText = query;
                //OleDbDataReader reader = command.ExecuteReader();
                progressBar1.Increment(1);
                while (reader.Read())
                {
                    listBox15.Items.Add(reader["Description"].ToString());
                }

            }
            finally
            {
                if (reader != null)
                {
                    reader.Dispose();
                }
            } 

Upvotes: 0

Views: 832

Answers (1)

TomTom
TomTom

Reputation: 62093

You create a lot of small objects that have a lot of memory OUTSIDE the garbage collection. The GC never kicks in because it does not see it.

The main reason is you are ignoring the IDisposable implementing objects and never disposing them. Do that, the problem goes away.

Upvotes: 1

Related Questions