Comments inline...
On Jan 20, 3:06 pm, Laura <
mcmahon.la...@gmail.com> wrote:
> That first line in the For-Loop was there because on the form the user
> makes a selection from a combobox (cboClassSelect) and then once that
> selection has been made, the listbox will fill up with all the names
> of students who are in that selected class.
Yes, but my point was that the user's selection remains the same
throughout the life of your For-loop, therefore it makes no sense to
use it *within* the For-Loop. I also suggested removing the line
altogether because of two reasons -
a) My suggested code in my previous post does not render the statement
useless, because it is already futile. It does not help your code to
find Students matching the given class.
b) The Find() method of the DataTable only returns Rows that match the
Primary Key of the Table. ClassID is not the Primary Key in the
Student table, so theoretically this method should not return anything
(unless you have similar ID's for Student and Class)
> I had been doing what you suggested previously but that simply
> displays ALL student names whereas I need it to display student names
> by class - by the class the user selects in the combobox.
It is my mistake as well... I did not entirely grasp what your
intentions were for running the For-loop (Reading so many different
snippets of code makes one capable of cutting through the supposedly
unimportant stuff). I was entirely concentrating on the reason for
your little error. I now realize that you're simply trying to iterate
through the Students table trying to find those records that match the
ClassID.
> How can I go about this? Was I anywhere close with that line of code :
> objRow = objDataSet.Tables("tblStudent").Rows.Find
> (cboClassSelect.SelectedValue.ToString)?
Let's take another look at this from the beginning (We'll ignore the
Attendance table for now)... Here is what you are presently doing (or
trying to do) :
1. Create a Dataset with two tables - Class and Students.
2. Create a DataRelation between them, with a 1-Many relationship
between Class and Student, respectively. Also note that this Relation
is not used further so creating it is ineffectual.
3. Get a Selected ClassID based on a selection in a ComboBox.
4. Iterate through the Students table and for each record, add the
first name and the surname of the student. There is presently no code
to match the Students that have the selected ClassID. The only
statement that provides a semblance of this match is the one we've
been discussing :
> objRow = objDataSet.Tables("tblStudent").Rows.Find(cboClassSelect.SelectedValue.ToString)
But it does not work, as I have explained above. Okay, enough, let's
go on to what you should be doing :
1. Create a Dataset with two tables - Class and Students.
2. Create a DataRelation between them, with a 1-Many relationship
between Class and Student, respectively.
3. Get a Selected ClassID based on a selection in a ComboBox.
4. *USE the DataRelation* you created above to find Rows in the
Student table which match the selected ClassID. This operation will
return an array of DataRows from the Student table.
5. Iterate through the array and for each record, add the first name
and the surname in the current Student record.
Now the way to perform Step 4 and 5 is illustrated below : (Typed in
here, watch for syntax)
---
Dim selClass as String = cboClassSelect.SelectedValue.ToString()
'Find the row in the Class table that matches this ID. In this case we
can use the Find() method.
Dim classRow as DataRow = objDataSet.Tables("tblClass").Rows.Find
(selClass)
If classRow isnot Nothing then
'Use the DataRelation to find all matching Student Rows.
Dim studentRows() as DataRow = classRow.GetChildRows
("Class2Student")
If studentRows.Length > 0 then
For each student as DataRow in studentRows
Dim sName as string = student("SName")
Dim fName as string = student("FName")
'Add this information to the ListBox
lstStudents51.Items.Add(sName + ", " + fName)
Next
Else
'This class does not have any students. Must be Plant
Biochemistry ! ;-)
End If
Else
'This Class does not exist in the Class table.
End If
---