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

namespace amlakyar
{
    public partial class frm_etelat_amlak : Form
    {
        static SqlConnection con = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\amlak.mdf;integrated security=true;User Instance=True");
        SqlDataAdapter da = new SqlDataAdapter("", con);
        DataSet ds = new DataSet();

        
        string s1;
        public frm_etelat_amlak()
        {
            InitializeComponent();
        }

        private void button2_Click_1(object sender, EventArgs e)
        {
            this.Close();
        }

        private void frm_etelat_amlak_Load(object sender, EventArgs e)
        {

            da.SelectCommand.CommandText = "select * from etelat_melk order by code";
            
            da.Fill(ds, "h");
            
            DGmelk.DataSource = ds;
            DGmelk.DataMember = "h";
            DGmelk.Columns[0].HeaderText = "كد";
            DGmelk.Columns[1].HeaderText = "نام ملك";
           
        }

        private void DGmelk_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            s1 = Convert.ToString(DGmelk.CurrentRow.Cells["code"].Value);
            da.SelectCommand.CommandText = "select * from etelat_melk where code='" + s1 + "'";
            da.Fill(ds, "v");
            txtdel .Text  = Convert.ToString(DGmelk .CurrentRow.Cells["name_melk"].Value);
                                 
            textBox1.Text = Convert.ToString(DGmelk.CurrentRow.Cells["code"].Value);
        }

        private void Button3_Click_1(object sender, EventArgs e)
        {
            string s;

            Int16 i;
            SqlDataAdapter da = new SqlDataAdapter("", con);
            if (txtmelk.Text != "")
            {
                ds.Clear();
                da.SelectCommand.CommandText = "select code from etelat_melk";
                da.Fill(ds, "w");
                if (ds.Tables["w"].Rows.Count > 0)
                {
                    da.SelectCommand.CommandText = "select max(code) as id from etelat_melk";
                    da.Fill(ds, "x");
                    dg.DataSource = ds;
                    dg.DataMember = "x";

                    i = Convert.ToInt16(dg.CurrentRow.Cells["id"].Value);
                    i++;
                }
                else
                {
                    i = 1;
                }

                s = "insert into etelat_melk(name_melk,code) values ('" + txtmelk.Text + "'," + i + " )";
                SqlCommand cmd = new SqlCommand(s, con);
                con.Open();
                cmd.ExecuteNonQuery();
                con.Close();


                da.SelectCommand.CommandText = "select * from etelat_melk order by code";
                da.Fill(ds, "h");
                DGmelk.DataSource = ds;
                DGmelk.DataMember = "h";
                txtmelk.Text = null;

                MessageBox.Show("اطلاعات با موفقیت ثبت شد");
            }
            else { MessageBox.Show("لطفا نام ملک را وارد کنید"); }
        }

        private void Button1_Click_1(object sender, EventArgs e)
        {
            string s;
            s = "delete from etelat_melk where name_melk='" + txtdel.Text + "'";
            SqlCommand cmd = new SqlCommand(s, con);
            con.Open();
            cmd.ExecuteNonQuery();
            con.Close();
            ds.Clear();
            da.SelectCommand.CommandText = "select * from etelat_melk";
            da.Fill(ds, "h2");
            DGmelk.DataSource = ds;
            DGmelk.DataMember = "h2";
            DGmelk.Columns[0].HeaderText = "كد";
            DGmelk.Columns[1].HeaderText = "نام ملك";
        }

        private void button4_Click_1(object sender, EventArgs e)
        {
            string s;

            s = "update  etelat_melk  set name_melk='" + txtmelk.Text + "' where code='" + textBox1.Text + "'";
            SqlCommand cmd = new SqlCommand(s, con);
            con.Open();
            cmd.ExecuteNonQuery();
            con.Close();
            ds.Clear();
            da.SelectCommand.CommandText = "select * from etelat_melk";
            da.Fill(ds, "y");
            DGmelk.DataSource = ds;
            DGmelk.DataMember = "y";
            txtmelk.Text = null;
        }

        private void button2_Click_2(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}