COMMENT SPECIFIER UN L'ERREUR EN CAS DU MESSAGE "la propriété Connection n'a pas

Résolu
MINDONGO - 21 août 2012 à 15:35
 MINDONGO - 27 août 2012 à 19:03
BONJOUR JUSTE SAVOIR QCK JE PEUT FAIRE QUAND J'EXECUTE Ma REQUETE IL Y A UN MESSAGE QUI S'AFFICHE
COMME CECI "ExecuteScalar : la propriété Connection n'a pas été initialisée."
ET LA LIGNE SUIVANTE EST SOULIGNEE "int temp = System.Convert.ToInt32(Checkuser.ExecuteScalar().ToString());" C POUR CELA QUE SUIS LA POUR CHERCHER QUELQU'UN QUI PEUT M'AIDER
MERCI D'AVANCE
VOICI LES CODES:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Data.SqlClient;
using System.Text;
using System.Configuration;
using System.Data.Sql;
using System.Data.OleDb;
using System.Windows.Forms;


namespace CLIENT
{
public partial class COMPTE_UTILISATEUR : Form
{
OleDbConnection cnn;
OleDbCommand cmd;
DataSet ds = new DataSet();
OleDbDataAdapter da;


public COMPTE_UTILISATEUR()
{
InitializeComponent();
string connectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\TRAVAILMINDONGO\CLIENT\CLIENT\TFCGESTIONMINDONGOKANIKI.accdb";
cnn = new OleDbConnection(connectionString);

}
private void form1_load(object sender, EventArgs e)
{
lecture();

}
void lecture()
{

cmd = new OleDbCommand("SELECT * FROM COMPTE", cnn);
da = new OleDbDataAdapter(cmd);

}
private void buttonconnexion_Click(object sender, EventArgs e)
{

string connectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\TRAVAILMINDONGO\CLIENT\CLIENT\TFCGESTIONMINDONGOKANIKI.accdb";
cnn = new OleDbConnection(connectionString);
cmd=new OleDbCommand (" select count(*) from COMPTE where UTILISATEUR='"+textBoxUTILISAT.Text+"'",cnn );
SqlCommand Checkuser = new SqlCommand();
cnn.Open();
int temp = System.Convert.ToInt32(Checkuser.ExecuteScalar().ToString());
if (temp == 1)
{
cmd = new OleDbCommand(" select PASSE from COMPTE where UTILISATEUR='" + textBoxUTILISAT.Text + "'", cnn);

}
SqlCommand PASS = new SqlCommand();
string PASSE = PASS.ExecuteScalar().ToString();
if (PASSE == textBoxPASSWORD.Text)
{
MENU_GENERALE MGT = new MENU_GENERALE();
MGT.Show();
this.Hide();
SqlCommand scmd = new SqlCommand();
scmd.ExecuteNonQuery();
cnn.Close();
}
else
{
MessageBox.Show("erreur de login");
}

}

}
}













rex1987

2 réponses

krimog Messages postés 1860 Date d'inscription lundi 28 novembre 2005 Statut Membre Dernière intervention 14 février 2015 49
21 août 2012 à 16:00
Salut

Alors on va commencer simplement : pas besoin d'écrire en majuscules pour qu'on te comprenne ou qu'on voie ton message. C'est juste désagréable à lire (ce qui n'est pas la meilleure méthode pour obtenir de l'aide...)

Ensuite, dans les outils, tu as le troisième bouton en partant de la gauche qui te permet d'avoir la coloration syntaxique et de garder l'indentation de ton code.

Ensuite, tu crées ta commande à cette ligne :
cmd = new OleDbCommand (" select count(*) from COMPTE where UTILISATEUR='" + textBoxUTILISAT.Text + "'",cnn );

Mais à la ligne d'après, tu crées une autre commande (qui n'est même plus une OleDbCommand, mais une SqlCommand)
SqlCommand Checkuser = new SqlCommand();

Et c'est cette commande (que tu n'as jamais initialisé) que tu utilises. C'est pour ça que ça ne marche pas.

En remplaçant ta ligne par
int temp = System.Convert.ToInt32(cmd.ExecuteScalar().ToString());

ça devrait déjà mieux marcher.

PS : tu fais la même erreur juste en dessous avec PASS.
PPS : Tu transmets le mot de passe de l'utilisateur que tu recherches à ton application, puis tu le compares. C'est beaucoup plus propre de vérifier directement s'il existe la ligne avec le bon mot de passe :
cmd new OleDbCommand("SELECT COUNT(*) FROM compte WHERE utilisateur '" + textBoxUTILISAT.Text + "' AND passe = '" + textBoxPASSWORD.Text + "'", cnn);

PPPS : N'oublie pas de fermer la commande également.

Krimog : while (!(succeed = try())) ;
- Nous ne sommes pas des décodeurs ambulants. Le style SMS est prohibé. -
3
Bravo! //Krimog
maintenant ma connexion s'authentifie correctement
et au cas contraire il y a le msg d'erreur
car j'ai reuci avec les codes suivants
grâce a vous (Krimog )

private void buttonconnexion_Click(object sender, EventArgs e)
{
try
{
cmd new OleDbCommand("SELECT COUNT(*) FROM compte WHERE utilisateur '" + textBoxUTILISAT.Text + "' AND passe = '" + textBoxPASSWORD.Text + "'", cnn);
cnn.Open();
int temp = System.Convert.ToInt32(cmd.ExecuteScalar().ToString());
if (temp == 1)
{
cmd = new OleDbCommand(" select * from COMPTE where UTILISATEUR='" + textBoxUTILISAT.Text + "' AND PASSE='" + textBoxUTILISAT.Text + "' ", cnn);
{
//Affichage de ma fenêtre suivante
}
}
else
{
MessageBox.Show("Ecrivez corretement vote nom et le mot de passe", "Erreur de Compte Utilisateur", MessageBoxButtons.OK, MessageBoxIcon.Stop);

}


REX MINDONGO KANIKI THE KING
0
Rejoignez-nous