C# - ExecuteNonQuery requires an open and available Connection. The connection's current state is closed
36,636
Solution 1
you forgot to open the connection,
vcon.Open();
vcom.ExecuteNonQuery();
but remember to use
-
using
statement -- to properly dispose objects -
try-catch
block -- to properly catch exceptions (exception handling)
UPDATE 1
string connStr = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\SB18\Documents\Visual Studio 2010\Projects\AzureSecureStore\AzureSecureStore\AzcureSecureStore Database.accdb; Persist Security Info=False;";
string query = "INSERT INTO Client VALUES(@col1,@col2,@col3,@col4,@col5,@col6)";
using (OleDbConnection conn = new OleDbConnection(connStr))
{
using (OleDbCommand comm = new OleDbCommand())
{
comm.Connection = conn;
comm.CommandText = query;
comm.CommandType = CommandType.Text;
comm.Parameters.AddWithValue("@col1", textBox1.Text);
comm.Parameters.AddWithValue("@col2", textBox2.Text);
comm.Parameters.AddWithValue("@col3", int.Parse(textBox3.Text));
comm.Parameters.AddWithValue("@col4", int.Parse(textBox4.Text));
comm.Parameters.AddWithValue("@col5", textBox9.Text);
comm.Parameters.AddWithValue("@col6", int.Parse(textBox10.Text));
try
{
conn.Open();
comm.ExecuteNonQuery();
MessageBox.Show("Data stored successfully");
}
catch(OleDbException e)
{
MessageBox.Show(e.ToString());
}
}
}
Solution 2
For connect with database you need to open connection so use
vcon.Open();
and then
vcom.ExecuteNonQuery();
Author by
user1971823
Updated on July 09, 2022Comments
-
user1971823 almost 2 years
am new to C#. Please assist!
I keep having the following error: "ExecuteNonQuery requires an open and available Connection. The connection's current state is closed." I am not able to insert into my database as well.
Below is my code:
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using System.Data.OleDb; namespace AzureSecureStore { public partial class Client : Form { OleDbConnection vcon = new OleDbConnection(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\SB18\Documents\Visual Studio 2010\Projects\AzureSecureStore\AzureSecureStore\AzcureSecureStore Database.accdb; Persist Security Info=False;"); //OleDbConnection vcon = new OleDbConnection(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\SB18\Documents\Visual Studio 2010\Projects\AzureSecureStore\AzureSecureStore\AzcureSecureStore Database.accdb"); public Client() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { } private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e) { } private void button5_Click(object sender, EventArgs e) { } private void button4_Click(object sender, EventArgs e) { } private void Client_Load(object sender, EventArgs e) { // TODO: This line of code loads data into the 'azcureSecureStore_DatabaseDataSet5.Client' table. You can move, or remove it, as needed. this.clientTableAdapter.Fill(this.azcureSecureStore_DatabaseDataSet5.Client); // TODO: This line of code loads data into the 'azcureSecureStore_DatabaseDataSet2.Client' table. You can move, or remove it, as needed. //this.clientTableAdapter.Fill(this.azcureSecureStore_DatabaseDataSet2.Client); } private void button2_Click(object sender, EventArgs e) { string ab = string.Format("insert into Client values({0}, '{1}', '{2}', {3}, {4}, '{5}')", textBox1.Text, textBox2.Text, int.Parse(textBox3.Text), int.Parse(textBox4.Text), textBox9.Text, int.Parse(textBox10.Text)); OleDbCommand vcom = new OleDbCommand(ab, vcon); vcom.ExecuteNonQuery(); MessageBox.Show("Data stored successfully"); vcom.Dispose(); } private void textBox1_TextChanged(object sender, EventArgs e) { } } }
-
user1971823 over 11 yearsHi, i did what you mentioned but i get the following error: "OleDbException was unhandled." No value given for one or more parameters.What should i do?
-
user1971823 over 11 yearsHi, i did what you mentioned but i get the following error: "OleDbException was unhandled." No value given for one or more parameters.What should i do?
-
user1971823 over 11 yearsUm, i did the changes. But the data still won't get into my database. Not exactly an error but i get the following popup when i click insert: "Systems.Windows.Forms.MouseEventArgs"
-
John Woo over 11 yearsthe code can run on its own. try putting it in a method an call that method.
-
Damien_The_Unbeliever over 11 years@user1971823 - sounds like you've got the wrong variable having
ToString()
called on it in thecatch
clause - it should be the reference to the exception two lines above. You probably had to change that reference since the sample in this answer usese
, bute
is the second argument to your function.