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

namespace moshaveramlak
{
    public partial class usrs : Form
    {
        public usrs()
        {
            InitializeComponent();
        }
        
        void display()
        {
            OleDbConnection con1 = new OleDbConnection();
            con1.ConnectionString = "provider=microsoft.jet.oledb.4.0; data source=amlak.mdb";
            OleDbDataAdapter da = new OleDbDataAdapter("select * from usrs",con1);
            DataSet ds = new DataSet();
            da.Fill(ds, "usrs");
            dataGridView1.DataSource = ds.Tables["usrs"].DefaultView;
            con1.Close();
        }

        private void usrs_Load(object sender, EventArgs e)
        {
            // TODO: This line of code loads data into the 'amlakDataSet.usrs' table. You can move, or remove it, as needed.
            this.usrsTableAdapter.Fill(this.amlakDataSet.usrs);

        }

        private void button2_Click(object sender, EventArgs e)
        {
            
            OleDbConnection f1 = new OleDbConnection();
            f1.ConnectionString = "provider=microsoft.jet.oledb.4.0; data source=amlak.mdb";
            OleDbCommand f2 = new OleDbCommand();
            f2.Connection = f1;
            int item;
            f2.CommandText = "select count(pass) from usrs where pass='" + textBox3.Text + "'";
            f1.Open();
            item = (int)f2.ExecuteScalar();
           f1.Close();
            if (item != 0)
            {
                f2.CommandText = "delete from usrs where pass='" +textBox3.Text + "'";
                f1.Open();
               f2.ExecuteNonQuery();
                f1.Close();
                display();
                MessageBox.Show("!حساب کاربری مورد نظر حذف گردید", "پیام", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            else
                MessageBox.Show("!رمز عبور مورد نظر موجود نيست", "خطا",MessageBoxButtons.OK, MessageBoxIcon.Error);
       

        }

        private void button1_Click(object sender, EventArgs e)
        {
            
            OleDbConnection f3 = new OleDbConnection();
            f3.ConnectionString = "provider=microsoft.jet.oledb.4.0; data source=amlak.mdb";
            OleDbCommand f4 = new OleDbCommand();
            f4.CommandText = "insert into usrs(usr,pass) values ('" + textBox1.Text + "','"+textBox2.Text +  "')";
            f3.Open();
            f4.Connection = f3;
            f4.ExecuteNonQuery();
            f3.Close();
            display();
            MessageBox.Show("!حساب کاربری جدید ایجاد گردید", "پیام", MessageBoxButtons.OK, MessageBoxIcon.Information);

        }
    }
}
