Mar 24, 2014

How to Create a Login form in Windows Form Using C#



Steps to be followed :->

1.) After designing this form using toolbar of Visual Studio. Click Enter button and type following code in it :->
private void btnEnter_Click(object sender, EventArgs e)
        {
            RKMVDataEntryCCA.Master frm = new RKMVDataEntryCCA.Master(textBoxUsername.Text);

            if (string.IsNullOrEmpty(textBoxUsername.Text))
            {
                //Focus box before showing a message
                textBoxUsername.Focus();
                MessageBox.Show("Enter your username", this.Text, MessageBoxButtons.OK, MessageBoxIcon.Information);
                //Focus again afterwards, sometimes people double click message boxes and select another control accidentally
                textBoxUsername.Focus();
                return;
            }
            else if (string.IsNullOrEmpty(textBoxPassword.Text))
            {
                textBoxPassword.Focus();
                MessageBox.Show("Enter your password", this.Text, MessageBoxButtons.OK, MessageBoxIcon.Information);
                textBoxPassword.Focus();
                return;
            }
            //OK they enter a user and pass, lets see if they can authenticate
            using (DataTable dt = LookupUser(textBoxUsername.Text))
            {
                if (dt.Rows.Count == 0)
                {
                    textBoxUsername.Focus();
                    MessageBox.Show("Invalid username.", this.Text, MessageBoxButtons.OK, MessageBoxIcon.Error);
                    textBoxUsername.Focus();
                    return;
                }
                else
                {
                    //Always compare the resulting crypto string or hash value, never the decrypted value
                    //By doing that you never make a call to Decrypt() and the application is harder to
                    //reverse engineer. I included the Decrypt() method here for informational purposes
                    //only. I do not recommend shipping an assembly with Decrypt() methods.
                    string dbPassword = Convert.ToString(dt.Rows[0]["Password"]);
                    //string appPassword = Encrypt(textBoxPassword.Text); //we store the password as encrypted in the DB
                    string appPassword = textBoxPassword.Text;
                    if (string.Compare(dbPassword, appPassword) == 0)
                    {
                        this.Hide();
                      
                        //Logged in
                        RKMVDataEntryCCA.Master master = new RKMVDataEntryCCA.Master(textBoxUsername.Text);
                        // MasterForm master = new MasterForm();
                        master.Show();

                    }
                    else
                    {
                        //You may want to use the same error message so they can't tell which field they got wrong
                        textBoxPassword.Focus();
                        MessageBox.Show("Invalid Password", this.Text, MessageBoxButtons.OK, MessageBoxIcon.Information);
                        textBoxPassword.Focus();
                        return;
                    }
                }
            }
        }

2.) Now Click button Exit and type this code :->
               Application.Exit();

3.) Then make a function to connect to the database of your choice and don't to forget to change the credential of following code according to your need :->
Function DatabaseConnection ()
{            const string connStr = "Data Source=.\\SQLEXPRESS; Initial Catalog=College Management System; Integrated Security=true";
            const string query = "Select Password From CMSloginID (NOLOCK) Where Name = @UserName";
            DataTable result = new DataTable();
            using (SqlConnection conn = new SqlConnection(connStr))
            {
                conn.Open();
                using (SqlCommand cmd = new SqlCommand(query, conn))
                {
                    cmd.Parameters.Add("@UserName", SqlDbType.NVarChar).Value = Username;
                    using (SqlDataReader dr = cmd.ExecuteReader())
                    {
                        result.Load(dr);
                    }
                }
            }
            return result;
        }
}

4.) Now press F5 to run the application.

Hope you enjoyed this code. for any query do check my website.

No comments:

Post a Comment