9

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.

4
  • I have looked twice but I can't see any con.Open call
    – Steve
    Commented Jul 9, 2014 at 19:24
  • Where do you open the connection? Commented Jul 9, 2014 at 19:24
  • 2
    However, you have a bigger problem. It is called Sql Injection
    – Steve
    Commented Jul 9, 2014 at 19:26
  • 1
    @Steve There's no problem if you never open the connection. :) Commented Jul 9, 2014 at 19:27

1 Answer 1

20

try calling connection.Open before executing your command

(yes, i know lol right)

3
  • 2
    Oh god. I had that before and got the same error, so I removed it to see if it made a difference, and then I changed some stuff, and now apparently it works. Thanks! Commented Jul 9, 2014 at 19:52
  • 3
    i hope you face palmed ;p Commented Jul 9, 2014 at 19:52
  • 4
    the moment when you see this answer and realize you already upvoted it sometime before and totally forgot. (double facepalm)
    – emregon
    Commented Dec 29, 2015 at 8:18

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.