Erreur de la base de donnée SQL au lancement du C Sharp

Résolu
momosan77 Messages postés 47 Date d'inscription lundi 17 mai 2010 Statut Membre Dernière intervention 19 décembre 2011 - 5 avril 2011 à 08:42
momosan77 Messages postés 47 Date d'inscription lundi 17 mai 2010 Statut Membre Dernière intervention 19 décembre 2011 - 5 avril 2011 à 15:06
Bonjour, actuellement en BTS d'informatique de gestion je dois obligatoirement développer différent application. L'application suivante est une gestion de stock de voiture ainsi que les réparation. Le problème rencontré est le plantage du programme lors du lancement de la classe réparer par une base SQL sur le code ci dessous.

Si quelqu'un peu m'aider si vous plait, Merci d'avance.


public partial class Reparer : Form
{
public static MySqlConnection obj;
public Reparer()
{

InitializeComponent();

}

void ReparerLoad(object sender, EventArgs e)
{
//ouverture base de donnee
string chcnx="database=voiture; Data Source=localhost;User id=root;Password=";
obj=new MySqlConnection(chcnx);
obj.Open();

MySqlCommand cmdSelect=MainForm.obj.CreateCommand();
cmdSelect.CommandText="select * from reparation";
MySqlDataReader resultat=cmdSelect.ExecuteReader();
while (resultat.Read())
{
domainUpDown1.Items.Add(resultat.GetValue(1));
}
resultat.Close();

MySqlCommand cmd1Select=MainForm.obj.CreateCommand();
cmd1Select.CommandText="select motif_reparation from reparation";
MySqlDataReader resultat1=cmd1Select.ExecuteReader();
while (resultat1.Read())
{
listBox1.Items.Add(resultat1.GetValue(1));
}
resultat.Close();

MySqlCommand cmd2Select=MainForm.obj.CreateCommand();
cmd2Select.CommandText="select piece from reparation";
MySqlDataReader resultat2=cmd2Select.ExecuteReader();
while (resultat2.Read())
{
listBox2.Items.Add(resultat2.GetValue(2));
}
resultat.Close();

3 réponses

momosan77 Messages postés 47 Date d'inscription lundi 17 mai 2010 Statut Membre Dernière intervention 19 décembre 2011
5 avril 2011 à 08:52
Ainsi il affiche l'erreur qui suit
3
cs_mo3ez Messages postés 15 Date d'inscription vendredi 1 avril 2011 Statut Membre Dernière intervention 7 avril 2011 4
5 avril 2011 à 14:43
C'est normal : tu lis resultat2 sans avoir fermé resultat1 : relis ton code, tu refermes resultat plusieurs fois :
resultat.Close();

Alors que tu devrais fermes resultat1 et resultat2 Comme ceci
public partial class Reparer : Form
{
public static MySqlConnection obj;
public Reparer()
{

InitializeComponent();

}

void ReparerLoad(object sender, EventArgs e)
{
//ouverture base de donnee
string chcnx="database=voiture; Data Source=localhost;User id=root;Password=";
obj=new MySqlConnection(chcnx);
obj.Open();

MySqlCommand cmdSelect=MainForm.obj.CreateCommand();
cmdSelect.CommandText="select * from reparation";
MySqlDataReader resultat=cmdSelect.ExecuteReader();
while (resultat.Read())
{
domainUpDown1.Items.Add(resultat.GetValue(1));
}
resultat.Close();

MySqlCommand cmd1Select=MainForm.obj.CreateCommand();
cmd1Select.CommandText="select motif_reparation from reparation";
MySqlDataReader resultat1=cmd1Select.ExecuteReader();
while (resultat1.Read())
{
listBox1.Items.Add(resultat1.GetValue(1));
}
resultat1.Close();

MySqlCommand cmd2Select=MainForm.obj.CreateCommand();
cmd2Select.CommandText="select piece from reparation";
MySqlDataReader resultat2=cmd2Select.ExecuteReader();
while (resultat2.Read())
{
listBox2.Items.Add(resultat2.GetValue(2));
}
resultat2.Close();
3
momosan77 Messages postés 47 Date d'inscription lundi 17 mai 2010 Statut Membre Dernière intervention 19 décembre 2011
5 avril 2011 à 15:06
Merci beaucoupcela fonctionne :D
3
Rejoignez-nous