在linq到实体查询中帮助解决InvalidCastException错误 [英] Help on InvalidCastException error in linq to entity query
问题描述
您好我正在为我的部门工作申请。我遇到了一个大问题。我正在尝试在列表框控件上选择一个名称的主键。这个列表框控件是从一个来自表单loads.Below是用于填充列表框控件的querry。
Hello i am making an application for my department at work.I have run into a big problem.I am trying to get the primary key of a name selected on a listbox control.This listbox control is populated from a querry from the database when the form loads.Below is the querry used to populate the listbox control.
Dim examinationOfficer As New ExamOfficerPayEntities
Dim examinationOfficer As New ExamOfficerPayEntities
Dim findOfficer =来自官员在examOfficer.Exams_Officer
命令由officer.First_Name Ascending
选择officer.First_Name.ToUpper& & officer.Last_Name.ToUpper
lstFullname.DataSource = findOfficer.ToList
lstFullname.DisplayMember =FullName
Dim findOfficer = From officer In examinationOfficer.Exams_Officer
Order By officer.First_Name Ascending
Select officer.First_Name.ToUpper & " " & officer.Last_Name.ToUpper
lstFullname.DataSource = findOfficer.ToList
lstFullname.DisplayMember = "FullName"
querry工作正常,列表框按预期填充。问题是我试图获取所选列表框项的主键值。主键值i我试图检索是一个整数值,它被设置为一个身份。
以下是我的数据库的摘要
Table Exam_Officer
Officer_ID为int,它是主键,也设置为标识列
First_Name as nvarchar
last_Name as nvarchar
其他表是Result_Submitted
列是
sn作为整数主键和标识列
Officer_ID为int并且是表格的外键_Officer
Result_Submitted as nvarchar
Outstanding_Result as nvarchar
这是我试图用来从windows窗体的列表框控件的selecteditem属性中获取officer_ID的代码。
The querry works fine and the listbox is populated as expected.The problem is i am trying to get the value of the primary key of the selected listbox item.The primary key value i am trying to retrieve is an integer value and it is set as an identity.
Below is a summary of my database
Table Exam_Officer
Officer_ID as int which is the primary key and is also set as an identity column
First_Name as nvarchar
last_Name as nvarchar
Other Table is Result_Submitted
columns are
sn as integer primary key and an identity column
Officer_ID as an int and is a foreign key to the Table Exam_Officer
Result_Submitted as nvarchar
Outstanding_Result as nvarchar
This is the code i am trying to use to get the officer_ID from the selecteditem property of the listbox control of a windows form.
Dim selectedOfficer As Exams_Officer = DirectCast(lstFullname.SelectedItem,Exams_Officer)
Dim selectedOfficerID As Integer = selectedOfficer.Officer_ID
Dim selectedOfficer As Exams_Officer = DirectCast(lstFullname.SelectedItem, Exams_Officer)
Dim selectedOfficerID As Integer = selectedOfficer.Officer_ID
T
代码不工作会在第一行引发错误(直播方法)
T
The code dosen''t work it throws an error on the first line (directcast method)
System.InvalidCastException未处理
消息=无法将System.String类型的对象强制转换为''EXAMINATION_OFFICER_PAYMENT.Exams_Officer ''。
来源=考试官员付款
System.InvalidCastException was unhandled
Message=Unable to cast object of type ''System.String'' to type ''EXAMINATION_OFFICER_PAYMENT.Exams_Officer''.
Source=EXAMINATION OFFICER PAYMENT
我很困惑我不知道还能做什么。感谢任何帮助。谢谢你。一个非常精彩的社区
I am confused i don''t know what else to do.Any help is appreciated.Thanks as usual to a very wonderful community
推荐答案
删除输入Exams_Officer并尝试(不要使用任何其他类型,让编译器决定)。
这与C#中''var''关键字的用途相同。
查看此链接以获取更多详细信息: http:/ /stackoverflow.com/questions/2478552/vb-net-equivalent-to-c-sharp-var-keyword [ ^ ]
我希望这会有所帮助你来解决这个问题。
如果有任何疑问请告诉我。
Remove the type Exams_Officer and try (dont use any other type, let compiler decide).
This will serve same purpose as ''var'' keyword do in C#.
Check this link for further details: http://stackoverflow.com/questions/2478552/vb-net-equivalent-to-c-sharp-var-keyword[^]
I hope this helps you to resolve the issue.
If any queries please let me know.
我不知道你是怎么做的你的连接,但基于简短的SQL查询,我在想linq2sql ...
I''m not sure how you did your connection, but based on the brief sql query I am thinking linq2sql ...
Dim findOfficer = From officer In examinationOfficer.Exams_Officer
Order By officer.First_Name Ascending
Select officer.First_Name.ToUpper & " " & officer.Last_Name.ToUpper
这不会拉''Office_ID''它只选择名字和姓氏。 :)
然后,您可以将列表控件上的主键设置为选定值,将名称设置为显示值。为了方便起见,如果你经常使用名字和姓氏,我会在数据库中创建一个等于你的第一个和第一个的字段。最后并称之为fullnametext或类似。它节省了编码,只是为了显示目的而添加它。
检查链接格式。
这篇关于在linq到实体查询中帮助解决InvalidCastException错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!