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_moreriat_ashkal_zamin : 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_moreriat_ashkal_zamin()
        {
            InitializeComponent();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            this.Close();
            Frm_Forush.ActiveForm.Refresh();
        }

        private void frm_moreriat_ashkal_zamin_Load(object sender, EventArgs e)
        {
            da.SelectCommand.CommandText = "select * from ashkalzamin order by code";

            da.Fill(ds, "b");

           DGashkal.DataSource = ds;
           DGashkal.DataMember = "b";
           DGashkal.Columns[0].HeaderText = "كد";
           DGashkal.Columns[1].HeaderText = "شكل";
        }

        private void button3_Click(object sender, EventArgs e)
        {
            
        }

        private void DGashkal_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            
        }

        private void button1_Click(object sender, EventArgs e)
        {
            
        }

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

        private void DGashkal_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            s1 = Convert.ToString(DGashkal.CurrentRow.Cells["code"].Value);
            da.SelectCommand.CommandText = "select * from ashkalzamin where code='" + s1 + "'";
            da.Fill(ds, "q");
            txtdel.Text = Convert.ToString(DGashkal.CurrentRow.Cells["name_shekl"].Value);

            textBox1.Text = Convert.ToString(DGashkal.CurrentRow.Cells["code"].Value);
        }

        private void button3_Click_1(object sender, EventArgs e)
        {
            string p;

            Int16 i;
            SqlDataAdapter da = new SqlDataAdapter("", con);
            if (txtshekl.Text != "")
            {
                ds.Clear();
                da.SelectCommand.CommandText = "select code from ashkalzamin";
                da.Fill(ds, "a");
                if (ds.Tables["a"].Rows.Count > 0)
                {
                    da.SelectCommand.CommandText = "select max(code) as id from ashkalzamin";
                    da.Fill(ds, "c");
                    dg1.DataSource = ds;
                    dg1.DataMember = "c";

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

                p = "insert into ashkalzamin(name_shekl,code) values ('" + txtshekl.Text + "'," + i + " )";
                SqlCommand cmd = new SqlCommand(p, con);
                con.Open();
                cmd.ExecuteNonQuery();
                con.Close();


                da.SelectCommand.CommandText = "select * from ashkalzamin order by code";
                da.Fill(ds, "z");
                DGashkal.DataSource = ds;
                DGashkal.DataMember = "z";
                txtshekl.Text = null;

                MessageBox.Show("اطلاعات با موفقیت ثبت شد");

            }
            else { MessageBox.Show("لطفا نوع شكل مورد نظر را وارد کنید"); }
        }

        private void button1_Click_1(object sender, EventArgs e)
        {
            string j;
            j = "delete from ashkalzamin where name_shekl='" + txtdel.Text + "'";
            SqlCommand cmd = new SqlCommand(j, con);
            con.Open();
            cmd.ExecuteNonQuery();
            con.Close();
            ds.Clear();
            da.SelectCommand.CommandText = "select * from ashkalzamin";
            da.Fill(ds, "h9");
            DGashkal.DataSource = ds;
            DGashkal.DataMember = "h9";
            DGashkal.Columns[0].HeaderText = "كد";
            DGashkal.Columns[1].HeaderText = "شكل";
        }

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

            p = "update  ashkalzamin  set name_shekl='" + txtshekl.Text + "' where code='" + textBox1.Text + "'";
            SqlCommand cmd = new SqlCommand(p, con);
            con.Open();
            cmd.ExecuteNonQuery();
            con.Close();
            ds.Clear();
            da.SelectCommand.CommandText = "select * from ashkalzamin";
            da.Fill(ds, "x");
            DGashkal.DataSource = ds;
            DGashkal.DataMember = "x";
            txtshekl.Text = null;
        }

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