Google+ Followers

Connect

Get Updates Via Email

Enter your email address:

Delivered by FeedBurner

Monday, 8 July 2013

Login and Registration forms in C# windows application with Back end Microsoft SQL Server for data access


In this article, I'm gonna share about how to make login and register form with MS SQL database;

1. Flow Chart Logic





















2. Normal Features




3. Form Designs
Login Form Design


Sign in Form Design



Password Retrieve Form



4. Database Design and SQL queries and Stored Procedure

Create new Database as "schooldata"
create table registerdata
(
 ID int identity,
 Username nvarchar(100),
 Password nvarchar(100),
 Fullname  nvarchar(100),
 MobileNO nvarchar(100),
 EmailID nvarchar(100) 
)

select * from registerdata

create procedure regis
(
 @Username as nvarchar(100),
 @Password as nvarchar(100),
 @Fullname as nvarchar(100),
 @MobileNO as nvarchar(100),
 @EmailID as nvarchar(100) 
)
as begin
insert into registerdata (Username, Password, Fullname, MobileNO,EmailID)
values (@Username, @Password, @Fullname, @MobileNO, @EmailID)
end


5. App.config Settings

<?xml version="1.0" encoding="utf-8" ?>
<configuration>

  <configSections>
  </configSections>
  <connectionStrings>
    <add name="ConnData"
        connectionString="Data Source=localhost\sqlexpress;Initial Catalog=Schooldata;Integrated Security=True"
        providerName="System.Data.SqlClient" />
  </connectionStrings>
  
</configuration>


6. SignIn Form Coding

 using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.Sql;
using System.Data.SqlClient;
using System.Configuration;
using System.Net;
using System.Collections.Specialized;

namespace SchoolData
{
    public partial class LoginForm : Form
    {
        public LoginForm()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            SigninForm sf = new SigninForm();
            sf.Show();
            this.Hide();
        }
        private void button2_Click(object sender, EventArgs e)
        {
            if ((textBox1.Text != "") && (textBox2.Text != ""))
            {
                string constring = ConfigurationManager.ConnectionStrings["ConnData"].ConnectionString;
                SqlConnection con = new SqlConnection(constring);
                string q = "select * from registerdata where Username = @Username and Password = @Password ";
                SqlCommand cmd = new SqlCommand(q, con);
                cmd.Parameters.AddWithValue("@Username", this.textBox1.Text);
                cmd.Parameters.AddWithValue("@Password", this.textBox2.Text);
                con.Open();
                SqlDataReader dr = cmd.ExecuteReader();
                while (dr.Read())
                {
                    if (dr.HasRows == true)
                    {
                        MessageBox.Show("Login Successfully Done");
                    }
                }
                if (dr.HasRows == false)
                { MessageBox.Show("Access Denied, password username mismatched"); }
            }
            else { MessageBox.Show("Enter username and password"); }
        }

        private void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            RetrievePwdForm rf = new RetrievePwdForm();
            rf.Show();
            this.Hide();
        }
        
    }
}


7. Register Form

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.Sql;
using System.Data.SqlClient;
using System.Configuration;
using System.Net;
using System.Collections.Specialized;

namespace SchoolData
{
    public partial class SigninForm : Form
    {
        public SigninForm()
        {
            InitializeComponent();
        }

        private void label2_Click(object sender, EventArgs e)
        {

        }

