Friday, May 20, 2016

Windows form Application: Creating a login page


  To create a login page in windows form application (in fact in any application), first ensure that you have a DBMS software installed in your PC.(e.g. Windows SQL server management studio).

Usually a login page contains two textboxes one for username another for password.You can also use more than two textboxes according to your requirement. For simplicity i take only two textboxes.

How a login page works?

  In the login page, the user needs to enter the username and password in the specified place and click the submit/login button. The soon the user clicks the button, the username and password combination is checked against the stored values for the username and password in the database. i.e. the database contains a login table having at least these two columns for username and password.The table is usually created using any of the DBMS software and the values are entered either manually using queries or through a registration page (click here to know how to create registration page).For this time being, i assume that you have a login table  in your database.

Create the login page:

1. open visual studio 2013.
2. On the menu bar, choose File, New, and Project. Choose Programming      
    language as  Visual C# .
3.  In the templates list, choose the Windows Forms Application icon.
4.  Name the project as Login Form and specify the address of your folder.
5.  In the main window design the form as shown below.

Login Form
Drag two  TextBoxes and two buttons from the toolbox onto the form. Name these controls accordingly. Drag two labels from the toolbox and name them accordingly. Don't forget to change the Text property of the buttons and labels. The Text property can be changed in the properties.( Right-click on any control and then click properties. you will get properties window).

Note: Name of a control is different from Text property of a control. Name is used to refer or use a control in code whereas Text property is used to display some text on the form. For example, the name of the first button is button1 whereas the Text property is set to Log In.

6. To connect with a SQL Server database right-click on the form and click on   
   properties then go to the upper second option (Data Binding). In Data Binding    click the text area. In the right side corner of the Text area you will find a   
   reverse triangle option. click on it to get an option "Add project data source". 

7. A new window will be opened where you need to select the  “Database” and   
    then click Next. Now select “Dataset” and click Next (you might be asked to   
   select a dataset a couple of times). Now, click on New connection then click        the Refresh Button and select your SQL Server name. 

  For example, my SQLserver name is SIYARAM-PC\SQLEXPRESS . Now enter a     database name for example my database name EMPLOYEE (You have to select   your own database which contains the login table)  then click Test Connection     and then press OK Button.

8. Click the connection string to see a connection string. Copy that connection      string and put it somewhere safely. Because you are going to use this       
   connection string in your code. Even if you don't want to save now, you can        open it anytime to copy it. Now click Finish button.

   Now its time to write some code in the Form_load and button click events.

9. Go to the code part of the form. You will find the following set of namespaces    and few other stuffs already generated for you and  you just need to add few      other code as per your requirement.

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.Sql;  
using System.Data.OleDb;  
using System.Data.SqlClient;  
  
namespace login_form  
{  
    public partial class Form1 : Form  
    {  
        SqlConnection con = new SqlConnection();  
        public Form1()  
        {  
            SqlConnection con = new SqlConnection();  
           con.ConnectionString = "Data Source=SIYARAM-PC\\SQLEXPRESS;Initial                                               Catalog=EMPLOYEE;Integrated Security=True";    
            InitializeComponent();  
        }  
  
        private void Form1_Load(object sender, EventArgs e)  
        {  
        SqlConnection con= new SqlConnection("Data Source=SIYARAM-PC\\                SQLEXPRESS ;Initial Catalog=EMPLOYEE ; Integrated Security=True");  
         con.Open();  
  
          } 
       }  
   }  
  
In the Button1_click event write the following code,(button1 is the name of the login button)

   private void button1_Click(object sender, EventArgs e)  
        {              
   SqlConnection con = new SqlConnection();  
   con.ConnectionString = "Data Source=SIYARAM-PC\\SQLEXPRESS;                
                                    Initial Catalog=EMPLOYEE;Integrated Security=True";  
   con.Open();  
   string userid = textBox1.Text;  
   string password = textBox2.Text;  
   SqlCommand cmd = new SqlCommand("select userid, password from login                                      where userid ='" + textBox1.Text + "'and password ='" +                                   textBox2.Text + "'", con);  
   SqlDataAdapter da = new SqlDataAdapter(cmd);  
   DataTable dt = new DataTable();  
   da.Fill(dt);  
   if (dt.Rows.Count > 0)  
            {  
                MessageBox.Show("Login sucessfull");  
                }  
   else  
            {  
                MessageBox.Show("Invalid  username or password");  
            }  
            con.Close();  
        }      

In the Button2_click event write the following code,(button2 is the name of the cancel button)

      private void button2_Click(object sender, EventArgs e)  
        {  
            Application.Exit();  
  
        }  

Save and Execute your program. If at all you get any error, check again the connection string, name of the controls, name of the database, name of the table and name of the objects declared in your program. If you are accurate with these names, you project should get executed.

Enjoy Programming.

click here for "how to create registration page"


1 comment: