bonjour
j'ai du mal a afficher le resultat d'une requete 'count' sql dans un Label!!
quelqu'un a des idees?????
merci
bonjour
j'ai du mal a afficher le resultat d'une requete 'count' sql dans un Label!!
quelqu'un a des idees?????
merci
Si tu utilise un datareader pour executer ta requete tu peux utiliser le code suivant:
te permettra d'afficher le resultat de ta requete dans ton label.
Code : Sélectionner tout - Visualiser dans une fenêtre à part label.text = datareader.GetString(0)
j'ai un message d'erreur genre:
Tentative non valide de lecture lorsque aucune donnée n'est présente.
voici mon code:
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6 tConnection = new SqlConnection(Global.ConnexionString); tConnection.Open(); sSQL = "select count(Actif) from tbl_Admin where Actif='0'" ; tCommand = new SqlCommand(sSQL, tConnection); tDataReader=tCommand.ExecuteReader(); lblCountIdentif.Text = tDataReader.GetString(0);
bonjour
je suis toujour bloqué sur cette fonction, avec mon code j'ai mon Label affiche -1!!!
merci
Il semblerais que le PB vienne du fait que ta requête ne retourne aucun résultat, non ?
Sinon tu peux utiliser ExecuteScalar() plutot que ExecuteReader() sur ton objet Command
merci pour tout
j'ai trouvé la reponse, voila tous le code:
SqlConnection CountIdentif_Connection;
SqlCommand CountIdentif_Command;
SqlDataReader CountIdentif_DataReader;
string CountIdentif_SQL;
CountIdentif_Connection = new SqlConnection(Global.ConnexionString);
CountIdentif_Connection.Open();
CountIdentif_SQL = "select count(colonne) from tbl where ......" ;
CountIdentif_Command = new SqlCommand(CountIdentif_SQL, CountIdentif_Connection);
CountIdentif_DataReader = CountIdentif_Command.ExecuteReader();
if (CountIdentif_DataReader.Read ())
{
lblCountIdentif.Text=CountIdentif_DataReader.GetValue(0).ToString();
}
CountIdentif_DataReader.Close();
CountIdentif_Connection.Close();
Ton code est incomplet tu dois d'abord tester si t'a requete retourne un resultat puis lancer la lecture du datareader et enfin tu ferme tes objets
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14 tConnection = new SqlConnection(Global.ConnexionString); tConnection.Open(); sSQL = "select count(Actif) from tbl_Admin where Actif='0'" ; tCommand = new SqlCommand(sSQL, tConnection); tDataReader=tCommand.ExecuteReader(); If (tDataReader.HasRows()) { tDataReader.Read(); lblCountIdentif.Text = tDataReader.GetString(0); } tDataReader.Close(); tConnection.Close();
Partager