Faire un ajout a une base

cs_colby Messages postés 126 Date d'inscription mardi 10 mai 2005 Statut Membre Dernière intervention 14 novembre 2022 - 9 déc. 2007 à 15:03
cs_colby Messages postés 126 Date d'inscription mardi 10 mai 2005 Statut Membre Dernière intervention 14 novembre 2022 - 9 déc. 2007 à 15:21
Bonjour
debutant je voudrais ajouter une donnée a une base access, mais je reçois " exception NullReferenceException
voila ce que j'ai ecris.Voulez vous m'aider à trouver l'erreur


'ouverture de la connection(à partir du répertoire de l'application)
Connx = "PROVIDER=Microsoft.Jet.OLEDB.4.0;Data Source =" & My.Application.Info.DirectoryPath & "\GP.mdb"

GP = New OleDb.OleDbConnection
GP.ConnectionString = Connx
Requet = "Select* from Employés"
GP.Open()
EmplTA = New OleDb.OleDbDataAdapter(Requet, GP)
EmplTA.Fill(GPDataset, "Employés")

'crátion d'une nouvelle ligne avec les données des textbox
GPDataset.Tables("Employés").NewRow()

DTR("Nom") = TextBox2.Text
DTR("Prénom") = TextBox3.Text
DTR("Sexe") = ComboBox1.Text
DTR("Adresse") = TextBox4.Text
DTR("Ville") = TextBox5.Text
DTR("Codepostal") = TextBox6.Text
DTR("Téléphone") = TextBox7.Text
DTR("PostedeTravail") = TextBox8.Text
DTR("CongéNormalRestant") = TextBox9.Text
DTR("CongéSpécialRestant") = TextBox10.Text
DTR("CongéMaladie") = TextBox11.Text

'ajout de la ligne dans le DataSet
GPDataset.Tables("Employés").Rows.Add(DTR)

Try

'création et exécution du commandbuilder pour mettre à jour le DataAdapter
CmdB = New OleDb.OleDbCommandBuilder(EmplTA)

EmplTA.UpdateCommand = CmdB.GetUpdateCommand()

'mise à jour des données du DataAdapter à partir du commandbuilder
EmplTA.Update(GPDataset, "Employés")
Catch
MsgBox("Une erreur est survenue pendant la mise à jour." & Chr(13) & Chr(10) & _
"L'opération a échoué!", MsgBoxStyle.Critical, "Echec")
End Try

1 réponse

cs_colby Messages postés 126 Date d'inscription mardi 10 mai 2005 Statut Membre Dernière intervention 14 novembre 2022
9 déc. 2007 à 15:21
Salut s'il vous plait ou se trouve l'erreur dans ce code
a l'execution je recois "nullReferenceException" comment le résoudre
meci d'avance

'ouverture de la connection(à partir du répertoire de l'application)
Connx = "PROVIDER=Microsoft.Jet.OLEDB.4.0;Data Source =" & My.Application.Info.DirectoryPath & "\GP.mdb

GP = New OleDb.OleDbConnection
GP.ConnectionString = Connx
Requet = "Select* from Employés"
GP.Open()

EmplTA = New OleDb.OleDbDataAdapter(Requet, GP)
EmplTA.Fill(GPDataset, "Employés")

'creátion d'une nouvelle ligne avec les données des textbox
GPDataset.Tables("Employés").NewRow()

DTR("Nom") = TextBox2.Text
DTR("Prénom") = TextBox3.Text
DTR("Sexe") = ComboBox1.Text
DTR("Adresse") = TextBox4.Text
DTR("Ville") = TextBox5.Text

'ajout de la ligne dans le DataSet
GPDataset.Tables("Employés").Rows.Add(DTR)

Try

'création et exécution du commandbuilder pour mettre à jour le DataAdapter

CmdB = New OleDb.OleDbCommandBuilder(EmplTA)

EmplTA.UpdateCommand = CmdB.GetUpdateCommand()

'mise à jour des données du DataAdapter à partir du commandbuilder
EmplTA.Update(GPDataset, "Employés")

Catch
MsgBox("Une erreur est survenue pendant la mise à jour." & Chr(13) & Chr(10) & _
"L'opération a échoué!", MsgBoxStyle.Critical, "Echec") End Try
0
Rejoignez-nous