Invalid Cast Exception when filling datatable wth table adaptor
I am using VB.NET 2010 (Visual Basic 2010 Express) on a WPF-based project. I am also using the SQL Server Express built-in to Visual Basic 2010 express.
I have just about finished refining my code for hooking up my wpf-based form to an existing SQL database (agentroster.sdf). I have a global data source (AGENT_ROSTER) connected to this database. Connections are confirmed to work properly.
This is the first part of the code I'm using, irrelevant code omitted,
Dim table_adaptor As New AGENT_ROSTERTableAdaptors.AGENT_ROSTERTableAdaptor
Dim roster_table As New DataTable("roster_table")
Dim rowposition As Integer
Private Sub ROSTER_Loaded...
table_adaptor.Fill(roster_table)
End Sub
I am getting the following errors: (In Immediate Window)
A first chance exception of type 'System.InvalidCastException' occured in VBP-WORD4WORD.exe
(In Messa开发者_StackOverflowge, pointing to the line: "table_adaptor.Fill(roster_table)')
InvalidCastException was unhandled Unable to cast object of type 'System.Data.DataTable' to type 'AGENT_ROSTERDataTable'.
What am I doing wrong, and furthermore, how do I fill roster_table with table_adaptor (or alternate method)?
Assuming that your strongly typed DataSet is called "AGENT_ROSTER":
Dim table_adaptor As New AGENT_ROSTERTableAdaptors.AGENT_ROSTERTableAdaptor
Dim roster_table As New AGENT_ROSTER.roster_table
table_adaptor.Fill(roster_table)
Have a look at Efficient Coding With Strongly Typed DataSets.
精彩评论