Sunday, October 21, 2012

C# Access Database Tutorials In Urdu (Code) - Create Login Screen


using System;
using System.Data;
using System.Windows.Forms;
using System.Configuration;
using System.Data.OleDb;

namespace DatabaseDemoWithAccessVC
{
    public partial class LoginForm : Form
    {
        public LoginForm()
        {
            InitializeComponent();
        }

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

        private void SignInButton_Click(object sender, EventArgs e)
        {
            if (IsValidated())
            {
                try
                {
                    DataTable dtRowsReturned = new DataTable();

                    string connString = ConfigurationManager.ConnectionStrings["dbx"].ConnectionString;
                    string cmdString = "SELECT '#' FROM Users WHERE UserName = @UserName AND Password = @Password";

                    using (OleDbConnection con = new OleDbConnection(connString))
                    {
                        using (OleDbCommand cmd = new OleDbCommand(cmdString, con))
                        {
                            con.Open();

                            cmd.Parameters.AddWithValue("@UserName", UserNameTextBox.Text);
                            cmd.Parameters.AddWithValue("@Password", PasswordTextBox.Text);

                            OleDbDataReader reader = cmd.ExecuteReader();

                            dtRowsReturned.Load(reader);
                        }
                    }

                    if (dtRowsReturned.Rows.Count > 0)
                    {
                        this.Hide();

                        DashBoardForm dsf = new DashBoardForm();
                        dsf.ShowDialog();
                    }
                    else
                    {
                        MessageBox.Show("User Name/Password is incorrect. Please try again.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                        UserNameTextBox.Clear();
                        PasswordTextBox.Clear();
                        UserNameTextBox.Focus();
                    }
                }
                catch (ApplicationException ex)
                {
                    MessageBox.Show("Error: " + ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
        }

        private bool IsValidated()
        {
            if (UserNameTextBox.Text.Trim() == string.Empty)
            {
                MessageBox.Show("User Name is required.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                UserNameTextBox.Clear();
                UserNameTextBox.Focus();
                return false;
            }

            if (PasswordTextBox.Text.Trim() == string.Empty)
            {
                MessageBox.Show("Password is required.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                PasswordTextBox.Clear();
                PasswordTextBox.Focus();
                return false;
            }

            return true;
        }

        private void LoginForm_VisibleChanged(object sender, EventArgs e)
        {
            UserNameTextBox.Clear();
            PasswordTextBox.Clear();

            UserNameTextBox.Focus();
        }
    }
}

No comments:

Post a Comment