Gestion combobox

hosmano Messages postés 1 Date d'inscription mercredi 25 novembre 2015 Statut Membre Dernière intervention 18 août 2017 - 18 août 2017 à 14:07
 hosmano - 21 août 2017 à 00:29
bonjour à tous, je veux savoir un code qui permet d'ajouter ,supprimer,modifer et afficher les données d'un combobox (je travaille avec une base de données access),merci pour votre collaboration

6 réponses

Whismeril Messages postés 19028 Date d'inscription mardi 11 mars 2003 Statut Non membre Dernière intervention 24 avril 2024 656
19 août 2017 à 23:26
Bonsoir

ça dépend de comment tu affiches les données de la base Access.

0
j'utilise Access comme un SGBD
0
Whismeril Messages postés 19028 Date d'inscription mardi 11 mars 2003 Statut Non membre Dernière intervention 24 avril 2024 656
20 août 2017 à 10:06
Ça tu l'as déjà écrit.
0
oui c'est deja ecrit j'ai pas fait attention, j'ai fais ce formulaire mais il ya des bugs que je n'arrive pas a trouver la solution tel que :
"object reference not set to an instance of an oject"
0

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

Posez votre question
Whismeril Messages postés 19028 Date d'inscription mardi 11 mars 2003 Statut Non membre Dernière intervention 24 avril 2024 656
20 août 2017 à 23:59
Ma boule de cristal est en panne.
Donc si tu veux que l'on puisse t'aider, il faut poster ton code.
Et pour nous en faciliter la lecture utilise la coloration syntaxique, voir procédure ici.
0

Imports System.Data.OleDb

Public Class Form1
'global declaration

Dim conString As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Users\houssem\Desktop\notice\soccerDB.mdb;"
Dim con As OleDb.OleDbConnection = New OleDb.OleDbConnection(conString)
Dim adapter As OleDb.OleDbDataAdapter
Dim cmd As OleDb.OleDbCommand
Dim dt As DataTable = New DataTable()

Private Function HasExist(ByVal SQL As String) As Boolean
Try
con.Open()
cmd = New OleDb.OleDbCommand(SQL, con)
Dim count As Int32 = Convert.ToInt32(cmd.ExecuteScalar())
con.Close()
If (count >= 1) Then
MsgBox("Value Has Exist")
Return True
Else
Return False
End If
cmd.dispose()
' con.Close()
Catch ex As Exception
MsgBox(ex.Message)
con.Close()
Return False
' Retreive()
End Try
End Function

Private Sub LoadComboBox()
ComboBox1.Items.clear()
con.Open()
Dim cm As New OleDbCommand("SELECT * FROM soccerTB", con)
Dim dr As OleDbDataReader = cm.ExecuteReader
While dr.Read
ComboBox1.Items.Add(dr.GetString(1))
End While
dr.Close()
con.Close()
End Sub


Private Sub populate(ByVal name As String)
'sql stmt
Dim sql As String = "INSERT INTO soccerTB(P_NAME) VALUES (@NAMES)"
cmd = New OleDb.OleDbCommand(sql, con)
'add parameters
cmd.Parameters.AddWithValue("@NAMES", name)
'open connection and insert
Try
If HasExist("SELECT COUNT(*) FROM soccerTB WHERE P_NAME='" & trim(nameTxt.Text) & "'") = True Then
Exit Sub
End If
con.Open()
'execute statement
If cmd.ExecuteNonQuery() > 0 Then
MsgBox("successfully entred data")
End If
'clear text
nameTxt.Text = ""
con.Close()
'Retreive()
Catch ex As Exception
MsgBox(ex.Message)
con.Close()
End Try
End Sub
Private Sub addBtn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles addBtn.Click
'populate()
populate(nameTxt.Text)
LoadComboBox()
End Sub
'retreive from db.

Private Overloads Sub Update(ByVal value As String, ByVal name As String)
Dim sql As String = "UPDATE soccerTB SET P_NAME='" + value + "'WHERE P_NAME='" + name + "'"
cmd = New OleDb.OleDbCommand
'execute
Try
con.Open()
adapter.UpdateCommand = con.CreateCommand()
adapter.UpdateCommand.CommandText = sql
If adapter.UpdateCommand.ExecuteNonQuery() > 0 Then
MsgBox("successfuly updated")
End If
con.Close()
Catch ex As Exception
MsgBox(ex.Message)
con.Close()
' Retreive()
End Try
End Sub

Private Sub updateBtn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles updateBtn.Click
Update(nameTxt.Text, ComboBox1.SelectedItem)
LoadComboBox()
End Sub


Public Sub delete(ByVal name As String)
Dim sql As String = "DELETE FROM soccerTB WHERE P_NAME='" + name + "'"
cmd = New OleDb.OleDbCommand(sql, con)
'execute
Try
con.Open()
adapter.DeleteCommand = con.CreateCommand()
adapter.DeleteCommand.CommandText = sql
If MessageBox.Show("etes vous sur ?", "DELETE", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning) = DialogResult.OK Then
If (adapter.DeleteCommand.ExecuteNonQuery > 0) Then
nameTxt.Text = ""
MsgBox("successfully Deleted")
End If
End If
'close connection
con.Close()
' Retreive()
Catch ex As Exception
MsgBox(ex.Message)
con.Close()
End Try
End Sub
Private Sub deleteBtn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles deleteBtn.Click
delete(ComboBox1.SelectedItem)
LoadComboBox()
End Sub

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
LoadComboBox()
End Sub

Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged
nameTxt.Text = ComboBox1.SelectedItem
End Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles sortirBtn.Click
Me.Close()
End Sub

End Class

0
Rejoignez-nous