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 rec3 : Form
    {
        public rec3()
        {
            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 aparteman", con1);
            DataSet ds = new DataSet();
            da.Fill(ds, "aparteman");
            dataGridView1.DataSource = ds.Tables["aparteman"].DefaultView;
            con1.Close();
        }

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

        }

        private void pictureBox9_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.CommandText = "insert into aparteman(ida,metr,sahebaparteman,tabagheh,mablaghe,address,tell,tozihat) values ('" + textBox1.Text + "','" + textBox2.Text + "','" + textBox3.Text + "','" + textBox4.Text + "','" + textBox5.Text + "','" + textBox6.Text + "','" + textBox7.Text + "','" + textBox8.Text + "')";
            f1.Open();
            f2.Connection = f1;
            f2.ExecuteNonQuery();
            f1.Close();
            display();
            MessageBox.Show("با موفقیت ثبت شد", "پیام", MessageBoxButtons.OK, MessageBoxIcon.Information);

            textBox1.Clear();
            textBox2.Clear();
            textBox3.Clear();
            textBox4.Clear();
            textBox5.Clear();
            textBox6.Clear();
            textBox7.Clear();
            textBox8.Clear();
        }

        private void pictureBox8_Click(object sender, EventArgs e)
        {
             // ویرایش -آپارتمان
         
            
            OleDbConnection f5 = new OleDbConnection();
            f5.ConnectionString = "provider=microsoft.jet.oledb.4.0; data source=amlak.mdb";
            OleDbCommand f6 = new OleDbCommand();
            f6.Connection = f5;
            f6.CommandText = "select * from aparteman where ida='" + textBox9.Text + "'";
            OleDbDataReader re;
            f5.Open();
            re = f6.ExecuteReader();
            if (re.Read())
            {
                textBox1.Text = re["ida"].ToString();
                textBox2.Text = re["metr"].ToString();
                textBox3.Text = re["sahebaparteman"].ToString();
                textBox4.Text = re["tabagheh"].ToString();
                textBox5.Text = re["mablaghe"].ToString();
                textBox6.Text = re["address"].ToString();
                textBox7.Text = re["tell"].ToString();
                textBox8.Text = re["tozihat"].ToString();
            }
            
           
            else
                MessageBox.Show("!كد شناسایی مورد نظر موجود نيست", "خطا", MessageBoxButtons.OK, MessageBoxIcon.Error);

        }

        private void pictureBox7_Click(object sender, EventArgs e)
        {
            //حذف - آپارتمان

            OleDbConnection con = new OleDbConnection();
            con.ConnectionString = "provider=microsoft.jet.oledb.4.0; data source=amlak.mdb";
            OleDbCommand com = new OleDbCommand();
            com.Connection = con;
            int item;
            com.CommandText = "select count(ida) from aparteman where ida='" + textBox9.Text + "'";
            con.Open();
            item = (int)com.ExecuteScalar();
            con.Close();
            if (item != 0)
            {
                com.CommandText = "delete from aparteman where ida='" + textBox9.Text + "'";
                con.Open();
                com.ExecuteNonQuery();
                con.Close();
                display();
            }
            else
                MessageBox.Show("!كد شناسایی مورد نظر موجود نيست", "خطا", MessageBoxButtons.OK, MessageBoxIcon.Error);
       
        }

        private void textBox9_TextChanged(object sender, EventArgs e)
        {

        }

       
    }

     
}
