Bonjour,
Mr NHenry c'est le code sans controle de doublons et ave c remplissage du dt
Imports System
Imports System.Data
Imports System.Data.SqlClient
Public Class ajout_agent
Dim con As String = ""
Dim connsql As New SqlConnection(con)
Dim i As Integer
Dim cmd As New SqlCommand
Dim da As New SqlDataAdapter
Dim ds As New DataSet
Dim dt As New DataTable
Private Sub load_Form()
matricule.Text = ""
mdp.Text = ""
nomag.Text = ""
preag.Text = ""
connsql.Open()
Try
cmd = connsql.CreateCommand
cmd.CommandText = "SELECT * FROM Agent"
da.SelectCommand = cmd
da.Fill(ds, "Agent")
Catch ex As Exception
MsgBox("ERREUR: " & ex.Source & ": " & ex.Message, MsgBoxStyle.OkOnly, "ERREUR DE CONNECTION ")
End Try
End Sub
Private Sub Bouttonajout_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Buttonajout.Click
Dim con As String = "
Dim connsql As New SqlConnection(con)
cmd.CommandText = "select * from agent"
If matricule.Text "" Or nomag.Text "" Or preag.Text = "" Or poste.Text = "" Or type.Text = "" Or service.Text = "" Then
MsgBox("certaines cases sont vides ", MsgBoxStyle.OkOnly)
Else
If MsgBox("etes vous sur d'enregistrer l'agent de matricule: " & matricule.Text & " ?", MsgBoxStyle.YesNo, "L'ajout est confirmé") Then
'vide
Else
Try
connsql.Open()
If (dt.Rows.Count > 0) And "(select* from Agent where numero_agent='" & (numero.Text) & "')" Then
MsgBox("l'agent de matricule " & (matricule.Text) & " existe ", MsgBoxStyle.OkOnly)
Else
cmd = connsql.CreateCommand
cmd.CommandText = "INSERT INTO Agent(Matricule_agent, MDP_agent, Nom_agent,Prenom_agent,ID_type,Code_postal,ID_service) VALUES('" & (matricule.Text) & "','" & (mdp.Text) & "','" & (nomag.Text) & "','" & (preag.Text) & "','" & type.GetItemText(type.SelectedItem) & "','" & poste.GetItemText(poste.SelectedItem) & "','" & service.GetItemText(service.SelectedItem) & "')"
i = cmd.ExecuteReader.RecordsAffected()
If i > 0 Then
MsgBox("l'agent de matricule " & (matricule.Text) & " est ajouté avec succsés", MsgBoxStyle.OkOnly, "Message :")
Else
MsgBox("l'agent de matricule " & (matricule.Text) & " n'est pas ajouté ", MsgBoxStyle.OkOnly, "Message :")
End If
load_Form()
connsql.Close()
End If
Catch ex As Exception
MsgBox("ERREUR: " & ex.Source & ": " & ex.Message, MsgBoxStyle.OkOnly, "ERREUR DE CONNECTION !!")
End Try
End If
End If
End Sub