使用表适配器填充数据表时出现无效转换异常
我在基于 WPF 的项目上使用 VB.NET 2010 (Visual Basic 2010 Express)。我还使用 Visual Basic 2010 Express 内置的 SQL Server Express。
我刚刚完成了将基于 wpf 的表单连接到现有 SQL 数据库 (agentroster.sdf) 的代码的完善工作。我有一个连接到该数据库的全局数据源(AGENT_ROSTER)。确认连接正常工作。
这是我正在使用的代码的第一部分,省略了不相关的代码,
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
我收到以下错误: (在立即窗口中)
VBP-WORD4WORD.exe 中第一次出现“System.InvalidCastException”类型的异常
(在消息中,指向行:“table_adaptor.Fill(roster_table)”)
InvalidCastException 未处理 无法将“System.Data.DataTable”类型的对象转换为“AGENT_ROSTERDataTable”类型。
我做错了什么,此外,如何用 table_adaptor (或替代方法)填充 roster_table ?
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 Message, 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)?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
假设您的强类型数据集称为“AGENT_ROSTER”:
请查看高效编码强类型数据集。
Assuming that your strongly typed DataSet is called "AGENT_ROSTER":
Have a look at Efficient Coding With Strongly Typed DataSets.