Click here to Skip to main content
15,881,204 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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.


Quote:
Dim examinationOfficer As New ExamOfficerPayEntities

Quote:
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"


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.

Quote:
Dim selectedOfficer As Exams_Officer = DirectCast(lstFullname.SelectedItem, Exams_Officer)
Dim selectedOfficerID As Integer = selectedOfficer.Officer_ID
T
The code dosen't work it throws an error on the first line (directcast method)
Quote:
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
Posted

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.
 
Share this answer
 
Comments
jamiebones 31-May-13 14:51pm    
Hello please i did what you said the error is now a MissingMemberException error saying Public member 'Officer_ID' on type 'String' not found. Please help me out.Thanks for your time
Mohammed Hameed 31-May-13 23:57pm    
The exception itself says that 'Office_ID' is not a member of selectedOfficer. You can check this link for the exception details: http://msdn.microsoft.com/en-us/library/system.missingmemberexception.aspx.
Carefully see your code:
Dim selectedOfficer = DirectCast(lstFullname.SelectedItem, Exams_Officer)
Dim selectedOfficerID As Integer = selectedOfficer.Officer_ID

DirectCast method is returning a string and obviously a string type does not have a member called 'Officer_ID'. So I suggest you to check the code inside the method DirectCast to analyze what exactly it is doing and why it is returning string only.
If you want me to have a look please provide the code of DirectCast method.
Mohammed Hameed 3-Jun-13 8:34am    
I'm glad that it has helped you resolve your issue :). Can you update me what was the problem with your DirectCast method?
I'm not sure how you did your connection, but based on the brief sql query I am thinking linq2sql ...

VB
Dim findOfficer = From officer In examinationOfficer.Exams_Officer
 Order By officer.First_Name Ascending
 Select officer.First_Name.ToUpper & " " & officer.Last_Name.ToUpper



This does not pull the 'Office_ID' and it only selects the first and last name. :)

Then you can set the primary key on the list control as the selected value and the name as the displayed value. To make things easier, if you use the first and last name often enough, I would create a field in the database that equals your first & last and call it fullnametext or similar. It saves on coding to just add it in for display purposes.

Check your link formatting.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900