How to create login page in form application c#?

Total Post:70

Points:350

C#
 765  View(s)
Ratings:
Rate this:
  1. How to create login page in form application c#?

    Here is the complete code for login form, which included maximum number of attempt by the user, showing error message when password or userid is blank or wrong, maximum character length of the password and userid, showing hidden text for the password like '*','?','+'etc. and open a new form when we enter correct userid and password.




    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 FormsApplication1
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }
           public  int i=0, j=0, k=0;
            private void button1_Click(object sender, EventArgs e)
            {  SqlConnection con = new SqlConnection();
                con.ConnectionString = (@"Data Source=msclient-012\sqlexpress;Initial Catalog=testing_st;User ID=sa;Password=mindstick");
                con.Open();
              
                SqlCommand cmd = new SqlCommand("Select UserId from User_information where UserId=@UserId and User_Password=@User_Password", con);
                //   SqlCommand cmd = new SqlCommand("Login_Check_Sp", con);
                // cmd.CommandType = CommandType.StoredProcedure;
                DataTable dt = new DataTable();
                cmd.Parameters.AddWithValue("@UserId", textBox1.Text);
                cmd.Parameters.AddWithValue("@User_Password", textBox2.Text);
                SqlDataReader read = cmd.ExecuteReader();
                read.Read();
                if (textBox1.Text.Trim() == string.Empty)
                {
                    MessageBox.Show("Userid need to be fill","Warning message");
                    i++;
                }
                else
                {
                    if(textBox2.Text.Trim() == string.Empty)
                    {
                        MessageBox.Show("Password Required!!", "Warning message");
                        j++;
                    }
                    else
                    {
                        if (read.HasRows)
                        {
                            //here you may redirect to another page like Response.Redirect("Page2.aspx");
                            Form2 objfill = new Form2();
                            objfill.Show();
                            this.Hide();
     
                            con.Close();
                        }
                        else
                        {
                            MessageBox.Show("Your UserId, Password or both are wrong ", "Error Message;
                            k++;
                        }
                    }
                }
                if (i > 3 )
                {
                    MessageBox.Show("Attempt for Userid Is greater than 3, please reopen","Error Message");
                    this.Close();
                }
                if ( j > 3)
                {
                    MessageBox.Show("Attempt for Password Is greater than 3, please repen and set your password","Error Messaage");
                    this.Close();
                }
                if (k>3)
                {
                    MessageBox.Show("Attempt for Password Is greater than 3, please repen and set your password","Error Message");
                    this.Close();
                }
            }
            private void button2_Click(object sender, EventArgs e)
            {
                Form3 F3 = new Form3();
                F3.Show();
            }
            private void Form1_Load(object sender, EventArgs e)
            {
            }
            private void textBox2_TextChanged(object sender, EventArgs e)
            {
                textBox2.MaxLength = 8;
                textBox2.PasswordChar = '*';
            }
            private void textBox1_TextChanged(object sender, EventArgs e)
            {
                textBox1.MaxLength = 16;
     
            }
        }
    }

      Modified On Mar-29-2018 12:06:23 AM

Answer