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.
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.
How can I go about this? Was I anywhere close with that line of code :
objRow = objDataSet.Tables("tblStudent").Rows.Find
(cboClassSelect.SelectedValue.ToString)?
On Jan 19, 3:00 pm, Cerebrus <[email protected]> wrote:
> No, it certainly isn't a spelling mistake. And the error is telling
> you exactly where the problem lies... ;-)
>
> The DataRow class exposes its Columns in the Row by means of the
> Indexer, i.e., the default Item property. This means that you can
> retrieve the data within any column in a given DataRow by the
> following syntax (in addition to various overloads) :
> ---
> Dim myData as Object = myDataRow.Item("MyColumnName")
> ---
>
> And the relevant part of your code is :
> ---
> objRow.Item(strSurname + ", " + strFirstName)
> ---
>
> What your code is trying to do is to find the data in the column named
> "Clancy, Martina" because that parameter is meant for the column name.
> It appears to me that you should simply write the following statement
> because you already have the firstName and lastName values in
> variables:
> ---
> lstStudents51.Items.Add(strSurname + ", " + strFirstName)
> ---
>
> P.S.: Since you are at the learning stage, I won't tell you that the
> first line in your For-loop should probably be outside the loop (if
> not removed altogether) because it serves no purpose within. Or maybe
> I will ! ;-)
>
> HTH.
>
> On Jan 19, 6:43 pm, Laura <[email protected]> wrote:
>
>
>
> > I have now made changes to my project. Now I have one form where you
> > select a class from a combobox and then the listbox needs to be
> > populated with the students from the selected class. However, I keep
> > getting an error and cannot figure out what's up ( my coding abilities
> > are not the strongest - as you may have noticed).
>
> > Public Sub FillStudentList()
> > Dim j As Integer
> > Dim strSurname As String
> > Dim strFirstName As String
>
> > For j = 1 To objDataSet.Tables("tblStudent").Rows.Count
> > objRow = objDataSet.Tables("tblStudent").Rows.Find
> > (cboClassSelect.SelectedValue.ToString)
> > strSurname = objDataSet.Tables("tblStudent").Rows(j -
> > 1).Item("SName")
> > strFirstName = objDataSet.Tables("tblStudent").Rows(j -
> > 1).Item("FName")
> > lstStudents51.Items.Add(objRow.Item(strSurname + ", " +
> > strFirstName))
> > Next
>
> > lstStudents51.SelectedIndex = 0
>
> > End Sub
>
> > The error is : "Column 'Clancy, Martina' does not belong to the table
> > tblStudent"
> > The column the error refers to is the first column in tblStudent. My
> > database is called tblStudent and its not a spelling mistake or
> > anything.
> > Can you help with this little error please?
>
> > Cerebrus wrote:
> > > Laura,
>
> > > I agree with "The_Fruitman" on this... Your design appears to be so
> > > deeply flawed (it violates the very tenets of programming) that it
> > > seems that your actual problem at hand is insignificant in comparison.
> > > I seriously hope we have misunderstood you and you are not using a
> > > separate form for each class... sounds like there are atleast 51 of
> > > these classes.
>
> > > On Jan 15, 8:10 pm, The_Fruitman <[email protected]> wrote:
> > > > Please tell me you aren't building a separate form for each class.
> > > > This is a bad design and should be avoided at all costs because of
> > > > several issues (these are the ones that quickly come to mind):
> > > > 1. What happens when a new class is added? Will you have to build
> > > > another form?
> > > > 2. What happens if a class has been removed? Do you need to remove or
> > > > hide the form?
> > > > 3. Having each class as a separate form means that you have duplicate
> > > > code all over the place, this is also something that should be
> > > > avoided. For one reason, if you have to make modifications to the
> > > > code, you have to change it in multiple areas.
>
> > > > If you must display the listing of students for a selected class in
> > > > another form there are other ways to do it. For example you may pass
> > > > the selected class id to the attendance form. This would allow you to
> > > > use the same form for all your classes (a much better design). On the
> > > > form load of the attendance form you would grab the passed in class id
> > > > and use it as a parameter in your sql statement to populate your
> > > > dataset. From there you can then do your adding of information from
> > > > the dataset to the listbox or whatever other controls you wish to
> > > > populate witih the information.
>
> > > > On Jan 14, 9:01 am, Laura <[email protected]> wrote:
>
> > > > > Hi,
>
> > > > > I have a database with a Student table and a Class table. A number of
> > > > > students are members of a particular class.
>
> > > > > On my windows form I need to display student names in a listbox. I
> > > > > then have a different form for each class. So for example (as per code
> > > > > below) this form is for Class51. The code I have so far is reading in
> > > > > all student names into the listbox. I need it to read in the names of
> > > > > those students in Class51.
>
> > > > > Each class has a classID. I know in SQL I would simply say SELECT *
> > > > > FROM STUDENT WHERE CLASSID = 8, but what do I need to add to my code
> > > > > here.
>
> > > > > Any help would be really appreciated.
>
> > > > > Private Sub frmAttendace51_Load(ByVal sender As System.Object, ByVal e
> > > > > As System.EventArgs) Handles MyBase.Load
>
> > > > > 'clear data set of any existing data
> > > > > objDataSet.Clear()
> > > > > 'fill schema
> > > > > objStudentDA.FillSchema(objDataSet, SchemaType.Source,
> > > > > "tblStudent")
> > > > > 'fill data set with info from data adapter
> > > > > objStudentDA.Fill(objDataSet, "tblStudent")
>
> > > > > 'fill the data set with info from tblClass
> > > > > objClassDA.FillSchema(objDataSet, SchemaType.Source,
> > > > > "tblClass")
> > > > > objClassDA.Fill(objDataSet, "tblClass")
>
> > > > > 'fill the data set with info from tblClass
> > > > > objAttendanceDA.FillSchema(objDataSet, SchemaType.Source,
> > > > > "tblAttendance")
> > > > > objAttendanceDA.Fill(objDataSet, "tblAttendance")
>
> > > > > 'setup the relationship
> > > > > objDataSet.Relations.Clear()
> > > > > objDataSet.Relations.Add("Class2Student", _
> > > > > objDataSet.Tables("tblClass").Columns
> > > > > ("ClassID"), _
> > > > > objDataSet.Tables
> > > > > ("tblStudent").Columns("ClassID"))
> > > > > objDataSet.Relations.Add("Student2Attendance", _
> > > > > objDataSet.Tables
> > > > > ("tblStudent").Columns("StudentID"), _
> > > > > objDataSet.Tables
> > > > > ("tblAttendance").Columns("StudentID"))
>
> > > > > lstStudents51.Items.Clear()
>
> > > > > Dim i As Integer
> > > > > Dim strSurname As String
> > > > > Dim strFirstName As String
> > > > > For i = 1 To objDataSet.Tables("tblStudent").Rows.Count
> > > > > strSurname = objDataSet.Tables("tblStudent").Rows(i -
> > > > > 1).Item("SName")
> > > > > strFirstName = objDataSet.Tables("tblStudent").Rows(i -
> > > > > 1).Item("FName")
> > > > > lstStudents51.Items.Add(strSurname + ", " + strFirstName)
> > > > > Next
>
> > > > > lstStudents51.SelectedIndex = 0- Hide quoted text -
>
> > > > - Show quoted text -- Hide quoted text -
>
> > - Show quoted text -- Hide quoted text -
>
> - Show quoted text -