Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting "Invalid Operation. The Connection is closed" error while trying to update oracle from visual c# program

Tags:

c#

oracle

I've got a visual c# program running, but I'm getting a connection is closed error whenever I try to update. Here's what my code looks like:

private void Update()
{
    try
    {
        String OneMachineScheduleOrder = "";
        String series = "";
        String oven = "";
        String battery = "";
        int x,y;

        var sortedTextboxes = panel1.Controls
                .OfType<TextBox>() // get all textboxes controls
                .OrderBy(ctrl => ctrl.TabIndex); // order by TabIndex
        foreach (TextBox txt in sortedTextboxes)
        {
            //Console.WriteLine(Convert.ToInt32(txt.TabIndex/2+1) + ": " + txt.Text);
            OneMachineScheduleOrder = (txt.TabIndex / 2 + 1).ToString();
            series = txt.Text.Substring(0, 1);
            oven = txt.Text.Substring(1, 2);
            battery = txt.Text.Substring(4).Trim();
            if (Char.IsLetter(series[0]) && int.TryParse(oven, out y) && int.TryParse(battery, out x) && txt.Text[3].Equals('/'))
            {
                using (OracleConnection con = new OracleConnection(connectString))
                {
                    OracleCommand cmd = connection.CreateCommand();
                    cmd.CommandType = CommandType.Text;
                    cmd.CommandText = "update Oven_Master set SERIES = '" + series + "', OVEN = '" + oven + "', BATTERY = '" + battery + "' where ONE_MACHINE_SCHEDULE_ORDER = '" + OneMachineScheduleOrder + "'";
                    cmd.Connection = con;
                    cmd.ExecuteNonQuery();
                    Console.WriteLine(cmd.CommandText);
                    con.Close();
                }
            }
            else { MessageBox.Show("Number: " + OneMachineScheduleOrder + " Is Invalid!"); }                           
        }
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.ToString());
    }
    finally
    {
        connection.Close();
    }
}

Basically, I've got a bunch of textboxes on the form that are filled in in the format A01/01. I'm sorting the textboxes into a variable, and then for each textbox, I parse out the relevent data (OneMachineScheduleOrder, series, oven, and battery). If the data is in the right format, I use an oracleConnection with a connectionstring that is global (and I checked with the debugger that it has the correct value) to create an execute and OracleCommand. Otherwise, alert the user that the data is in the wrong format.

However, I'm getting an error that the connection is open. I tried putting a breakpoint on that line, and I'm getting that con = OracleConnection, so I can see that there is a connection. No idea where to go from here.

like image 631
Alex Kibler Avatar asked Jul 09 '14 19:07

Alex Kibler


1 Answers

try calling connection.Open before executing your command

(yes, i know lol right)

like image 95
John Nicholas Avatar answered Oct 01 '22 06:10

John Nicholas