C# Login form design

Database table:

database-table

Form Design :

database-table

Application configuration file(App.config) :

If you want to create "App.config" file then you press this shortcut key "Crtl+Shift+A" then you choose the Applicatiion configuration file(App.config)

<?xml version="1.0" encoding="utf-8" ?> 
<configuration>
<connectionStrings>
<add name="DBCS" connectionString="data source=.;database=dbSample;integrated security=SSPI" providerName="System.Data.SqlClient"/> 
</connectionStrings>
</configuration>

Note :-

  • data source: you enter your own sql server name(if you use local server then you use(.))
  • database : you enter your own database name
  • integrated security=SSPI means this is use windows authentication. If you use sql server authentication then you use the (Username=_yourusername;Password=_youpassword) then you replace in intergrated security portion.
  • Add the System.Configuration reference. if you don't know how to adding references right click your project folder references => add reference => select .Net => select System.Configuration => click OK

Form1.cs file:

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

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

        private void btnLogin_Click(object sender, EventArgs e)
        {
            Calling the LoginCheck() function and return value hold on id. Send the textbox username and textbox password text.
            int id = LoginCheck(txtusername.Text, txtpassword.Text);
            if (id > 0)
            {
                MessageBox.Show("Login Successfully");
            }
            else
            {
                MessageBox.Show("Invalid username and password");
            }
        }
        private int LoginCheck(string username, string password)
        {
            int id = 0;
           //retrieve the connection string in App.config file,the DBCS is the name of connection string
            string cs = ConfigurationManager.ConnectionStrings["DBCS"].ConnectionString;
           //send the connection string in SqlConnection Class
            SqlConnection con = new SqlConnection(cs);
            //creating command for check login valid or invalid
            string command = "select id from tblLogin where username='"+username+"' and password='"+password+"'";
            //command and SqlConnection object pass through the SqlCommand
            SqlCommand cmd = new SqlCommand(command, con);
            try
            {
              //open the database connection
                con.Open();
                //Execute the cmd and getting id value
                id = Convert.ToInt32(cmd.ExecuteScalar());
                //dispose the cmd 
                cmd.Dispose();
            }
            catch (Exception ex)
            {
                //if any exception is occurred then show the message box
                MessageBox.Show(ex.Message);
            }
            finally
            {
                //close the con
                con.Close();
            }
            //return id
            return id;
        }
    }
}

Note :

  • cmd.ExecuteScalar() : Executes the query, return the first column of the first row in the result set returned by the query. Additional column or rows are ingnored.
  • cmd.Dispose() : Releases all resources by the System.ComponetModel.Component
  • con.Close() : You always close the database connection in finally block.

Form1.Desinger.cs file:

namespace LoginWindowsFormProject
{
    partial class Form1
    {
        /// <span><</span>summary<span>></span>
        /// Required designer variable.
        /// <span><</span>/summary<span>></span>
        private System.ComponentModel.IContainer components = null;

        /// <span><</span>summary<span>></span>
        /// Clean up any resources being used.
        /// <span><</span>/summary<span>></span>
        /// <span><</span>param name="disposing"<span>></span>true if managed resources should be disposed; otherwise, false.<span><</span>/param<span>></span>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        #region Windows Form Designer generated code

        /// <span><</span>summary<span>></span>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// <span><</span>/summary<span>></span>
        private void InitializeComponent()
        {
            this.label1 = new System.Windows.Forms.Label();
            this.lblname = new System.Windows.Forms.Label();
            this.txtusername = new System.Windows.Forms.TextBox();
            this.txtpassword = new System.Windows.Forms.TextBox();
            this.lblpassword = new System.Windows.Forms.Label();
            this.btnLogin = new System.Windows.Forms.Button();
            this.SuspendLayout();
            // 
            // label1
            // 
            this.label1.AutoSize = true;
            this.label1.Font = new System.Drawing.Font("Microsoft Sans Serif", 16F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.label1.Location = new System.Drawing.Point(168, 31);
            this.label1.Name = "label1";
            this.label1.Size = new System.Drawing.Size(65, 26);
            this.label1.TabIndex = 0;
            this.label1.Text = "Login";
            // 
            // lblname
            // 
            this.lblname.AutoSize = true;
            this.lblname.Font = new System.Drawing.Font("Microsoft Sans Serif", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.lblname.Location = new System.Drawing.Point(102, 97);
            this.lblname.Name = "lblname";
            this.lblname.Size = new System.Drawing.Size(65, 15);
            this.lblname.TabIndex = 1;
            this.lblname.Text = "Username";
            // 
            // txtusername
            // 
            this.txtusername.Font = new System.Drawing.Font("Microsoft Sans Serif", 10F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.txtusername.Location = new System.Drawing.Point(105, 116);
            this.txtusername.Name = "txtusername";
            this.txtusername.Size = new System.Drawing.Size(193, 23);
            this.txtusername.TabIndex = 2;
            // 
            // txtpassword
            // 
            this.txtpassword.Font = new System.Drawing.Font("Microsoft Sans Serif", 10F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.txtpassword.Location = new System.Drawing.Point(105, 173);
            this.txtpassword.Name = "txtpassword";
            this.txtpassword.Size = new System.Drawing.Size(193, 23);
            this.txtpassword.TabIndex = 4;
            this.txtpassword.UseSystemPasswordChar = true;
            // 
            // lblpassword
            // 
            this.lblpassword.AutoSize = true;
            this.lblpassword.Font = new System.Drawing.Font("Microsoft Sans Serif", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.lblpassword.Location = new System.Drawing.Point(102, 155);
            this.lblpassword.Name = "lblpassword";
            this.lblpassword.Size = new System.Drawing.Size(61, 15);
            this.lblpassword.TabIndex = 3;
            this.lblpassword.Text = "Password";
            // 
            // btnLogin
            // 
            this.btnLogin.Font = new System.Drawing.Font("Microsoft Sans Serif", 9F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.btnLogin.Location = new System.Drawing.Point(235, 216);
            this.btnLogin.Name = "btnLogin";
            this.btnLogin.Size = new System.Drawing.Size(63, 28);
            this.btnLogin.TabIndex = 5;
            this.btnLogin.Text = "Login";
            this.btnLogin.UseVisualStyleBackColor = true;
            this.btnLogin.Click += new System.EventHandler(this.btnLogin_Click);
            // 
            // Form1
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(389, 304);
            this.Controls.Add(this.btnLogin);
            this.Controls.Add(this.txtpassword);
            this.Controls.Add(this.lblpassword);
            this.Controls.Add(this.txtusername);
            this.Controls.Add(this.lblname);
            this.Controls.Add(this.label1);
            this.Name = "Form1";
            this.Text = "LoginForm";
            this.ResumeLayout(false);
            this.PerformLayout();

        }

        #endregion

        private System.Windows.Forms.Label label1;
        private System.Windows.Forms.Label lblname;
        private System.Windows.Forms.TextBox txtusername;
        private System.Windows.Forms.TextBox txtpassword;
        private System.Windows.Forms.Label lblpassword;
        private System.Windows.Forms.Button btnLogin;
    }
}

Output

Output of login page

Write a comment


Name Email Message