skip to Main Content

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.

enter image description here

2

Answers


  1. 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)()

    cmd = New MySqlCommand(sql, myconn)
    dr = cmd.ExecuteReader
    
    While dr.Read
        For Each txt As TextBox In Me.Controls.OfType(Of TextBox)()
            txt.Text = dr.Item("subject_name").ToString
        Next
    End While
    

    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):

    While dr.Read = True
    
        Dim txt As New TextBox = DirectCast(Me.Controls.Find(string.Format("Textbox{0}", cnt ),false).FirstOrDefault(),Textbox);
    
        If Not txt Is Nothing Then txt.Text = dr.Item("subject_name").ToString
    
        cnt += 1
    
    End While
    dr.Close()
    
    Login or Signup to reply.
  2. When you read a query’s resultset, you use a loop as you know.

    While dr.Read
        ' run this for every row in your resultset 
        ...
    End While
    

    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

     If dr.Read
        ' just the first row
     End If
     If dr.Read
        ' just the second row
     End If
     If dr.Read
        ' just the third row
     End If
     ...
    

    From your question I guess you have Textbox1, Textbox2, … Textbox5 on your form. I also guess you have Grade1, Grade2 ….

    To handle both of the subject name and grade, change the first line of your query to

       sql = "SELECT subject_name, grade " & _
    

    You can populate those items like this:

     If dr.Read
        TextBox1.Text = dr.Item("subject_name").ToString
        Grade1.Text = dr.Item("grade").ToString
     End If
     If dr.Read
        TextBox2.Text = dr.Item("subject_name").ToString
        Grade2.Text = dr.Item("grade").ToString
     End If
     If dr.Read
        TextBox3.Text = dr.Item("subject_name").ToString
        Grade3.Text = dr.Item("grade").ToString
     End If
     ' more of these sets of four lines to fill your whole form.
    

    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.

      Dim Subjects As Textbox()
      Dim Grades As Textbox()
      ...
    
      Dim rownumber, Y
      rownumber = 0
      Y = 200
      Dim Subject
      Dim Grade
      While dr.Read
        Subject = New Textbox
        Subject.Text = dr.Item("subject_name").ToString
        Subject.Width = 200
        Subject.Height = 40
        Subject.X = 175
        Subject.Y = Y
        Subjects(rownumber) = Subject
        Form.Controls.Add(Subject)
        Grade = New Textbox
        Grade.Text = dr.Item("grade").ToString
        Grade.Width = 50
        Grade.Height = 40
        Grade.X = 400
        Grade.Y = Y
        Grades(rownumber) = Grade
        Form.Controls.Add(Grade)
        rownumber = rownumber + 1
        Y = Y + 50
      End While
    

    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 then Y = Y + 50 arithmetic.

    That’s why grid controls exist. They take care of that kind of thing.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search