In this post i am going to explain a simple login application in windows c# . In this App we will cover few concepts like login concept with database connection, working with multiple forms in windows c# ,  passing values from one form to another form , adding local database to our windows c# project.
For this i am designing two forms one is for Login and another is for displaying Welcome Message for logged in user.
The screenshots of application shown below .
CSharp winforms login app_1
C# winforms login application

We are designing the two forms as shown above . and we are designing a table LoginDetails in LoginDB , which is a Local Database added to our application .

Table : LoginDetails

C#.Net login application table

Sql query to create table :

CREATE TABLE LOGINDETAILS( NAME VARCHAR(25),PASSWORD NVARCHAR(25))

Now the C# code of Form1 is like this

using System;
using System.Windows.Forms;
using System.Data;
using System.Data.SqlClient;
 
namespace LoginApplication
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        string msg;
        //SqlConnection con = new SqlConnection("Data Source=LocalHost;Integrated Security=true;Initial Catalog=sameer");
       
        /** the connection string below is for connecting localdatabase LoginDB.mdf exists in the project **/
        SqlConnection con = newSqlConnection("Data Source=.\SQLExpress;User Instance=true;Integrated Security=true;AttachDbFilename=|DataDirectory|lOGINdb.mdf");
       
        SqlCommand cmd;
        DataSet ds = new DataSet();
        private voidbtnlogin_Click(object sender, EventArgs e)
        {
 
            try
            {
                if (txtname.Text == ""|| txtpassword.Text == "")
                {
                    MessageBox.Show(" Enter UserName and Password .");
                    return;
                }
 
                cmd = new SqlCommand("SELECT * FROM LoginDETAILS where Name='"+ txtname.Text + "' and Password='"+ txtpassword.Text + "'", con);
                SqlDataAdapter da = newSqlDataAdapter(cmd);
                da.Fill(ds);
                int i = ds.Tables[0].Rows.Count;
                if (i == 1)
                {
                    msg = "Welcome " + txtname.Text;
                    this.Hide();
                    Form2 f2 = newForm2(msg);
                    f2.Show();
                    ds.Clear();
 
                }
                else
                {
                    MessageBox.Show("Not Registered User or Invalid Name/Password");
                    txtpassword.Text = "";
                }
            }
            catch(Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
 
        }
 
        private voidbtnRegister_Click(object sender, EventArgs e)
        {
            try
            {
 
                if (txtname.Text == ""|| txtpassword.Text == "")
                {
                    MessageBox.Show(" Enter UserName and Password .");
                    return;
                }
 
                /** checking whether name exists **/
                cmd = new SqlCommand("SELECT * FROM LoginDETAILS where Name='"+ txtname.Text + "'", con);
                SqlDataAdapter da = newSqlDataAdapter(cmd);
                da.Fill(ds); //filling dataset
                int i = ds.Tables[0].Rows.Count; //checking rows count in dataset
                if (i > 0)
                {
                    MessageBox.Show("UserName "+txtname.Text+" Already Exists..");
                    txtpassword.Text = "";
                    ds.Clear(); //clearing dataset
                }
                else
                {
                    /** inserting name and password in table logindetails **/
                    cmd = new SqlCommand("INSERT INTO LOGINDETAILS VALUES('"+txtname.Text+"','"+txtpassword.Text+"')", con);
                    con.Open();
                    cmd.ExecuteNonQuery();
                    con.Close();
                   
                    msg = "Registered Successfully n Welcome "+txtname.Text;
                    this.Hide(); //hiding form1
                    Form2 f2 = newForm2(msg);
                    f2.Show(); //showing form2
 
                }
            }
            catch (Exceptionex)
            {
                MessageBox.Show(ex.Message);
                con.Close();
            }
 
        }
 
    }
}

and the c# code of Form2 is like this

using System.Windows.Forms;
 
namespace LoginApplication
{
    public partial class Form2 : Form
    {
        public Form2(stringmsg)   //passing parameter
        {
            InitializeComponent();
            lblWelcome.Text =msg;
           
        }
 
   
        private voidlblLogOut_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            this.Hide();  //hiding form2
            Form1 f1 = new Form1();
            f1.Show();  //showing form1
        }
    }

Download Source Code

Leave a Reply

Your email address will not be published. Required fields are marked *