How to create login form in C#

                                     How to create login form in C#


create login form in csharp




Today I will explain How to create login form in C# and  In Application it is most important to create first form is login form because in many application is important to restrict the users to access with registered users it safety our record to maintained it.

And we create different profiles for different users to restricts access and structure are developed.

Only one person to view and do everything it's admin only

In this article we are going to learn and develop how to create a very simple login form using sql database .


First create Empty Window Application files:

>>1.Login form

>>2.Database connection

>>3.connect to database and execute Login form


1. Login form


login page




To strat with a new project and i named it RRstudio_finacial_application .Change the name as you wishes and change the name properties of class form1 to "Login" form This will be the main form which will load before the main form and user authenticated.

This will be the pop up every time the application execute.

drag and drop two labels for login username and password and two text boxes and two button for login and reset on the login form 

login page




As you wishes In password char properties of password Text box you can change "*" for hide the password char .Am not using this properties change because i have to show you how it's works.

2 .Database connection

login page




       





sql server







Am just using sql server management 2008R2 for database and database name finacial

create database finacial; - >click "Execute" or press (F5)

use finacial;  - >click "Execute" or press (F5)

Now you can see the top left corner database change finacial and you can drop down and choose the database 'finacial'

and then create a table and name it 'login':

create table login (username varchar(25) not null primary key, password varchar(25)not null);


sql server





sql server




Now we enter into main form codes:

and first add connection string to connect database



connection string


sqlconnection con=new sqlconnection("Data source="DESKTOP-19Q8E5J\\SQLEXPRESS";Initial catalog=finacial;Integrated security=true;");

I detailed Given How to connect SQL sever Database connect in this link >>How to connect SQL Server database in c#


3. connect to database and execute Login form

Here the Main Code :

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 System.Data.SqlClient;


namespace RRstudio_finacial_application

{

    public partial class Login : Form

    {


        SqlConnection con = new SqlConnection("Data Source=DESKTOP-19Q8E5J\\SQLEXPRESS;Initial Catalog=finacial;Integrated Security=True");

        public Login()

        {

            InitializeComponent();

        }


        private void label2_Click(object sender, EventArgs e)

        {


        }


        private void label3_Click(object sender, EventArgs e)

        {


        }


        private void button1_Click(object sender, EventArgs e)

        {


            if (textBox1.Text == "")

            {

                MessageBox.Show("please enter username", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);

                textBox1.Focus();

                return;

            }

            if (textBox2.Text == "")

            {

                MessageBox.Show("please enter username", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);

                textBox2.Focus();

                return;

            }

            try

            {

                con.Open();

                SqlDataAdapter sda = new SqlDataAdapter("select * from login where username='" + textBox1.Text + "' and password='" + textBox2.Text + "'", con);

                DataTable dt = new DataTable();

                sda.Fill(dt);

                if (dt.Rows[0][0].ToString() == "1")

                {

                    RR_Main rm = new RR_Main();

                    rm.Show();

                    this.Hide();

                }

                else { MessageBox.Show("please check user username and password"); }

                con.Close();

            }

            catch (Exception ex)

            {

                MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);

            }

        }

        private void button2_Click(object sender, EventArgs e)

        {

            textBox1.Clear();

            textBox2.Clear();

        }

    }

}


if you want to exit Application button and just add the three button and name it "Exit" and add the code below catch(Exception ex){

}

}

private void button3_Click(object sender, EventArgs e)

{

Application.Exit();

}

In this step we will learn How to create login form using in C#. The next time I will put some more interesting commands, and continue project thank you this is helpful for you will Like That's all, You can customize this code further as per your requirement. And please feel free to give comments on this tutorial.

                          

                                   <<User-account-activation-using-email in php>>

                                     <<Basic-Insert-View-Edit-Delete using php>>                                 

                                    <<Free-bootstrap-responsive-templates>>

                                    <<cool-free-bootstrap-responsive Template>>
 







Previous Post Next Post