Student Registration System in VB.Net – Coding the Program


This is the last series of tutorial designed for our student registration system to make run and use as your reference.

Double Click the Close Button and type this code:



Private Sub btnClose_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClose.Click
        Me.Close()
End Sub

It enables you to close or exit the whole system.

Double click the Clear Button and type this code:



'CLEAR ALL THE TEXT BOX
Private Sub btnClear_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClear.Click
        Me.txtStudID.Text = ""
        Me.txtName.Text = ""
        Me.txtPhone.Text = ""
        Me.txtAddress.Text = ""
        Me.cboGender.SelectedIndex = -1
        Me.txtStudID.Tag = ""
        'ENABLE EDIT BUTTON
        Me.btnEdit.Enabled = True
        'SET BUTTON ADD LABEL
        Me.btnSave.Text = "Save"
        Me.txtStudID.Focus()
End Sub
The code is simple unfilled the entire object that contain a value.

Next is we are going create a function to call it, every time the form is loading.

'REFILL ALL THE DATA IN DATAGRIDVIEW
    Private Sub RefreshData()
        If Not OleConn.State = ConnectionState.Open Then
            OleConn.Open()
        End If

        Dim da As New OleDb.OleDbDataAdapter("SELECT StudID as [Student ID], StudName as [Name], Gender, Phone, Address FROM StudData ORDER BY StudID", OleConn)
        Dim dt As New DataTable

        'FILL DATA TO DATA TABLE
        da.Fill(dt)

        'OFFER DATA IN DATA TABLE IN TO DATAGRIDVIEW
        Me.dgvData.DataSource = dt

        'CLOSE CONNECTION
        OleConn.Close()
    End Sub
The name of the function is RefreshData, So this function is simply loading the data to datagridview in our current database. As you can see in the Form Load event we call the function to easily call the data.



Double Click in the Form and type this code:


Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Timer2.Enabled = True

        'ESTABLISHED DATABASE CONNECTIONS
        OleConn = New OleDb.OleDbConnection
        OleConn.ConnectionString = "Provider=Microsoft.Jet.Oledb.4.0; Data Source=" & Application.StartupPath & "\StudData.mdb"

        'GET DATA ON THE LIST
        Me.RefreshData()
    End Sub

Before you run this program make sure to move your database access file from My Documents to Debug Folder found in your Bin Folder. After that we need to declare a variable under your public class form.



Public Class Form1
    'DECLARE VARIABLE FOR DATABASE CONNECTION
    Dim OleConn As New OleDb.OleDbConnection
Type in your variable for oledb connection.
Then Double click the Timer and Type this code:
 
'VARIABLE DECLARATION FOR TYPING EFFECT IN LABEL
    Public StrTick As String
    Public Count As Integer

    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        StrTick = "STUDENT REGISTRATION SYSTEM"
        If Label1.Text.Length = StrTick.Length Then
            Timer1.Enabled = False
            Exit Sub
        End If
        Label1.Text = StrTick.Substring(0, Count)
        Count = Count + 1
    End Sub
The label1 object has a empty value if you go back to GUI Design area. It is simply creating a typing effect of the label.

Next double click the Save Button and type this code:

'SAVE RECORD IN DATABASE
    Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click
        Dim OleCmd As New OleDb.OleDbCommand

        'OPEN CONNECTION
        If Not OleConn.State = ConnectionState.Open Then
            OleConn.Open()
        End If
        'ADD DATA TO TABLE
        OleCmd.Connection = OleConn
        If Me.txtStudID.Tag & "" = "" Then
            OleCmd.CommandText = "INSERT INTO StudData (StudID, StudName, Gender, Phone, Address) VALUES (" & Me.txtStudID.Text & ",'" & Me.txtName.Text & "','" & Me.cboGender.Text & "','" & Me.txtPhone.Text & "','" & Me.txtAddress.Text & "')"
            OleCmd.ExecuteNonQuery()
        Else
            'UPDATE DATA IN THE TABLE
            OleCmd.CommandText = "UPDATE StudData SET StudID=" & Me.txtStudID.Text & ", StudName='" & Me.txtName.Text & "', Gender='" & Me.cboGender.Text & "', Phone='" & Me.txtPhone.Text & "', Address='" & Me.txtAddress.Text & "' WHERE StudID=" & Me.txtStudID.Tag
            OleCmd.ExecuteNonQuery()
        End If
        'REFRESH DATA AFTER SAVING
        RefreshData()
        'CLEAR FORM
        Me.btnClear.PerformClick()

        'CLOSE CONNECTION
        OleConn.Close()
    End Sub

