Login and logout in C# Windows Application

In this C# tutorial, we will see how to create a simple login and logout application in C# Windows Application.

The basic working :

Create a windows application in C# that will take user input on the login page which is connected with our database login table, and check the input that they are matching or not if yes it will take us to the main page of the application and on the main page there we will create one logout button by clicking on that it will redirect us to the login page again. So now we know that we have to create

  • One SQL database table.
  • And two forms in our project of C# windows application.

Login and logout in C# Windows Application

Let’s start with the following steps

Step 1:

First we will create one SQL database in MySQL workbench > Open your MySQL workbench > create new database with any name you want I gave my database name as My_Database > and then we have to make one table in it with the name login_tbl here we will take two columns which are username and password give their data type as varchar (20) and varchar (10) respectively, click on apply for the script > and fill some data in the table in both the columns > and Save it.

Step 2:

Now we will create a windows application for it, Open your visual studio > go to File menu > click on New project > in visual C# select Windows Form Application > give your project name I have given its name as LoginApplication > click on Ok.

If you want to change your form 1 name then change it I have given my form 1 name as loginform. On loginform take two labels, two Text boxes, and one button on it. Give the label’s name as username and password and the button name as Login.

Step 3:

Now we will add one more form to our application and name it as a mainform which will be displayed when we will fill in the correct username and password in the login page which will match with our database table. I have only taken one button on it and named it Logout because I am here showing the logout code and working on it, if you want to design more things on the main form you can do that.

Step 4

Now we have completed all the designing part of our application so let’s move to the coding part of it. Here we have to add System.Data and System.Data.SqlClient namespace. Here also we have to double click on the login button which will create a button click event write the below code on loginform.cs.

loginform.cs
using System;
using System.Data;
using System.Windows.Forms;
using System.Data.SqlClient;
namespace LoginApplication
{
public loginform()
{
InitializeComponent();
}

//Connection String
string cs = @"Data Source=(LocalDB)\v11.0;
AttachDbFilename=|DataDirectory|\My_Database.mdf;
Integrated Security=True;";
//btn_Login Click event
private void button1_Click(object sender, EventArgs e)
{
if(txt_UserName.Text=="" || txt_Password.Text=="")
{
MessageBox.Show("Please provide UserName and Password");
return;
}
try
{

//Create SqlConnection
SqlConnection con = new SqlConnection(cs);
SqlCommand cmd = new SqlCommand("Select * from login_tbl where UserName=@username and Password=@password",con);
cmd.Parameters.AddWithValue("@username",txt_UserName.Text);
cmd.Parameters.AddWithValue("@password", txt_Password.Text);
con.Open();
SqlDataAdapter adapt = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
adapt.Fill(ds);
con.Close();
int count = ds.Tables[0].Rows.Count;

//If count is equal to 1, than show mainform
if (count == 1)
{
MessageBox.Show("Login Successful!");
this.Hide();
mainform fm = new mainform();
fm.Show();
}
else
{
MessageBox.Show("Login Failed!");
}
}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
}
}
}
}

Step 5:

Now go to mainform.cs file and click on the logout button to create a click event, add the below code there. By clicking on the logout button the event will close the main form and redirect us to login form.

Mainform.cs
using System;
using System.Windows.Forms;
namespace LoginApplication{
public partial class mainform:
Form{
public mainform()
{
InitializeComponent();
}
//btn_LogOut Click Event
private void button1_Click(object sender, EventArgs e)
{
this.Hide();
loginform fl = new loginform();
fl.Show();
}
private void mainform_FormClosing(object sender, FormClosingEventArgs e)
{
Application.Exit();
}
}
}

Step 6:

Now run the application first the login page will display to us then if we do not enter any data into Text boxes then it will show us warning that please fill username and password into it.

if we entered some data but if it’s not matching with our database table which is connected with our application so then it will show the warning “Login Failed!”.

When we fill the correct username and password and if it is present in our data table then by clicking on the login button

In above example I have entered the data it is already exists in my connected login_tbl which I have made before.That will show the message on screen in message box that is “Login Successful!”.

By clicking on the ok button of that message box,It will take us to the main form.

And there when we click on the logout button it will again take us back to the login page and stop showing the main page.

So in this way we can simply make the application which do the login and logout operation in C# Windows Application.