Error

omranebrahim - 24 avril 2013 à 23:05
 MINDONGO - 29 avril 2013 à 11:23
bonsoir
lors d'exécution de mon application c# à connexion base donnée access. l'erreur suivant se produit:
""la connexion n'est pas fermée. l'état actuel de la connexion est ouvert access""
voila mon code:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.OleDb;

namespace exxx
{
public partial class Form1 : Form
{

OleDbConnection con=new OleDbConnection(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\omrani\Documents\emplyee.accdb");
OleDbDataAdapter ad= new OleDbDataAdapter();
DataSet ds=new DataSet();
public Form1()
{
InitializeComponent();
}

private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
{

}

private void button1_Click(object sender, EventArgs e)
{
try
{
ad.InsertCommand=new OleDbCommand ("INSERT INTO Personne VALUES (@ID,@Nom)",con);
ad.InsertCommand.Parameters.Add("@ID",OleDbType.VarChar).Value=textBox1.Text.ToString();
ad.InsertCommand.Parameters.Add("@Nom",OleDbType.VarChar).Value=textBox2.Text.ToString();
con.Open();
ad.InsertCommand.ExecuteNonQuery();
con.Close();
MessageBox.Show("SAVEDDDDDDDD");
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}

private void button4_Click(object sender, EventArgs e)
{
try
{
ad.SelectCommand = new OleDbCommand("SELECT * FROM Personne", con);
ds.Clear();
ad.Fill(ds);
dataGridView1.DataSource = ds.Tables[0];
con.Open();
ad.SelectCommand.ExecuteNonQuery();
con.Close();
MessageBox.Show("showwwwwwwwwwwww");
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}

private void button2_Click(object sender, EventArgs e)
{
try
{
ad.UpdateCommand = new OleDbCommand("UPDATE Personne SET Nom='" + textBox2.Text + "' WHERE (ID='" + textBox1 + "')", con);
con.Open();
ad.UpdateCommand.ExecuteNonQuery();
con.Close();
MessageBox.Show("Updated");

}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}

private void button3_Click(object sender, EventArgs e)
{
try
{
ad.DeleteCommand = new OleDbCommand("DELETE FROM Personne WHERE(ID='" + textBox1.Text + "')", con);
con.Open();
ad.DeleteCommand.ExecuteNonQuery();

con.Close();
MessageBox.Show("DELETED");

}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}

}

private void groupBox1_Enter(object sender, EventArgs e)
{

}

private void label1_Click(object sender, EventArgs e)
{

}

private void label2_Click(object sender, EventArgs e)
{

}

private void textBox1_TextChanged(object sender, EventArgs e)
{

}

private void textBox2_TextChanged(object sender, EventArgs e)
{

}
}
}
Merci

5 réponses

salut,
tu peut essayé de faire ça
//con.Open();


donc quelque chose de ce genre si ça marche ok si non revien encore avec le nouveau msg
private void button1_Click(object sender, EventArgs e)
{
try
{
ad.InsertCommand=new OleDbCommand ("INSERT INTO Personne VALUES (@ID,@Nom)",con);
ad.InsertCommand.Parameters.Add("@ID",OleDbType.VarChar).Value=textBox1.Text.ToString();
ad.InsertCommand.Parameters.Add("@Nom",OleDbType.VarChar).Value=textBox2.Text.ToString();
//con.Open();
ad.InsertCommand.ExecuteNonQuery();
con.Close();
MessageBox.Show("SAVEDDDDDDDD");
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}

private void button4_Click(object sender, EventArgs e)
{
try
{
ad.SelectCommand = new OleDbCommand("SELECT * FROM Personne", con);
ds.Clear();
ad.Fill(ds);
dataGridView1.DataSource = ds.Tables[0];
//con.Open();
ad.SelectCommand.ExecuteNonQuery();
con.Close();
MessageBox.Show("showwwwwwwwwwwww");
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}

private void button2_Click(object sender, EventArgs e)
{
try
{
ad.UpdateCommand = new OleDbCommand("UPDATE Personne SET Nom='" + textBox2.Text + "' WHERE (ID='" + textBox1 + "')", con);

ad.UpdateCommand.ExecuteNonQuery();
con.Close();
MessageBox.Show("Updated");

}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}

private void button3_Click(object sender, EventArgs e)
{
try
{
ad.DeleteCommand = new OleDbCommand("DELETE FROM Personne WHERE(ID='" + textBox1.Text + "')", con);

ad.DeleteCommand.ExecuteNonQuery();

con.Close();
MessageBox.Show("DELETED");

}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}

} 



Soyons Toujours à la disposition des autres
0
omranebrahim
26 avril 2013 à 14:17
bonjour
oui le code marche , mais ou niveau "UPDATE " il n y'a pas de changement
tjrs le meme (bouton update ne fonctionne pas")
0
svp montre juste ces codes de votre button update


Soyons Toujours à la disposition des autres
0
omranebrahim
27 avril 2013 à 10:39
private void button2_Click(object sender, EventArgs e)
{
try
{
ad.UpdateCommand = new OleDbCommand("UPDATE Personne SET Nom='" + textBox2.Text + "' WHERE (ID='" + textBox1 + "')", con);

ad.UpdateCommand.ExecuteNonQuery();
con.Close();
MessageBox.Show("Updated");

}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
0

Vous n’avez pas trouvé la réponse que vous recherchez ?

Posez votre question
private void button2_Click(object sender, EventArgs e)
{
try
{//dans ton message d erreur c est toujours le problème de la connexion?
//si non regarde ton :
ad.UpdateCommand = new OleDbCommand("UPDATE Personne SET Nom='" + textBox2.Text + "' WHERE (ID='" + textBox1 + "')", con);
//*******************************************************************************************
("UPDATE Personne SET Nom='" + textBox2.Text + "' WHERE (ID='" + textBox1.text + "')", con);//
//il manque au moins le .tetx dans textbox1                                                 //
//*******************************************************************************************
}



Soyons Toujours à la disposition des autres
0
Rejoignez-nous