using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.OleDb;
namespace shop
{
    public partial class karbar : Form
    {
        public karbar()
        {
            InitializeComponent();
        }

        void display()
        {
            OleDbConnection con1 = new OleDbConnection();
            con1.ConnectionString = "provider=microsoft.jet.oledb.4.0; data source=shoping.mdb";
            OleDbDataAdapter da = new OleDbDataAdapter("select * from karbar", con1);
            DataSet ds = new DataSet();
            da.Fill(ds, "karbar");
            dataGridView1.DataSource = ds.Tables["karbar"].DefaultView;
            con1.Close();
        }

        private void cls()
        {
            textBox1.Text = textBox2.Text = textBox3.Text = textBox4.Text = "";
        }

        private void button1_Click_1(object sender, EventArgs e)
        {
            OleDbConnection con = new OleDbConnection();
            con.ConnectionString = "provider=microsoft.jet.oledb.4.0; data source=shoping.mdb";
            OleDbCommand com = new OleDbCommand();
            com.CommandText = "insert into karbar(nam,famili,karbari,pas) values ('" + textBox1.Text + "','" + textBox2.Text + "','" + textBox3.Text + "','" + textBox4.Text + "')";
            con.Open();
            com.Connection = con;
            com.ExecuteNonQuery();
            con.Close();
            display();
            cls();
        }

        private void karbar_Load(object sender, EventArgs e)
        {
            display();
        }

        private void button4_Click(object sender, EventArgs e)
        {
            cls();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            OleDbConnection con = new OleDbConnection();
            con.ConnectionString = "provider=microsoft.jet.oledb.4.0; data source=shoping.mdb";
            OleDbCommand com = new OleDbCommand();
            com.Connection = con;
            int item;
            com.CommandText = "select count(karbari) from karbar where karbari='" + textBox3.Text + "'";
            con.Open();
            item = (int)com.ExecuteScalar();
            con.Close();
            if (item != 0)
            {
                if (MessageBox.Show("آيا مي خواهيد اطلاعات مورد نظز حذف شود؟", "توجه", MessageBoxButtons.YesNo) == DialogResult.Yes)
                    com.CommandText = "delete from karbar where karbari='" + textBox3.Text + "'";
                con.Open();
                com.ExecuteNonQuery();
                con.Close();
                display();
                cls();
            }
            else
            {
                MessageBox.Show("!شخص مورد نظر موجود نيست", "خطا", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            OleDbConnection con = new OleDbConnection();
            con.ConnectionString = "provider=microsoft.jet.oledb.4.0; data source=shoping.mdb";
            OleDbCommand com = new OleDbCommand();
            com.CommandText = "update karbar set nam='" + textBox1.Text + "',famili='" + textBox2.Text + "',karbari='" + textBox3.Text + "',pas='" + textBox4.Text + "'";
            con.Open();
            com.Connection = con;
            com.ExecuteNonQuery();
            con.Close();
            cls();
            display();
        }

        private void button5_Click(object sender, EventArgs e)
        {
            OleDbConnection con = new OleDbConnection();
            con.ConnectionString = "provider=microsoft.jet.oledb.4.0; data source=shoping.mdb";
            OleDbCommand com = new OleDbCommand();
            com.Connection = con;
            com.CommandText = "select * from karbar where karbari='" + textBox3.Text + "'";
            OleDbDataReader dr;
            con.Open();
            dr = com.ExecuteReader();
            if (dr.Read())
            {
                textBox1.Text = dr["nam"].ToString();
                textBox2.Text = dr["famili"].ToString();
                textBox3.Text = dr["karbari"].ToString();
                textBox4.Text = dr["pas"].ToString();
            }
            else
            {
                MessageBox.Show("!نام كاربري مورد نظر موجود نيست", "خطا", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void textBox3_TextChanged(object sender, EventArgs e)
        {
            OleDbConnection con1 = new OleDbConnection();
            con1.ConnectionString = "provider=microsoft.jet.oledb.4.0; data source=shoping.mdb";
            OleDbDataAdapter da = new OleDbDataAdapter("select * from karbar where karbari like'%" + textBox3.Text + "%'", con1);
            DataSet ds = new DataSet();
            da.Fill(ds, "karbar");
            dataGridView1.DataSource = ds.Tables["karbar"].DefaultView;
            con1.Close();
        }
    }
}
