InvalidCastException lors d'un accès base de donnée Access
Bonjour à tous,
Voila j'ai un problème que je n'arrive pas à comprendre...
Dans mon programme j'essaye de récupérer les informations contenues dans une base de donnée access via un OleDbDataReader et de les afficher dans une ListBox...
Le problème est qu'il me renvoi une InvalidCastException lors de la lecture d'une ligne.
Voila le code utilisé:
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
| Imports System
Imports System.Data
Imports System.Data.OleDb
Imports Microsoft.VisualBasic
Public Class Form3
Inherits System.Windows.Forms.Form
Dim connexion As OleDbConnection
Dim commande As OleDbCommand
Dim reader As OleDbDataReader
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
Me.Close()
End Sub
Private Sub Form3_FormClosing(ByVal sender As System.Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles MyBase.FormClosing
Form1.Show()
End Sub
Private Sub Form3_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Try
connexion = New OleDbConnection("Provider=Microsoft.Jet.Oledb.4.0;Data Source=C:\Film.mdb")
commande = connexion.CreateCommand()
commande.CommandText = "Select * From Film"
connexion.Open()
reader = commande.ExecuteReader()
Do While reader.Read()
ListBox1.Items.Add(reader.GetString(0))
Loop
reader.Close()
connexion.Close()
Catch ex As Exception
MsgBox(ex.ToString, MsgBoxStyle.Critical, "Erreur Form3")
My.Computer.FileSystem.WriteAllText("Erreur.log", TimeOfDay.ToString + ":" & ControlChars.CrLf + ex.ToString & ControlChars.CrLf, True)
Form1.Close()
End Try
End Sub
End Class |
Voici l'erreur générée:
Citation:
01/01/0001 20:51:29:
System.InvalidCastException: Le cast spécifié n'est pas valide.
à System.Data.OleDb.ColumnBinding.ValueString()
à System.Data.OleDb.OleDbDataReader.GetString(Int32 ordinal)
à WindowsApplication1.Form3.Form3_Load(Object sender, EventArgs e) dans D:\Visual Basic\ListeFilm\ListeFilm\Form3.vb:ligne 30
La ligne 30 correspond à ListBox1.Items.Add(reader.GetString(0))
Merci d'avance pour votre aide.