Click here to Skip to main content
15,881,588 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
VB
Dim da As New SqlDataAdapter("SELECT Subjects.subject_name FROM Subjects " _
     & "INNER JOIN Departments_Subjects_junction ON Subjects.subject_id = Departments_Subjects_junction.subject_id " _
     & "INNER JOIN Semesters_Subjects_junction ON Subjects.subject_id = Semesters_Subjects_junction.subject_id " _
     & "WHERE Departments_Subjects_junction.department_id = @DepartmentID AND Semesters_Subjects_junction.semester_id = @SemesterID ", con)
       da.SelectCommand.Parameters.AddWithValue("@DepartmentID", departmentCb.ValueMember)
       da.SelectCommand.Parameters.AddWithValue("@SemesterID", semesterCb.ValueMember)
       da.Fill(dt)
       con.Close()
       If dt.Rows.Count = 0 Then
           MessageBox.Show("No record exists, try again", "SORRY!", MessageBoxButtons.OK)
       End If
Posted

"ValueMember"[^] (for a combo box) returns the name of the property that provides the value - not the actual value itself.

I suspect that you meant to have departmentCB.SelectedValue [^]?
 
Share this answer
 
Comments
Zoltán Zörgő 27-Oct-15 13:18pm    
Looks a good catch! +5
No wonder; nothing guarantees you that some random string value always holds correct representation of an integer value.
The whole idea put in your database schema is wrong. You either need to make your department_id a string uniquely identifying your department in required scope, not limiting it to integers, or use proper data type representing a numeric value. Anyway, you should never use string types where numeric data is required; it's both inefficient and unreliable.

See, for example, https://msdn.microsoft.com/en-us/library/ms187745.aspx[^].

—SA
 
Share this answer
 
SQL
da.SelectCommand.Parameters.AddWithValue("@DepartmentID", departmentCb.SelectedValue)
       da.SelectCommand.Parameters.AddWithValue("@SemesterID", semesterCb.SelectedValue)
 
Share this answer
 

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

  Print Answers RSS
Top Experts
Last 24hrsThis month


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