It enables the user to save and update the database.

Next double click the Edit Button and type the code:


'EDIT DATA IN DATABASE
    Private Sub btnEdit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnEdit.Click

        'CHECK FOR THE SELECTED ITEM IN LIST
        If dgvData.Rows.Count > 0 Then
            If dgvData.SelectedRows.Count > 0 Then
                Dim intStudID As Integer = Me.dgvData.SelectedRows(0).Cells("Student ID").Value
                'GET DATA FROM DATABASE FOLLOWED BY STUDENT ID
                'OPEN CONNECTION

                If Not OleConn.State = ConnectionState.Open Then
                    OleConn.Open()
                End If

                Dim da As New OleDb.OleDbDataAdapter("SELECT * FROM StudData WHERE StudID=" & intStudID, OleConn)
                Dim dt As New DataTable

                da.Fill(dt)

                Me.txtStudID.Text = intStudID
                Me.txtName.Text = dt.Rows(0).Item("StudName")
                Me.cboGender.Text = dt.Rows(0).Item("Gender")
                Me.txtPhone.Text = dt.Rows(0).Item("Phone")
                Me.txtAddress.Text = dt.Rows(0).Item("Address")

                'HIDE ID TO BE EDITED IN TAG OF TXTSTUDUD IN CASE ID IS CHANGED
                Me.txtStudID.Tag = intStudID

                'CHANGE BUTTON SAVE TO UPDATE

                Me.btnSave.Text = "Update"

                'DISABLE BUTTON EDIT
                Me.btnEdit.Enabled = False

                'CLOSE CONNECTION
                OleConn.Close()
            End If
        End If
    End Sub
To edit the data, make sure that it full row selected or highlighted to work the edit function properly.

Double click the delete button and type this code:
 
'DELETE RECORD IN DATABASE
    Private Sub btnDelete_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDelete.Click
        'CHECK FOR THE SELECTED ITEMS IN LIST
        If Me.dgvData.Rows.Count > 0 Then
            If Me.dgvData.SelectedRows.Count > 0 Then
                Dim intStudID As Integer = Me.dgvData.SelectedRows(0).Cells("Student ID").Value

                'OPEN CONNECTION
                If Not OleConn.State = ConnectionState.Open Then
                    OleConn.Open()
                End If

                'DELETE DATA
                Dim OleCmd As New OleDb.OleDbCommand
                OleCmd.Connection = OleConn
                OleCmd.CommandText = "DELETE FROM StudData WHERE StudID=" & intStudID
                OleCmd.ExecuteNonQuery()

                'REFRESH DATA
                Me.RefreshData()
                'CLOSE CONNECTION
                OleConn.Close()

            End If
        End If
    End Sub
After typing the code, run it and your done! If an error occur just comment below. Enjoy and Happy Coding!


Student Registration System in VB.Net – Coding the Program Student Registration System in VB.Net – Coding the Program Reviewed by code-dev on 12:36 AM Rating: 5

1 comment:

  1. where does the code below go?

    Private Sub RefreshData()
    If Not OleConn.State = ConnectionState.Open Then
    OleConn.Open()
    End If

    Dim da As New OleDb.OleDbDataAdapter("SELECT StudID as [Student ID], StudName as [Name], Gender, Phone, Address FROM StudData ORDER BY StudID", OleConn)
    Dim dt As New DataTable

    'FILL DATA TO DATA TABLE
    da.Fill(dt)

    'OFFER DATA IN DATA TABLE IN TO DATAGRIDVIEW
    Me.dgvData.DataSource = dt

    'CLOSE CONNECTION
    OleConn.Close()

    ReplyDelete

Powered by Blogger.