0

A coworker and I got the task to fix an problem on a SQL-Client. The error is a System.OutOfMemoryException.

I figured that it could be because of the client has to many connections that doesn't get closed properly.

I tested a bit around but I can't seem to get it working.

Anyone have a suggestion?

This is the code where the error happens:

        public void SQLIntoDataGridView(OracleCommand cmd, int startIndex, int maxIndex, int ersteSpalte, int maxSpalten, DataTable datenTabelle, DataGridView dgv, int sqlNr, DataTable dt)
    {
        OracleConnection test = new OracleConnection();

        int i = 0;
        int j = 0;
        int spalten = ersteSpalte;
        int id;
        int spaltenZaehler = ersteSpalte;
        int maxrows = datenTabelle.Rows.Count;
        int readZeile = 0;
        int writeZeile = 0;
        int startrow = 0;
        int startSpalte = 0;
        int endSpalte = 0;

        Stopwatch watch = new Stopwatch();
        watch.Start();

        test.ConnectionString = "Data Source=; User ID=; Password=";

        using (test)
        {

            if (test.State == ConnectionState.Closed)
                test.Open(); // <-- this is where the error occures

            using (OracleDataReader reader = cmd.ExecuteReader())
            {
                datenTabelle.Load(reader);
                dataGridViewErgebnis.DataSource = dt;
                dataGridViewTest.DataSource = datenTabelle;
                dataGridViewErweitern(sqlNr);
                #region Rechnungsempfänger X
                if (startIndex == 27 || startIndex == 24)
                {
                    while (j < dataGridViewErgebnis.Rows.Count - 1)
                    {
                        id = startIndex;
                        if (dataGridViewErgebnis.Rows[j].Cells[21].Value.ToString() == "")
                        {
                            readZeile++;
                            i = readZeile;
                            j++;
                            writeZeile++;
                        }
                        else if (dataGridView1.Rows[startrow].Cells[0].Value.ToString() == dataGridViewErgebnis.Rows[j].Cells[21].Value.ToString())
                        {
                            if (startIndex == 24)
                            {
                                startSpalte = 7;
                                endSpalte = 9;
                                spaltenZaehler = startSpalte;
                            }
                            else if (startIndex == 27)
                            {
                                startSpalte = 10;
                                endSpalte = 14;
                                spaltenZaehler = startSpalte;
                            }
                            spaltenZaehler = startSpalte;
                            maxSpalten = endSpalte;
                            while (spaltenZaehler <= maxSpalten)
                            {
                                i = startrow;
                                dataGridViewErgebnis.Rows[writeZeile].Cells[id].Value = dgv.Rows[i].Cells[spaltenZaehler].Value.ToString();
                                spaltenZaehler++;
                                id++;
                            }
                            readZeile++;
                            i = readZeile;
                            j++;
                            writeZeile++;
                            spaltenZaehler = startSpalte;
                            startrow = 0;
                        }
                        else
                        {
                            startrow++;
                        }
                        if (i == dt.Rows.Count - 1 && j == datenTabelle.Rows.Count - 1)
                        {
                            id = startIndex;
                            while (id <= maxIndex && spaltenZaehler <= maxSpalten)
                            {
                                dataGridViewErgebnis.Rows[writeZeile].Cells[id].Value = dataGridViewTest.Rows[i].Cells[spaltenZaehler].Value.ToString();
                                id++;
                                spaltenZaehler++;
                            }
                        }
                    }
                }
                #endregion
                else
                {
                    while (i < dt.Rows.Count - 1)
                    {
                        id = startIndex;
                        if (dataGridViewErgebnis.Rows[i].Cells[0].Value.ToString() == dataGridViewTest.Rows[j].Cells[0].Value.ToString())
                        {
                            while (spaltenZaehler <= maxSpalten)
                            {
                                dataGridViewErgebnis.Rows[i].Cells[id].Value = dataGridViewTest.Rows[j].Cells[spaltenZaehler].Value.ToString();
                                spaltenZaehler++;
                                id++;
                            }
                            if (startIndex == 10 && j == datenTabelle.Rows.Count - 1)
                            {
                                j--;
                                j--;
                                i++;
                            }
                            if (j <= datenTabelle.Rows.Count - 1)
                            {
                                j++;
                            }
                            spaltenZaehler = ersteSpalte;
                        }
                        else
                        {
                            i++;
                            while (id <= maxIndex)
                            {
                                dataGridViewErgebnis.Rows[i].Cells[id].Value = "";
                                id++;
                            }
                        }
                        if (i == dt.Rows.Count - 1 && j == datenTabelle.Rows.Count - 1)
                        {
                            id = startIndex;
                            while (id <= maxIndex && spaltenZaehler <= maxSpalten)
                            {
                                dataGridViewErgebnis.Rows[i].Cells[id].Value = dataGridViewTest.Rows[j].Cells[spaltenZaehler].Value.ToString();
                                id++;
                                spaltenZaehler++;
                            }
                        }
                        else if (j == datenTabelle.Rows.Count && startIndex != 10)
                        {
                            j--;
                            if (i <= dt.Rows.Count - 2)
                            {
                                i++;
                            }
                        }
                    }
                }
                watch.Stop();
                StoppUhr(watch);
            }
        }
    }
3
  • 1
    I suggest editing your title to "while opening OracleConnection" instead of "while getting data from database" Commented Oct 14, 2019 at 9:02
  • 1
    If you crash on test.Open() only, the rest of your code seems to be unimportant. OutOfMemory is very unlikely than opening a connection. This could be a recursive call. Are you sure this function is not calling itself indirectly ? Or there are some side effects taking place, like cashing some data, when opening a connection. Commented Oct 14, 2019 at 9:02
  • As you are in a "using" statement, it takes care of closing the connection. No need to handle this. I would focus on checking the connection to be available and the process has enough resources to serve the requests. Commented Oct 14, 2019 at 9:04

1 Answer 1

1

I would rewrite the code like this.

.
.
.
OracleConnection test = new OracleConnection();
.
.
.

        using (OracleConnection test = new OracleConnection("Data Source=; User ID=; Password=")) // <-- SQLconnection here
        {

            test.Open(); // <-- Only Open connection

            using (OracleDataReader reader = cmd.ExecuteReader())
            {
.
.
.
            }
         }
Sign up to request clarification or add additional context in comments.

1 Comment

Work's like a charm :) Thank you!

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.