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.ClickMe.Close()End Sub
It enables you to close or exit the whole system.
Double click the Clear Button and type this code:
The code is simple unfilled the entire object that contain a value.'CLEAR ALL THE TEXT BOXPrivate Sub btnClear_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClear.ClickMe.txtStudID.Text = ""Me.txtName.Text = ""Me.txtPhone.Text = ""Me.txtAddress.Text = ""Me.cboGender.SelectedIndex = -1Me.txtStudID.Tag = ""'ENABLE EDIT BUTTONMe.btnEdit.Enabled = True'SET BUTTON ADD LABELMe.btnSave.Text = "Save"Me.txtStudID.Focus()End Sub
Next is we are going create a function to call it, every time the form is loading.
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.'REFILL ALL THE DATA IN DATAGRIDVIEWPrivate Sub RefreshData()If Not OleConn.State = ConnectionState.Open ThenOleConn.Open()End IfDim 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 TABLEda.Fill(dt)'OFFER DATA IN DATA TABLE IN TO DATAGRIDVIEWMe.dgvData.DataSource = dt'CLOSE CONNECTIONOleConn.Close()End Sub
Double Click in the
Form and type this code:
Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.LoadTimer2.Enabled = True'ESTABLISHED DATABASE CONNECTIONSOleConn = New OleDb.OleDbConnectionOleConn.ConnectionString = "Provider=Microsoft.Jet.Oledb.4.0; Data Source=" & Application.StartupPath & "\StudData.mdb"'GET DATA ON THE LISTMe.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.
Type in your variable for oledb connection.Public Class Form1'DECLARE VARIABLE FOR DATABASE CONNECTIONDim OleConn As New OleDb.OleDbConnection
Then Double click the Timer and Type this code:
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.'VARIABLE DECLARATION FOR TYPING EFFECT IN LABELPublic StrTick As StringPublic Count As IntegerPrivate Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.TickStrTick = "STUDENT REGISTRATION SYSTEM"If Label1.Text.Length = StrTick.Length ThenTimer1.Enabled = FalseExit SubEnd IfLabel1.Text = StrTick.Substring(0, Count)Count = Count + 1End Sub
Next double click the Save Button and type this code:
'SAVE RECORD IN DATABASEPrivate Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.ClickDim OleCmd As New OleDb.OleDbCommand'OPEN CONNECTIONIf Not OleConn.State = ConnectionState.Open ThenOleConn.Open()End If'ADD DATA TO TABLEOleCmd.Connection = OleConnIf Me.txtStudID.Tag & "" = "" ThenOleCmd.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 TABLEOleCmd.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.TagOleCmd.ExecuteNonQuery()End If'REFRESH DATA AFTER SAVINGRefreshData()'CLEAR FORMMe.btnClear.PerformClick()'CLOSE CONNECTIONOleConn.Close()End Sub
It enables the user to save and update the database.
Next double click the Edit Button and type the code:
To edit the data, make sure that it full row selected or highlighted to work the edit function properly.'EDIT DATA IN DATABASEPrivate Sub btnEdit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnEdit.Click'CHECK FOR THE SELECTED ITEM IN LISTIf dgvData.Rows.Count > 0 ThenIf dgvData.SelectedRows.Count > 0 ThenDim intStudID As Integer = Me.dgvData.SelectedRows(0).Cells("Student ID").Value'GET DATA FROM DATABASE FOLLOWED BY STUDENT ID'OPEN CONNECTIONIf Not OleConn.State = ConnectionState.Open ThenOleConn.Open()End IfDim da As New OleDb.OleDbDataAdapter("SELECT * FROM StudData WHERE StudID=" & intStudID, OleConn)Dim dt As New DataTableda.Fill(dt)Me.txtStudID.Text = intStudIDMe.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 CHANGEDMe.txtStudID.Tag = intStudID'CHANGE BUTTON SAVE TO UPDATEMe.btnSave.Text = "Update"'DISABLE BUTTON EDITMe.btnEdit.Enabled = False'CLOSE CONNECTIONOleConn.Close()End IfEnd IfEnd Sub
Double click the delete button and type this code:
After typing the code, run it and your done! If an error occur just comment below. Enjoy and Happy Coding!'DELETE RECORD IN DATABASEPrivate Sub btnDelete_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDelete.Click'CHECK FOR THE SELECTED ITEMS IN LISTIf Me.dgvData.Rows.Count > 0 ThenIf Me.dgvData.SelectedRows.Count > 0 ThenDim intStudID As Integer = Me.dgvData.SelectedRows(0).Cells("Student ID").Value'OPEN CONNECTIONIf Not OleConn.State = ConnectionState.Open ThenOleConn.Open()End If'DELETE DATADim OleCmd As New OleDb.OleDbCommandOleCmd.Connection = OleConnOleCmd.CommandText = "DELETE FROM StudData WHERE StudID=" & intStudIDOleCmd.ExecuteNonQuery()'REFRESH DATAMe.RefreshData()'CLOSE CONNECTIONOleConn.Close()End IfEnd IfEnd Sub
Student Registration System in VB.Net – Coding the Program
Reviewed by code-dev
on
12:36 AM
Rating:
where does the code below go?
ReplyDeletePrivate 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()