WHAT'S NEW?
Loading...

How to insert record into related tables


I’ve already created simple table that has a relationships among other tables. Actually, lot of research findings how to create a program in Visual Basic.Net that will be able to insert multiple records into related table. This is my big problem how to insert record into related tables in visual basic.net, and lot of practice wondering why, how it could data to be inserted. Sometimes, I was thinking with that solution because I am having a project soon and I already taught only one table to insert record. However, good things I was learned while working for fun having programmed and I was thoughts what if I could try again how to insert data into multiple records. So I’ll try to create tables and has a field names. When you noticed, there’s no proper way doing naming what looks like my database is. The purpose while making this table is to try if it is work. While doing this, I was thinking all other possibilities how to do first. However, although it is challenge for me, so I’ll try over and over again. Accidentally, I made it! Now, I already insert data into related tables in Visual Basic.Net, that’s why I posted it, because this is what I am aim for.

All the diagrams, codes, and queries are stated here.

Relational databases
Dataset diagram

This are queries made from my database:

Query for tblstudent
INSERT INTO `tblstudent` (`Fname`, `Lname`) VALUES (?, ?)

Query for tblenroll
INSERT INTO `tblenroll` (`StudentID`,  `Section`) VALUES (?, ?)

Query for tblsubjects
INSERT INTO `tblsubjects` (`EnrollID`,`TeacherID`,`FirstGrading`) VALUES (?,?,?)

Query for tblteacher
INSERT INTO `tblteacher` (`Fname`) VALUES (?)

We have 5 texboxes in form Fnametextbox,Lnametextbox,FirstGrading,Section, teacher, and 1 button

Me.TblstudentTableAdapter.InsertQueryStudent(FnameTextBox.Text, LnameTextBox.Text)
Me.TblstudentTableAdapter.Update(Me.DbtestingDataSet.tblstudent)
Me.TblstudentTableAdapter.Fill(Me.DbtestingDataSet.tblstudent)
Me.TblstudentBindingSource.MoveLast()

Me.TblenrollTableAdapter.InsertQueryenroll(Me.TblstudentBindingSource.
Current.item("StudentID"), SectionTextBox.Text)
Me.TblenrollTableAdapter.Update(Me.DbtestingDataSet.tblenroll)
Me.TblenrollTableAdapter.Fill(Me.DbtestingDataSet.tblenroll)
Me.TblenrollBindingSource.MoveLast()

Me.TblteacherTableAdapter.InsertQueryteacher(TeacherTextbox.Text)
Me.TblteacherTableAdapter.Update(Me.DbtestingDataSet.tblteacher)
Me.TblteacherTableAdapter.Fill(Me.DbtestingDataSet.tblteacher)
Me.TblteacherBindingSource.MoveLast()

Me.TblsubjectsTableAdapter.InsertQuerysubject(Me.TblenrollBindingSource.
Current.item("EnrollID"), Me.TblteacherBindingSource.Current.
item("TeacherID"), Me.FirstGradingTextBox.Text)
Me.TblsubjectsTableAdapter.Update(Me.DbtestingDataSet.tblsubjects)

MessageBox.Show("Save")


Display record in a grid. It will be create Datatable

Datatable has now field from other tables in order to view in gridview.

How to clear textbox in visual basic.net


Visual Basic.Net has a capability in clearing all textboxes and combo boxes in just one click. The usual way in clearing a textboxes using .Clear() keyword that clear only one textbox at a time or simply put “” represent null to clear textbox. However, in combo box, clear is not applicable to clear the text instead combobox.text =” “. For instance, if you have a lot of textbox in your form, it is difficult to manage which textbox you are going to clear, and it waste a lot of time typing the name of every textboxes. To address this problem, we are going to control all textboxes in order to clear in just one click.


Here are the codes:

Note: Just copy the code after End Sub, because you create new method that clear all textbox.

#Region "Clear all Textboxes"
    Public Sub ClearTextBox(ByVal ClearAll As Control)
        For Each ctrl As Control In ClearAll.Controls
            ClearTextBox(ctrl)
            If TypeOf ctrl Is TextBox Then
                CType(ctrl, TextBox).Text = String.Empty
            End If
        Next ctrl
    End Sub
#End Region

After that, Copy ClearTextBox(Me) and paste in your button clear.

The same in ComboBox.
#Region "Clear all combobox"
    Public Sub clearcombo(ByVal ok As Control)
        For Each clr As Control In ok.Controls
            clearcombo(clr)
            If TypeOf clr Is ComboBox Then
                CType(clr, ComboBox).Text = String.Empty
            End If
        Next clr
    End Sub
#End Region