KeyNotFoundException beim füllen von DataTable?

  • VB.NET

Es gibt 9 Antworten in diesem Thema. Der letzte Beitrag () ist von INOPIAE.

    KeyNotFoundException beim füllen von DataTable?

    Hallo, leutz ich bin grad am herumbasteln mit ner datenbank und hab grad kein plan wo das Problem liegt.
    Und zwar versuch ich mit folgenden Code eine Datenbank in eine Listview mit Spalten Einzutragen.

    VB.NET-Quellcode

    1. MySQLReadConn.Open()
    2. Dim sqlQuery As String = "SELECT * FROM " + Mysqlusertable
    3. MsgBox(sqlQuery)
    4. Dim sqlAdapter As New MySqlDataAdapter
    5. Dim sqlCommand As New MySqlCommand
    6. Dim datatable As New DataTable
    7. Dim i As Integer
    8. With sqlCommand
    9. .CommandText = sqlQuery
    10. .Connection = MySQLReadConn
    11. End With
    12. With sqlAdapter
    13. Try
    14. .SelectCommand = sqlCommand
    15. .Fill(datatable)
    16. Catch ex As Exception
    17. MessageBox.Show(ex.Message)
    18. End Try
    19. End With
    20. For i = 0 To datatable.Rows.Count - 1
    21. With listview
    22. .Items.Add(datatable.Rows(i)("id"))
    23. With .Items(.Items.Count - 1).SubItems
    24. .Add(datatable.Rows(i)("name"))
    25. .Add(datatable.Rows(i)("pass"))
    26. End With
    27. End With
    28. Next
    29. End Sub

    So jetzt hab ich folgendes Problem und zwar Spuckt der Mir folgenden fehler an der stelle .FillDatatable aus


    Der connection String wird vorher so gesetzt:

    VB.NET-Quellcode

    1. MySQLReadConn = New MySqlConnection
    2. MySQLReadConn.ConnectionString = "server=" + host + ";userid=" + userid + ";password=" + password + ";database=" + database
    3. MySQLusertable = "users"


    Wo liegt hier der Hund Begraben? :S kann mir das wer sagen?
    Ich hoffe, dass ihr mir hierbei helfen könnt danke :thumbup:

    Naja zusehen ist eigentlich nichts unnormales? Das komische ist das genau dieser code in einem Anderen Programm funktioniert.
    Könnte das problem auch an der Datenbank liegen, wobei dan würde mir Ja ein Richtiger Fehlercode ausgegeben oder?
    Ups sorry,
    Stack Trace:
    bei System.ThrowHelper.ThrowArgumentNullException(ExceptionArgument argument)
    bei System.Collections.Generic.Dictionary`2.FindEntry(TKey key)
    bei System.Collections.Generic.Dictionary`2.get_Item(TKey key)
    bei MySql.Data.MySqlClient.CharSetMap.GetChararcterSet(DBVersion version, String CharSetName)
    bei MySql.Data.MySqlClient.NativeDriver.GetFieldMetaData41()
    bei MySql.Data.MySqlClient.NativeDriver.GetFieldMetaData()
    bei MySql.Data.MySqlClient.NativeDriver.ReadColumnMetadata(Int32 count)
    bei MySql.Data.MySqlClient.MySqlDataReader.NextResult()
    bei MySql.Data.MySqlClient.MySqlCommand.ExecuteReader(CommandBehavior behavior)
    bei MySql.Data.MySqlClient.MySqlCommand.ExecuteDbDataReader(CommandBehavior behavior)
    bei System.Data.Common.DbCommand.System.Data.IDbCommand.ExecuteReader(CommandBehavior behavior)
    bei System.Data.Common.DbDataAdapter.FillInternal(DataSet dataset, DataTable[] datatables, Int32 startRecord, Int32 maxRecords, String srcTable, IDbCommand command, CommandBehavior behavior)
    bei System.Data.Common.DbDataAdapter.Fill(DataTable[] dataTables, Int32 startRecord, Int32 maxRecords, IDbCommand command, CommandBehavior behavior)
    bei System.Data.Common.DbDataAdapter.Fill(DataTable dataTable)
    bei KTM_ServiceProtokoll.databasemodule.loadusertable() in C:\Users\Xaver\Documents\Visual Studio 2008\Projects\KTM ServiceProtokoll\KTM ServiceProtokoll\databasemodule.vb:Zeile 56.
    bei KTM_ServiceProtokoll.LoginForm.dologin(String user, String passmd5) in C:\Users\Xaver\Documents\Visual Studio 2008\Projects\KTM ServiceProtokoll\KTM ServiceProtokoll\LoginForm.vb:Zeile 47.
    bei KTM_ServiceProtokoll.LoginForm.OK_Click(Object sender, EventArgs e) in C:\Users\Xaver\Documents\Visual Studio 2008\Projects\KTM ServiceProtokoll\KTM ServiceProtokoll\LoginForm.vb:Zeile 24.
    bei System.Windows.Forms.Control.OnClick(EventArgs e)
    bei System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
    bei System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
    bei System.Windows.Forms.Control.WndProc(Message& m)
    bei System.Windows.Forms.ButtonBase.WndProc(Message& m)
    bei System.Windows.Forms.Button.WndProc(Message& m)
    bei System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
    bei System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
    bei System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
    bei System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(Int32 dwComponentID, Int32 reason, Int32 pvLoopData)
    bei System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
    bei System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
    bei Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.OnRun()
    bei Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.DoApplicationModel()
    bei Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.Run(String[] commandLine)
    bei KTM_ServiceProtokoll.My.MyApplication.Main(String[] Args) in 17d14f5c-a337-4978-8281-53493378c1071.vb:Zeile 81.
    bei System.AppDomain._nExecuteAssembly(Assembly assembly, String[] args)
    bei Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
    bei System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
    bei System.Threading.ThreadHelper.ThreadStart()
    und innerException?

    naja, vlt. egal. Im StacTrace ist von CharsetMap die Rede, also vermute ich, dass entweder was mitte DB nicht stimmt, oder mitte Konfiguration der MySql-Connector-Klassen.

    Tipp: lass die Datenbank erstmal weg, und knöpf dir vordringlichere Lernfelder vor: Datenverarbeitungs-Vorraussetzungen
    Weil dein Ansatz mit Listview und untypisierten DataTables ist eh für nix brauchbares zu gebrauchen.

    Aber auch schon deine Grundeinstellungen mittm Microsoft.Visualbasic GeneralImport, und Option Strict Off erschweren es dir, .Net-Programmier-Konzepte ganz allgemein und überhaupt zu verstehen.
    Näheres dazu in den weiterführenden Links des Links.

    Dieser Beitrag wurde bereits 3 mal editiert, zuletzt von „ErfinderDesRades“ ()

    Alles klar, hab grad mal an der Datenbank rumgebastelt, lag wohl daran das der 1 Typ ein INTEGEr war,
    Werd mich dass aber anders probieren weil ich glaube das der code ziemlicher schrott is den ich da gebastelt hab hehe ;)
    Muss mich mit dem Ganzen wohl noch einige zeit auseinandersetzen:>