Loading, please wait...

A to Z Full Forms and Acronyms

How to create a login form in Windows forms using ado.net

In this article we will learn how we can create a simple login form using WinForms with ado.net.

Let us try to figure out the simple concept that works behind the login Form.  We have the credentials of all users stored in a table and that table is connected with our form with the help of ado.net. When the user inputs any data in the text field of the form, that data is compared and matched with the table data to find out whether that particular data exists in the table or not.

This will be made more understandable with this example. We have created a table named ‘logIn_1’ in our database having the following given data i.e. User Id and Password of some users:

Now, to make a login form, start a new project in Windows Form Application(.Net Framework). You will come across a blank form. From the toolbox section, drag and drop controls which you think would be required. For example, here we have added two labels, two text boxes and one button for login event to take place.

From the solution explorer section, open ‘app.config’ the file where you have to add the connection string for providing access to the database. The code in the configuration file will look somewhat like this:

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

<configuration>
    <startup>
        <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.7.2" />
    </startup>
<connectionStrings>
       <add name = "myDB" connectionString= "Data Source=(LocalDB);AttachDbFilename=Database1;Integrated Security=True"/>
</connectionStrings>

</configuration>

 

Now again from the solution explorer, we will add a class that will contain all the functionalities required for the further connectivity of the form to the database. Also, we will add the DLL System.Configuration in this class so as to access ConfigurationManager class that will help in collecting the database information provided by the ConnectionString for the client application(here, login form).

We will also use a set of other classes like DataTable and SqlDataAdapter class that is contained in the DLL System.Data and System.Data.SqlClient. DataTable class is being used to represent one table from a Database(here, logIn_1 table) and the SqlDataAdapter class is used to provide communication between the DataSet and the SQL Database.

The code for the class (named SQLhelp) is given below:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;

namespace LogInForm
{
    class SQLhelp
    {
        public string ConnectionString
        {
            get
            {
                return ConfigurationManager.ConnectionStrings["myDB"].ConnectionString;
            }
        }
        DataTable userTable;
        SqlDataAdapter adapter;
        public DataTable GetTable(string query)
        {
            adapter = new SqlDataAdapter(query, ConnectionString);
            userTable = new DataTable();
            adapter.Fill(userTable);
            return userTable;
        }
       }
      }

Now, the final part comes where we will provide the functionality to the login button present in our form. We will also create an instance for the SQLhelp class in our code so that we can access the data stored in the SQL database in our program.

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 LogInForm
{

    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        SQLhelp sql = new SQLhelp();
        DataTable data;
        private void Form1_Load(object sender, EventArgs e)
        {
            data = sql.GetTable("select * from login_1");
        }

        private void btnSubmit_Click(object sender, EventArgs e)
        {
            data = sql.GetTable("select * from login_1 where userId='" + txtuserId.Text + "' and Password='" + txtPassword.Text + "'");
            if (data.Rows.Count == 1)
            {
                MessageBox.Show("login successful");
            }
            else
            {
                MessageBox.Show("invalid username or password");
            }
        }
    }
}

 

Explanation: as soon as the form is loaded, the query is passed on and the data from the table is brought into the SQL database of the application.

When the user enters a certain username and password in the text field, it is matched with the data in the table and the result is generated as per the accordance of the data being matched and then being found or not.

When correct data is filled in the form:

When incorrect data is filled in the form:

A to Z Full Forms and Acronyms

Related Article