I have
Table student, student_subject and subject_bsit
"student"
-----------------------
|studentID | FullName |
-----------------------
|1234 | John |
|1235 | Michael |
|1236 | Bryce |
"subject_bsit"
-----------------------------------
|subject_id| subject_name |grade |
-----------------------------------
| 1 | Programming | 3 |
| 2 | Networking | 2.5 |
| 3 | Algorithm | 1.75|
| 4 | Physical Educ | 2 |
This is the Junction table to connect the
two now.
"student_subject"
----------------------------
| student_id | subject_id |
----------------------------
| 1235 | 1 |
| 1235 | 2 |
| 1235 | 3 |
| 1234 | 1 |
As you can see the table ID 1235 is michael He has three three subjects, subject_id 1,2 and 3. What I want to do is to display all the subject name and grades of michael in textboxes, not in a datagrid view.
As of now I still have failed to output it to textboxes. This is my sample query
sql = "SELECT subject_name " & _
" FROM student_subject " & _
" INNER JOIN subject_bsit ON subject_bsit.subject_id = student_subject.sub_id" & _
" where student_subject.student_id='" & Txtbox.Text & "'"
The Txtbox.text in the last query is where the user will input the ID number.
This is my code on displaying the data to the textbox. I don’t have any idea or approach on how can i loop on the textbox and display it on each textbox.
cmd = New MySqlCommand(sql, myconn)
dr = cmd.ExecuteReader
While dr.Read
TextBox1.Text = dr.Item("subject_name").ToString
TextBox2.Text = dr.Item("subject_name").ToString
End While
This is the sample User Interface of what i am trying to achieve. Thank you so much.
2
Answers
If you are looking to create Textboxes dynamically then you should refer to the @OJones answer
You can simply loop over
Me.Controls.OfType(Of TextBox)()
Or you can do a similar approach if you need to fill the first subjects name inside the textboxes (if returned subjects are more than textboxes additional subjects will be ignored):
When you read a query’s resultset, you use a loop as you know.
The
While
loop keeps going until you have read all the rows.You don’t have to use a loop. If you wish you can read the rows one at a time, like this
From your question I guess you have
Textbox1
,Textbox2
, …Textbox5
on your form. I also guess you haveGrade1
,Grade2
….To handle both of the subject name and grade, change the first line of your query to
You can populate those items like this:
This solves your problem. But you probably notice it is absurdly repetitive. What you really need is an array (actually two arrays) of textboxes. You create, and then fill in, these texboxes in your program. I have not debugged this: that is for you do to.
When this runs you will have two columns of controls, one for each subject. But this code is complex, and you have to do all the layout of your form with
Something.Y = value
and thenY = Y + 50
arithmetic.That’s why grid controls exist. They take care of that kind of thing.