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.
try calling connection.Open before executing your command
(yes, i know lol right)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With