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.SqlClient;

namespace lavazem_behdashti
{
    public partial class login : Form
    {
        public login()
        {
            InitializeComponent();
        }

        SqlConnection objconnection = new SqlConnection("Data Source=(local);Initial Catalog=lavazem behdashti;Integrated Security=True");
        SqlCommand objcommand = new SqlCommand();

        private void login_Load(object sender, EventArgs e)
        {

        }

        private void buttonX3_Click(object sender, EventArgs e)
        {
            int i = 0;
            try
            {

                if ((textBox1.Text == string.Empty) || (textBox2.Text == string.Empty))
                {
                    MessageBox.Show("Enter Data !");
                    return;
                }

                objcommand = new SqlCommand("SELECT COUNT(*) FROM Tbl_Users " +
                    "WHERE UserName=@UserName AND Password=@Password", objconnection);

                objcommand.Parameters.AddWithValue("@UserName", textBox1.Text.ToString());
                objcommand.Parameters.AddWithValue("@Password", textBox2.Text.ToString());

                if (objconnection.State == ConnectionState.Closed)
                {
                    objconnection.Open();
                    i = (int)objcommand.ExecuteScalar();
                }//end if

                objconnection.Close();

                if (i > 0)
                {
                    this.Hide();
                    Form1 frm2 = new Form1();
                    frm2.Show();

                }
                else
                    MessageBox.Show("نام کاربری یا کلمه عبور اشتباه است !");
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message.ToString());
            }
            finally
            {
                i = 0;
                objconnection.Close();
            }
        }

        private void textBox1_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
                buttonX3_Click(sender, e);
        }

        private void textBox2_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
                buttonX3_Click(sender, e);
        }

        private void buttonX3_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
                buttonX3_Click(sender, e);
        }

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