Advanced Login System

Introduction

In this article we will show you how to create Registration Form using Windows Application in Visual Studio using C#.

Prerequisites

Visual Studio 2010/2012/2013/15/17, SQL Server 2005/08/2012

Project used version

VS2017, SQL SERVER 2012

Registration Form
Double click on DataClasses.dbml (LINQ to SQL file) in Solution explorer. Drag and drop tables and stored procedures as shown below.

 Advance Login System-User Role Relation

Add new item Window Form and name it as "frmRegistration".

Advance Login System Registration 

 

Add the following items as shown below.

Control Name  Text  Name
Label UserName lblUserName
Label Password lblPassword
Label Email lblEmail
Label Role lblRole
Label Question lblQuestion
Label Answer lblAnswer
Text Box   txtUserName
Text Box   txtPassword
Text Box   txtEmail
    cobRole
    cobQuestion
Text Box   txtAnswer
Button Register btnRegister
Group Box Registration grbReistrations

 

Paste follwing code to populate Roles in combo box. 


private void Registration_Load(object sender, EventArgs e)
        {
            //Load combobox from database using LINQ TO SQL CLASSES
            using (DataClassesDataContext db = new DataClassesDataContext())
            {
                cobRole.Items.Clear();
                cobRole.DataSource = db.Roles.ToList();
                cobRole.DisplayMember = "RoleName";
                cobRole.ValueMember = "RoleID";
                cobRole.SelectedValue = "";
            }
        }

Below example shows populated roles from database. 

Advance Login System-Show-Roles

Declare variable result as public string. The result value populated in Message Box.

 Advance Login System-Declare-Variable.jpg

Paste the follwing code in "btnRegistration"(Register Button) event

First of all add define "RegularExpression" class at topof you "frmRegistration" coding window.

 
using System.Text.RegularExpressions;
 
private void BtnRegistration_Click(object sender, EventArgs e)
        {
            //Insert Registration details in database
            //Validating Input values
            Regex rEmail = new Regex(@"^[a-zA-Z][\w\.-]*[a-zA-Z0-9]@[a-zA-Z0-9][\w\.-]*[a-zA-Z0-9]\.[a-zA-Z][a-zA-Z\.]*[a-zA-Z]$");
            if (!rEmail.IsMatch(txtEmail.Text.Trim()) ||
                txtUserName.Text.Trim() == "" ||
                txtPassword.Text.Trim() == "" ||
                cobRole.SelectedValue.ToString() == "" ||
                cobQuestion.Text.Trim() == "" ||
                txtAnswer.Text.Trim() == "")
            {
                MessageBox.Show("Fill all the information or Check Email address");
            }
            else
            {
                // accessing spRegister stored procedure and return result
                try
                {
                    using (DataClassesDataContext db = new DataClassesDataContext())
                    {
                        db.spRegister(txtUserName.Text, txtPassword.Text, txtEmail.Text, Convert.ToInt32(cobRole.SelectedValue.ToString()), cobQuestion.Text, txtAnswer.Text, ref result);
                        db.SubmitChanges();
                    }
                    // Result shown in Meassage Box
                    MessageBox.Show(result, "Information",
                        MessageBoxButtons.OK,
                        MessageBoxIcon.Information,
                        MessageBoxDefaultButton.Button1,
                        MessageBoxOptions.RightAlign);
                    Close();
                }
                catch (Exception ex)
                {
                    // Returns if any error 
                    MessageBox.Show(ex.Message);
                }
            }
        }

 

Complete code


using System;
using System.Linq;
using System.Windows.Forms;
using System.Text.RegularExpressions;

namespace AdvancedLoginSystem
{
    public partial class frmRegistration : Form
    {
        // Declare Result variable as string
        public string result;
        public frmRegistration()
        {
            InitializeComponent();
        }
        private void BtnRegistration_Click(object sender, EventArgs e)
        {
            //Insert Registration details in database
            //Validating Input values
            Regex rEmail = new Regex(@"^[a-zA-Z][\w\.-]*[a-zA-Z0-9]@[a-zA-Z0-9][\w\.-]*[a-zA-Z0-9]\.[a-zA-Z][a-zA-Z\.]*[a-zA-Z]$");
            if (!rEmail.IsMatch(txtEmail.Text.Trim()) ||
                txtUserName.Text.Trim() == "" ||
                txtPassword.Text.Trim() == "" ||
                cobRole.SelectedValue.ToString() == "" ||
                cobQuestion.Text.Trim() == "" ||
                txtAnswer.Text.Trim() == "")
            {
                MessageBox.Show("Fill all the information or Check Email address");
            }
            else
            {
                // accessing spRegister stored procedure and return result
                try
                {
                    using (DataClassesDataContext db = new DataClassesDataContext())
                    {
                        db.spRegister(txtUserName.Text, txtPassword.Text, txtEmail.Text, Convert.ToInt32(cobRole.SelectedValue.ToString()), cobQuestion.Text, txtAnswer.Text, ref result);
                        db.SubmitChanges();
                    }
                    // Result shown in Meassage Box
                    MessageBox.Show(result, "Information",
                        MessageBoxButtons.OK,
                        MessageBoxIcon.Information,
                        MessageBoxDefaultButton.Button1,
                        MessageBoxOptions.RightAlign);
                    Close();
                }
                catch (Exception ex)
                {
                    // Returns if any error 
                    MessageBox.Show(ex.Message);
                }
            }
        }

        private void Registration_Load(object sender, EventArgs e)
        {
            //Load combobox from database using LINQ TO SQL CLASSES
            using (DataClassesDataContext db = new DataClassesDataContext())
            {
                cobRole.Items.Clear();
                cobRole.DataSource = db.Roles.ToList();
                cobRole.DisplayMember = "RoleName";
                cobRole.ValueMember = "RoleID";
                cobRole.SelectedValue = "";
            }
        }
    }
}

Run The Program 

Login as admin, from admin panel go to master option and select "User registration".

Advance Login System-Admin Master 

If we tried with existing email id then returns "Email Id already exist!!"

Advance Login System-Registration-Email-already-exists 

Try register with different from existing email id then returns "Successfully registred!!"

Advance Login System-Registration Filling

Advance Login System-Registration-Successfully Registration MSG

 

Download Complete Source Code C#

 

 

Video Tutorial