Creating Login Form With SQL Server in Windows Forms

Here I will explain how to create a login form with SQL Server. Before this I explained Creating Login Form with SQL Server in Window Form.

Step 1: login form

Drag and down two Labels (for username and password) and two text boxes and a Button (for submit) onto a Window Forms form and change their name to "usrtxt" and "passtxt".

username and password

Step 2: Database table

database table
Step 3: The code for the button click of the button is as in the following:

button click

using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Text;

using System.Windows.Forms;

using System.Data.SqlClient;

 

namespace First_Csharp_app

{

    public partial class Form1 : Form

    {

        public Form1()

        {

            InitializeComponent();

        }

 

        private void button1_Click(object sender, EventArgs e)

        {

            try

            {

                if (!(usertxt.Text == string.Empty))

                {

                    if (!(passtxt.Text == string.Empty))

                    {

                        String str = "server=MUNESH-PC;database=windowapp;UID=sa;password=123";

                        String query = "select * from data where username = '" + usertxt.Text + "'and password = '" + this.passtxt.Text + "'";

                        SqlConnection con = new SqlConnection(str);

                        SqlCommand cmd = new SqlCommand(query, con);

                        SqlDataReader dbr;

                        con.Open();

                        dbr = cmd.ExecuteReader();

                        int count = 0;

                        while (dbr.Read())

                        {

                            count = count + 1;

                        }

                        if (count == 1)

                        {

                            MessageBox.Show("username and password is correct");

                        }

                        else if (count > 1)

                        {

                            MessageBox.Show("Duplicate username and password", "login page");

                        }

                        else

                        {

                            MessageBox.Show(" username and password incorrect", "login page");

                        }

                    }

                    else

                    {

                        MessageBox.Show(" password empty", "login page");

                    }

                }

 

                else

                {

                    MessageBox.Show(" username empty", "login page");

                }

                // con.Close();

 

            }

            catch (Exception es)

            {

                MessageBox.Show(es.Message);

 

            }

        }

    }

}

Step 4: Run your application

If the username and/or password is not correct:

Run your application

If the username and password are correct:

username and password is correct 

Up Next
    Ebook Download
    View all
    Learn
    View all