        private void label4_Click(object sender, EventArgs e)
        {

        }
        //checking whether username already exist
        public void UserNameCheck()
        {
            string constring = ConfigurationManager.ConnectionStrings["ConnData"].ConnectionString;
            SqlConnection con = new SqlConnection(constring);
            SqlCommand cmd = new SqlCommand("Select * from registerdata where Username= @Username", con);
            cmd.Parameters.AddWithValue("@Username", this.textBox1.Text);
            con.Open();
            SqlDataReader dr = cmd.ExecuteReader();
            while (dr.Read())
            {
                if (dr.HasRows == true)
                {
                    MessageBox.Show("Username = "+ dr[1].ToString() +" Already exist");
                    break;
                }
            }
            //return textBox1.Text  ;
        }
        // clear all values in textbox
        public void clearall()
        {
            textBox1.Text = " ";
            textBox2.Text = " ";
            textBox3.Text = " ";
            textBox4.Text = " ";
            textBox5.Text = " ";

        }
        //checking whether mobile number already exist
        public void MobileNOCheck()
        {
            string constring = ConfigurationManager.ConnectionStrings["ConnData"].ConnectionString;
            SqlConnection con = new SqlConnection(constring);
            SqlCommand cmd = new SqlCommand("Select * from registerdata where MobileNO= @MobileNO", con);
            cmd.Parameters.AddWithValue("@MobileNO", this.textBox4.Text);
            con.Open();
            SqlDataReader dr = cmd.ExecuteReader();
            while (dr.Read())
            {
                if (dr.HasRows == true)
                {
                    MessageBox.Show("MobileNO = " + dr[4].ToString() + "is Already exist");
                    break;
                }
            }
            //return textBox4.Text;
        }
        public void datastore()
        {

            string Fname, Sname, Fullname, MobileNO, EmailID;
            Fname = textBox1.Text;
            Sname = textBox2.Text;
            Fullname = textBox3.Text;
            MobileNO = textBox4.Text;
            EmailID = textBox5.Text;
            try
            {
                string constring = ConfigurationManager.ConnectionStrings["ConnData"].ConnectionString;

                /* Declaring Connection Variable */
                SqlConnection con = new SqlConnection(constring);

                /* Checking Connection is Opend or not If its not open the Opens */
                if (con.State != ConnectionState.Open)
                    con.Open();

                /* Calling Stored Procedure as SqlCommand */
                SqlCommand cmd = new SqlCommand("regis", con);
                cmd.CommandType = CommandType.StoredProcedure;

                /* Passing Input Parameters with Command */
                cmd.Parameters.AddWithValue("@Username", Fname);
                cmd.Parameters["@Username"].Direction = ParameterDirection.Input;

                cmd.Parameters.AddWithValue("@Password", Sname);
                cmd.Parameters["@Password"].Direction = ParameterDirection.Input;

                cmd.Parameters.AddWithValue("@Fullname", Fullname);
                cmd.Parameters["@Fullname"].Direction = ParameterDirection.Input;

                cmd.Parameters.AddWithValue("@MobileNO", MobileNO);
                cmd.Parameters["@MobileNO"].Direction = ParameterDirection.Input;

                cmd.Parameters.AddWithValue("@EmailID", EmailID);
                cmd.Parameters["@EmailID"].Direction = ParameterDirection.Input;

                /* Executing Stored Procedure */
                cmd.ExecuteNonQuery();
                con.Close();
                MessageBox.Show("Data Inserted Succesfully");
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
             
        }
        public void registerdata()
        {       datastore();
                this.Hide();
                LoginForm lf = new LoginForm();
                lf.Show();
            
        }
        private void button1_Click(object sender, EventArgs e)
        {
            registerdata();
        }

        private void textBox1_KeyDown(object sender, KeyEventArgs e)
        {
            //UserNameCheck();
            //clearall();
        }

        private void textBox4_KeyDown(object sender, KeyEventArgs e)
        {
            //MobileNOCheck();
            //clearall();
        }

        private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
        {
            //UserNameCheck();
            //clearall();
        }

        private void textBox4_KeyPress(object sender, KeyPressEventArgs e)
        {
            //MobileNOCheck();
            //clearall();
        }

        private void textBox4_KeyUp(object sender, KeyEventArgs e)
        {
            //MobileNOCheck();
            //clearall();
        }
        private void textBox1_KeyUp(object sender, KeyEventArgs e)
        {
            //UserNameCheck();
            //clearall();
        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {
            UserNameCheck();
           
        }

        private void textBox4_TextChanged(object sender, EventArgs e)
        {
           // MobileNOCheck();
           
        }

        private void textBox4_TextChanged_1(object sender, EventArgs e)
        {
            MobileNOCheck();
        }
    }
}

8. Retrieve Password Form

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.Sql;
using System.Data.SqlClient;
using System.Configuration;
using System.Net;
using System.Collections.Specialized;

namespace SchoolData
{
    public partial class RetrievePwdForm : Form
    {
        public RetrievePwdForm()
        {
            InitializeComponent();
        }

        private void label1_Click(object sender, EventArgs e)
        {

        }

        private void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            this.Hide();
            LoginForm lf = new LoginForm();
            lf.Show();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            string constring = ConfigurationManager.ConnectionStrings["ConnData"].ConnectionString;
            SqlConnection con = new SqlConnection(constring);
            SqlCommand cmd = new SqlCommand("Select * from registerdata where MobileNO= @MobileNO", con);
            cmd.Parameters.AddWithValue("@MobileNO", this.textBox1.Text);
            con.Open();
            SqlDataReader dr = cmd.ExecuteReader();
            while (dr.Read())
            {
                if (dr.HasRows == true)
                {
                    string str = "Your Password = ";
                    label2.Text=str +  dr[2].ToString();
                }
            }
        }
    }
}

Output


a) Register New User and store into database






b) Check Username already Exist

C) Check Mobile Number Already Exist


D) Forget Password



Finally Login Successfully done



















14 comments :

  1. shows error in the name 'ConfigurationManager' does not exist in the current context

    ReplyDelete
  2. Thank you very much! (^_^)
    It's so useful for me to learn. Thanks :)

    ReplyDelete
  3. If I continue to click Register Button in SinginForm even though it announces error:"Both Username and MobileNO are already exist!",
    it's still save datas into database and it is "Polymeric Datas about Username and MobileNO". Do you have solutions to resolve it? Please, help me!

    ReplyDelete
  4. Thank you very much! (^_^)
    It's so useful for me to learn. Thanks :)

    ReplyDelete
  5. is this necessary to add configuration in app config?

    ReplyDelete
  6. I have read your blog its very attractive and impressive. I like it your blog.

    Java Online Training Java EE Online Training Java EE Online Training Java 8 online training Core Java 8 online training

    Java Online Training from India Java Online Training from India Core Java Training Online Core Java Training Online Java Training InstitutesJava Training Institutes

    ReplyDelete
  7. when I register then showing
    configuration system failed to initialize

    ReplyDelete

Share this to your friends