2011-01-13 60 views
0

我使用VB.NET 2010(Visual Basic中2010速成)时。我也使用Visual Basic 2010 express内置的SQL Server Express。无效的转换异常在基于WPF的项目填充数据表问心无愧表适配器

我刚才已经谈完了琢磨我的代码钩住我的基于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中发生类型'System.InvalidCastException'的异常-WORD4WORD.exe

(In Message,指向该行:“table_adaptor.Fill(ro ster_table)')

InvalidCastException的是未处理 无法转换类型的对象 'System.Data.DataTable' 为类型 'AGENT_ROSTERDataTable'。

我在做什么错了,此外,如何填充table_adaptor(或替代方法)roster_table?

回答

0

假设你的强类型DataSet名为 “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) 

看一看Efficient Coding With Strongly Typed DataSets

+0

太棒了!非常感谢。 – CodeMouse92 2011-01-14 01:40:48

相关问题