C# MS Access System Concpets => Student Details - Save,Validate

c# crud

Our aim is to see how to make a student registration system from scratch.We shall be looking at various concepts and making it easy so that beginners can know at least how to start when you want to make say a student registration system.Today we start by looking at student details validation and saving/inserting into MS Access database. We check errors using ErrorProvider and use metro theme for nice user interface. [toc]

Intro

  • Beginner C# tutorial right here.
  • The aim is to exlpore concepts when making a student management system.
  • Today we see at the concept of validation and data entry.
  • The database is MS Access and we insert data from textboxes.
  • First we of course validate that data and only insert when its correct.
  • we use errorprovider to show errors and see how to clear those errors,and attach it to the specific components.
  • We've used Visual Studio as our IDE.
  • The source is attached above for reference.
  • The code is well commented for easier understanding.

Common Questions we answer

With this simple example we explore the following :

  • How to insert/save data to MS Access database in C#.
  • How to validate data before entering into access database.
  • How to use errorprovider.
  • How to make a student registration form with C#.
  • How to clear errors in errorprovider.
  • How to connect to MS Access access database.

Tools Used

  • IDE : Visual Studio 2013
  • OS : Windows 8.1
  • LANGUAGE : C#
  • PLATFORM : .NET
  • DATABASE : MS Access

 

Source Code

Student.cs

  • Our POCO class
  • Represents a single student with properties like name.

 

using System;

namespace Enter_Student_Details
{
    class Student
    {
        private string firstname, middlename, lastname;
        private DateTime dob;

        public string Firstname
        {
            get { return firstname; }
            set { firstname = value; }
        }

        public string Middlename
        {
            get { return middlename; }
            set { middlename = value; }
        }

        public string Lastname
        {
            get { return lastname; }
            set { lastname = value; }
        }

        public DateTime Dob
        {
            get { return dob; }
            set { dob = value; }
        }
    }
}

     

DBClass

  • Our database class.
  • Contains connection string to help us connect to database.
  • Database of choice today is MS Access.
  • Also contains method for inserting into access database.

 

using System;
using System.Data;
using System.Data.OleDb;
using System.Windows.Forms;

namespace Enter_Student_Details
{
    class DBClass
    {
        private const string conString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:/Users/Hp/Documents/DataBases/priDB.mdb;";
        readonly OleDbConnection con = new OleDbConnection(conString);
        OleDbCommand cmd;
        OleDbDataAdapter adapter;
        DataTable dt = new DataTable();

        public bool registerStudent(Student student)
        {
            //SQL STMT
            const string sql = "INSERT INTO studentsTB(firstname,middlename,lastname,dob) VALUES(@FNAME,@MNAME,@LNAME,@DOB)";
            cmd = new OleDbCommand(sql, con);

            //ADD PARAMS
            cmd.Parameters.AddWithValue("@FNAME", student.Firstname);
            cmd.Parameters.AddWithValue("@MNAME", student.Middlename);
            cmd.Parameters.AddWithValue("@LNAME", student.Lastname);
            cmd.Parameters.AddWithValue("@DOB", student.Dob.ToShortDateString());

            //OPEN CON AND EXEC
            try
            {
                con.Open();

                if (cmd.ExecuteNonQuery() > 0)
                {

                    return true;

                }

                con.Close();

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            return false;
        }

    }
}

     

Form1 class

  • Our main gui class.
  • We handle our components events here.

 

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using MetroFramework;
using MetroFramework.Forms;

namespace Enter_Student_Details
{
    public partial class Form1 : MetroForm
    {
        public Form1()
        {
            InitializeComponent();
            dateLabel.Text = "Today : "+DateTime.Today.ToShortDateString();
        }

        //REGISTER ENTERED STUDENT
        private void register()
        {
            Student student = new Student
            {
                Firstname = txtFirstName.Text,
                Middlename = txtMiddleName.Text,
                Lastname = txtLastName.Text,
                Dob = dobDateTime.Value
            };

            DBClass db=new DBClass();
            if (db.registerStudent(student))
            {
                resetTxtx();
                MetroMessageBox.Show(this, "Successfully Registered " + student.Firstname);

            }
        }

        //RESET INPUT TEXTBOXES
        private void resetTxtx()
        {
            txtFirstName.Text = "";
            txtMiddleName.Text = "";
            txtLastName.Text = "";
            dobDateTime.Value = DateTime.Today;
        }

        //VALIDATE DATA
        private bool validateStudentDetails()
        {
            if (txtFirstName.Text == "")
            {
                errorProvider1.SetError(txtFirstName,"Please enter FirstName");
            }
            else if (txtLastName.Text == "")
            {
                errorProvider1.SetError(txtLastName, "Please enter LatsName");
            }
            else
            {
                return true;
            }
            return false;
        }

        //REGISTER BUTTON CLICKED
        private void registerBtn_Click(object sender, EventArgs e)
        {
            if (validateStudentDetails())
            {
                register();
            }
        }

        private void txtFirstName_TextChanged(object sender, EventArgs e)
        {

        }

        private void txtLastName_TextChanged(object sender, EventArgs e)
        {

        }

        private void clearErrorsLink_Click(object sender, EventArgs e)
        {
            errorProvider1.Clear();
        }

    }
}

     

How To Run

  • Just download the project above.
  • Open the Visual Studio.
  • Close any open project.
  • Import it into visual studio.
  • Open Microsoft Access and create a database and a table.
  • Save it with .mdb extension,with 2003-2007 format if possible.
  • Create a table.
  • Go to DBClass and specify the path to that database file.
  • Make sure your database columns correspond to the properties of our Student POCO class.
  • Thats it.
  • If you are not sure of how to do the above then you need to watch our video tutorial below.

Results

  • Here's what we get on running the project :

MS Access C# Validate Student Details

  • Access Database Table

MS Access Student Details Table

More

YouTube

  • Visit our channel for more examples like these.

Facebook

Oclemy,Cheers.

How do You Feel after reading this?

According to scientists, we humans have 8 primary innate emotions: joy, acceptance, fear, surprise, sadness, disgust, anger, and anticipation. Feel free to tell us how you feel about this article using these emotes or via the comment section. This feedback helps us gauge our progress.

Help me Grow.

I set myself some growth ambitions I desire to achieve by this year's end regarding this website and my youtube channel. Am halfway. Help me reach them by:




Recommendations


What do You Think


Previous Post Next Post