Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
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 31-May-13 4:37am
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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.
  Permalink  
Comments
jamiebones at 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 at 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 at 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?
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

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
 

This does not pull the 'Office_ID' and it only selects the first and last name. Smile | :)
 
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.
  Permalink  

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

  Print Answers RSS
0 OriginalGriff 277
1 Maciej Los 210
2 BillWoodruff 205
3 Jochen Arndt 180
4 Sergey Alexandrovich Kryukov 165
0 OriginalGriff 5,130
1 DamithSL 4,157
2 Maciej Los 3,650
3 Kornfeld Eliyahu Peter 3,460
4 Sergey Alexandrovich Kryukov 2,811


Advertise | Privacy | Mobile
Web04 | 2.8.141216.1 | Last Updated 2 Jun 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100