Requete sql

nouna1551 Messages postés 10 Date d'inscription dimanche 4 janvier 2009 Statut Membre Dernière intervention 21 mars 2010 - 23 mai 2009 à 22:00
 elmeksaoui1 - 5 août 2015 à 16:26
bonjour tt le monde,j'ai un peit probleme,je suis entraine de crrer une form dans laquelle j'ai une combobox et une datagridview la basse de donnes contient quatre table ja doit choisir un Athlete dans la combobx et les cordonnes d'entrainement vont etre affiche dans la datagridview
voila le code avec lme quelle j'ai travaillé mais il me donne une erreur
**************************
Imports System.Data.SqlClient
Public Class Form1
Dim con As SqlConnection
Dim cmd1, cmd2 As SqlCommand
Dim da, da2 As SqlDataAdapter
Dim ds As New DataSet
Dim dt, dt1 As DataTable
'*********************

Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged

End Sub

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
con = New SqlConnection
con.ConnectionString = "Data Source=poste-92frh1132\SQLEXPRESS;integrated security=sspi;initial catalog=Athlete"
cmd1 = New SqlCommand
cmd1.CommandText = "select * from athlete"
con.CreateCommand()
con.Open()
cmd1.Connection = con
da = New SqlDataAdapter(cmd1)
da.Fill(ds, "Athlete")
dt = ds.Tables("athlete")
ComboBox1.DataSource = dt
ComboBox1.DisplayMember = "NomAthlete"
ComboBox1.ValueMember = "NumLicence"
cmd2 = New SqlCommand
cmd2.CommandText = "select * from Plan_Entrainement where NumLicence=" & ComboBox1.SelectedValue
con.CreateCommand()
cmd2.Connection = con
da2 = New SqlDataAdapter(cmd2)
da2.Fill(ds, "Plan_Entrainement")
dt1 = ds.Tables("Plan_Entrainement")
DataGridView1.DataSource = dt1
con.Close()
End Sub
End Class


je suis vraiment nesion de votre aide et merci d'avance

noun@

8 réponses

Mayzz Messages postés 2813 Date d'inscription mardi 15 avril 2003 Statut Membre Dernière intervention 2 juin 2020 28
23 mai 2009 à 22:14
Bonsoir,

Quel type d'erreur rencontre tu ?

ComboBox1.DataSource = dt
ComboBox1.DisplayMember = "NomAthlete"
ComboBox1.ValueMember = "NumLicence"
cmd2 = New SqlCommand
'Le comboBox est rempli mais comme l'événement se déroule dans le Form_Load, il ne doit pas avoir été initialisé du coup place ces lignes
If ComboBox1.Items.Count > 0 then
    ComboBox1.SelectedIndex = 0
Else
    MsgBox ("Table vide !")
    Return
End If
cmd2.CommandText = "select * from Plan_Entrainement where NumLicence=" & ComboBox1.SelectedValue 

con.CreateCommand()

Commence déja par cela puis en suite si ca ne fonctionne toujours pas dis en plus su ton message d'erreur..

@+
0
nouna1551 Messages postés 10 Date d'inscription dimanche 4 janvier 2009 Statut Membre Dernière intervention 21 mars 2010
23 mai 2009 à 22:44
bon monsieur voila l'erreur[ L'opérateur '&' n'est pas défini pour chaîne "select * from Plan_Entrainement " et type 'DataRowView'.]
le debogage commene dans l'instruction
cmd2.CommandText =

"select * from Plan_Entrainement where Plan_Entrainement.NumLicence='" &
Me.ComboBox1.SelectedItem &
"'"

noun@
0
Mayzz Messages postés 2813 Date d'inscription mardi 15 avril 2003 Statut Membre Dernière intervention 2 juin 2020 28
23 mai 2009 à 22:58
En ce qui concerne ta seconde requête, ça à l'air bon, il me faut plus d'infos si tu veux un coup de main.

Donne moi l'erreur exacte que te donne Visual Studio en faisant un copié/collé.

Merci.
0
omarlward Messages postés 5 Date d'inscription samedi 4 novembre 2006 Statut Membre Dernière intervention 25 mai 2009
24 mai 2009 à 16:49
c'est normale car aucun élément n'est selectionné dans le combo N° 1.

donc tu derva mettre la deuxieme requéte dans l'evenement seletedindex_changed du combo N°1:

Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged

If ComboBox1.SelectedIndex > -1 then
      cmd2 = New SqlCommand
      cmd2.CommandText =   "select       *          from          Plan_Entrainement    where  NumLicence=" & ComboBox1.SelectedValue
      con.CreateCommand()
      cmd2.Connection = con
      da2 = New SqlDataAdapter(cmd2)
      da2.Fill(ds, "Plan_Entrainement")
      dt1 = ds.Tables("Plan_Entrainement")
      DataGridView1.DataSource = dt1
End If

End Sub

ennaim omar
0

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

Posez votre question
Mayzz Messages postés 2813 Date d'inscription mardi 15 avril 2003 Statut Membre Dernière intervention 2 juin 2020 28
24 mai 2009 à 18:01
j'ai déja posté ce que tu viens de dire plus haut,

de plus ta méthode ne permet pas la sélection d'un élément par défaut au démarrage du formulaire...
0
nouna1551 Messages postés 10 Date d'inscription dimanche 4 janvier 2009 Statut Membre Dernière intervention 21 mars 2010
24 mai 2009 à 20:08
dsl monsieur mais le code que vous m'avez donner ne marche pas je l'ai deja utilisé .l'erreur c'est
[L'opérateur '&' n'est pas défini pour chaîne "select * from Plan_Entrainement " et type 'DataRowView'.]

noun@
0
Mayzz Messages postés 2813 Date d'inscription mardi 15 avril 2003 Statut Membre Dernière intervention 2 juin 2020 28
24 mai 2009 à 20:46
Essaye comme ça :

cmd2.CommandText = "select * from Plan_Entrainement where Plan_Entrainement.NumLicence='" & cstr(ComboBox1.SelectedItem) & "'"

Si ce ne marche pas post moi l'erreur en faisant un copié collé de celle ci
0
Bonjour,
tester ce code ca va repondre a ton porbleme.

 Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As  

If IsNumeric(ComboBox1.SelectedValue) then 

      cmd2 = New SqlCommand 
      cmd2.CommandText =   "select * from          Plan_Entrainement    where  NumLicence=" & ComboBox1.SelectedValue 
      con.CreateCommand() 
      cmd2.Connection = con 
      da2 = New SqlDataAdapter(cmd2) 
      da2.Fill(ds, "Plan_Entrainement") 
      dt1 = ds.Tables("Plan_Entrainement") 
      DataGridView1.DataSource = dt1 
End If 

End Sub  

ca pour aider les genes
0
Rejoignez-nous