我为学校制作了一个VB应用程序。我花了很多时间在它上面,当我最终完成它时我很高兴。它在调试模式下工作得很好。
现在的问题是:当我发布它,安装它并执行它时,我打开某些表单时会出错。
我注意到的是,只有代码中包含“ * *。Datasource =”的表单才会产生这些错误。 (例如:cbVertrekpunt.DataSource = getListofBstations()
)
GridView DataSource的原始功能:
Public Function getStation(ByVal station As String) As Station
Return (From s In treinDataContext.Stations Select s Where s.naam = station).First
End Function
以下是其中一种形式的错误:英文第一行:序列不包含元素
System.InvalidOperationException: Reeks bevat geen elementen
bij System.Data.Linq.SqlClient.SqlProvider.Execute(Expression query, QueryInfo queryInfo, IObjectReaderFactory factory, Object[] parentArgs, Object[] userArgs, ICompiledSubQuery[] subQueries, Object lastResult)
bij System.Data.Linq.SqlClient.SqlProvider.ExecuteAll(Expression query, QueryInfo[] queryInfos, IObjectReaderFactory factory, Object[] userArguments, ICompiledSubQuery[] subQueries)
bij System.Data.Linq.SqlClient.SqlProvider.System.Data.Linq.Provider.IProvider.Execute(Expression query)
bij System.Data.Linq.DataQuery`1.System.Linq.IQueryProvider.Execute[S](Expression expression)
bij System.Linq.Queryable.First[TSource](IQueryable`1 source)
bij TreinplannerBL.TreinController.getStation(String station)
bij TreinplannerBL.RittenbeheerderController.getEindstationMogelijkMetBeginstation(String beginstation)
bij RitController.AankoopTicket.cbVertrekpunt_SelectedIndexChanged(Object sender, EventArgs e)
bij System.Windows.Forms.ComboBox.OnSelectedIndexChanged(EventArgs e)
bij System.Windows.Forms.ComboBox.set_SelectedIndex(Int32 value)
bij RitController.AankoopTicket.AankoopTicket_Load(Object sender, EventArgs e)
bij System.Windows.Forms.Form.OnLoad(EventArgs e)
bij System.Windows.Forms.Form.OnCreateControl()
bij System.Windows.Forms.Control.CreateControl(Boolean fIgnoreVisible)
bij System.Windows.Forms.Control.CreateControl()
bij System.Windows.Forms.Control.WmShowWindow(Message& m)
bij System.Windows.Forms.Control.WndProc(Message& m)
bij System.Windows.Forms.ScrollableControl.WndProc(Message& m)
bij System.Windows.Forms.Form.WmShowWindow(Message& m)
bij System.Windows.Forms.Form.WndProc(Message& m)
bij System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
bij System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
bij System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
额外信息:我确定问题在于当我打开没有函数是数据源的表单时函数是数据源的事实,我没有得到任何错误。当我打开具有“.DataSource =”的表单时,我会收到错误,但是当我删除这些行时,错误不再出现
答案 0 :(得分:0)
是。您只需要将LINQ数据加载到DataTable中,然后返回:
Imports System.Data.DataTableExtensions
' ...
' Example LINQ query comparing two DataTables. Use the query you need.
Dim q = From a In DataTable1.AsEnumerable(), b In DataTable2.AsEnumerable() _
Where a!product_id = b!field_xyz _
Select a!field_1, a!field_2, a!field_3
Dim results As New DataTable
results.Columns.Add("field_1")
results.Columns.Add("field_2")
results.Columns.Add("field_3")
For Each item As Object In q
results.Rows.Add(item.field_1, item.field_1, item.field_1)
Next
Return results
答案 1 :(得分:0)
你得到“序列不包含任何元素”,这意味着它就像检索数据集一样(并且数据集中没有项目)。
在堆栈跟踪中我可以看到
bij System.Linq.Queryable.First [TSource](IQueryable`1 source)
bij TreinplannerBL.TreinController.getStation(String station)
这表明你的getStation
方法中的 ,你正在使用First
并且它失败了,因为集合中没有元素。
如果不知道该方法中的内容我只能猜测,但它看起来像这样:
Public Function getStation(string station) As Station
' get someCollectionOfStations from somewhere (or perhaps already retrieved),
' maybe apply a select ... where on the collection
Return someCollectionOfStations.First()
End Function
这突出了两个可能的问题:
First
会引发异常。在这种情况下会发生什么?不幸的是,您发布的代码与您发布的例外情况不符,因此我无法提供任何进一步的建议。