Drole de bug avec DataGrid et requete
Bonjour,
J'ai un drole de bug que je ne comprends pas.
J'affiche dans un DataGrid le contenu d'une table. J'ai produit une procédure pour valider si la table est vide ou pleine. Si c'est le cas ca n'affiche rien. SINON je call une autre procédure pour afficher les données dans la table.
Voici le code pour vérifier si la table PIECES est vide ou pleine.
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
|
Private Sub verifier_trier_piece()
Dim strCon = CStr("provider=microsoft.jet.oledb.4.0;" & "data source= " & Application.StartupPath & "\demandes.mdb" & ";")
Dim CnEnreg As OleDb.OleDbConnection
Dim CmdEnreg As OleDb.OleDbCommand
CnEnreg = New OleDb.OleDbConnection(strCon)
CnEnreg.ConnectionString = strCon
CnEnreg.Open()
CmdEnreg = CnEnreg.CreateCommand() 'Creation de commande
Dim requete As String = vbEmpty
requete = "SELECT ID_Piece FROM Pieces"
CmdEnreg.CommandText = requete
Try
CmdEnreg.ExecuteNonQuery() 'Exécution de requete
Dim enumerate As OleDb.OleDbDataReader = CmdEnreg.ExecuteReader()
If enumerate.HasRows = True Then '' la piece existe!
CnEnreg.Close()
CnEnreg = Nothing
enumerate.Close()
enumerate = Nothing
CmdEnreg = Nothing
Call Me.afficher_DansGrid_Pieces()
Else ' elle n'existe pas.
CnEnreg.Close()
CnEnreg = Nothing
enumerate.Close()
enumerate = Nothing
CmdEnreg = Nothing
MsgBox("Aucune pièce trouvée avec la lettre " & Me.Cbox_Pieces.Text, MsgBoxStyle.Information)
End If
Catch ex As OleDb.OleDbException
MsgBox(ex.Message)
CnEnreg.Close()
CnEnreg = Nothing
CmdEnreg = Nothing
End Try
End Sub |
Après je call cette procédure qui affiche les données dans la table PIECES.
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
|
Dim strCon$ = CStr("provider=microsoft.jet.oledb.4.0;" & "data source= " & Application.StartupPath & "\Demandes.mdb" & ";")
Dim CnEnreg As New OleDb.OleDbConnection
Dim CmdR As New OleDb.OleDbCommand
Dim DtsetRecherche As New DataSet
Dim dtAdapteurRecherche As New OleDb.OleDbDataAdapter
'Dim IEnum As IEnumerator
CnEnreg.ConnectionString = strCon$
Dim requete As String
requete = "select Id_Piece from Pieces"
CnEnreg.Open()
CmdR = CnEnreg.CreateCommand() 'Creation de commande
CmdR.CommandText = requete
' mode non connecté
DtsetRecherche = New DataSet
Try
' prepare le code pour afficher le resultat de la requete dans la datagridview
dtAdapteurRecherche = New OleDb.OleDbDataAdapter
dtAdapteurRecherche.SelectCommand = CmdR
dtAdapteurRecherche.Fill(DtsetRecherche, "Pieces")
Me.DG_Techniciens.DataSource = DtsetRecherche.Tables("Pieces")
Me.DG_Pieces.Refresh()
Me.DG_Pieces.Columns.Item(0).HeaderText = "ID Piece"
Me.DG_Pieces.Columns.Item(0).Visible = true
Me.DG_Pieces.AllowUserToAddRows = False
Me.DG_Pieces.AllowUserToDeleteRows = False
Me.DG_Pieces.AllowUserToOrderColumns = False
Me.DG_Pieces.AllowUserToResizeColumns = False
Me.DG_Pieces.AllowUserToResizeRows = False
CnEnreg = Nothing
CmdR = Nothing
dtAdapteurRecherche = Nothing
'Objet déconnectée
DtsetRecherche = Nothing
Catch ex As OleDb.OleDbException
MsgBox(ex.Message)
CnEnreg = Nothing
CmdR = Nothing
dtAdapteurRecherche = Nothing
'Objet déconnectée
DtsetRecherche = Nothing
End Try
CnEnreg = Nothing
CmdR = Nothing
dtAdapteurRecherche = Nothing
'Objet déconnectée
DtsetRecherche = Nothing |
A la ligne
Code:
Me.DG_Pieces.Columns.Item(0).HeaderText = "ID Piece"
tout arrête. Le programme plante. Voici le message d'erreur.
Citation:
System.ArgumentOutOfRangeException was unhandled
Message="L'index était hors limites. Il ne doit pas être négatif et doit être inférieur à la taille de la collection. Nom du paramètre*: index"
ParamName="index"
Source="mscorlib"
StackTrace:
à System.Collections.ArrayList.get_Item(Int32 index) à System.Windows.Forms.DataGridViewColumnCollection.get_Item(Int32 index) à Gestion_Des_Services.Gestions.afficher_DansGrid_Pieces() dans K:\Programme Gestion Des Demandes\Gestion Des Services\Gestion Des Services\Gestions.vb:ligne 464 à Gestion_Des_Services.Gestions.verifier_trier_piece() dans K:\Programme Gestion Des Demandes\Gestion Des Services\Gestion Des Services\Gestions.vb:ligne 309 à Gestion_Des_Services.Gestions.Btn_Trier_Piece_Click(Object sender, EventArgs e) dans K:\Programme Gestion Des Demandes\Gestion Des Services\Gestion Des Services\Gestions.vb:ligne 276 à System.Windows.Forms.Control.OnClick(EventArgs e) à System.Windows.Forms.Button.OnClick(EventArgs e) à System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent) à System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks) à System.Windows.Forms.Control.WndProc(Message& m) à System.Windows.Forms.ButtonBase.WndProc(Message& m) à System.Windows.Forms.Button.WndProc(Message& m) à System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m) à System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m) à System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam) à System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg) à System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(Int32 dwComponentID, Int32 reason, Int32 pvLoopData) à System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context) à System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context) à System.Windows.Forms.Application.Run(ApplicationContext context) à Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.OnRun() à Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.DoApplicationModel() à Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.Run(String[] commandLine) à Gestion_Des_Services.My.MyApplication.Main(String[] Args) dans 17d14f5c-a337-4978-8281-53493378c1071.vb:ligne 81 à System.AppDomain._nExecuteAssembly(Assembly assembly, String[] args) à System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args) à Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly() à System.Threading.ThreadHelper.ThreadStart_Context(Object state) à System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state) à System.Threading.ThreadHelper.ThreadStart()
InnerException:
Le problème est que la table a 2 enregistrements et il y a juste un champ, Id_Piece qui est une clé unique et champ String.
Le code que vous voyez est utilisé et modifiés pour deux autres DataGrid et ils fonctionnent très bien.
En dernier recours j'ai détruit le contrôle DataGrid DG_Pieces dans le projet et je l'ai recréé sans modification des propriétés. Rien ne change! Ça plante toujours à la même place.... Si je comprends bien il ne semble pas voir les 2 enregistrements dans la table. mais c'est illogique! :marteau: :cfou:
Avez-vous une idée du problème ?
Merci :)
AngelEvil!