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

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

            da.Fill(ds, "o");

            DGnema .DataSource = ds;
            DGnema.DataMember = "o";
            DGnema.Columns[0].HeaderText = "كد";
            DGnema.Columns[1].HeaderText = "نما";
        }

       

        

        

       

        

        private void DGnema_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            s1 = Convert.ToString(DGnema.CurrentRow.Cells["code"].Value);
            da.SelectCommand.CommandText = "select * from nemaha where code='" + s1 + "'";
            da.Fill(ds, "n");
            txtdel.Text = Convert.ToString(DGnema.CurrentRow.Cells["name_nama"].Value);

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

        }

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

            Int16 i;
            SqlDataAdapter da = new SqlDataAdapter("", con);
            if (txtnema.Text != "")
            {
                ds.Clear();
                da.SelectCommand.CommandText = "select code from nemaha";
                da.Fill(ds, "k");
                if (ds.Tables["k"].Rows.Count > 0)
                {
                    da.SelectCommand.CommandText = "select max(code) as id from nemaha";
                    da.Fill(ds, "u");
                    dg2.DataSource = ds;
                    dg2.DataMember = "u";

                    i = Convert.ToInt16(dg2.CurrentRow.Cells["id"].Value);
                    i++;
                }
                else
                {
                    i = 1;
                }
                r = "insert into nemaha(name_nama,code) values ('" + txtnema.Text + "'," + i + " )";
                SqlCommand cmd = new SqlCommand(r, con);
                con.Open();
                cmd.ExecuteNonQuery();
                con.Close();


                da.SelectCommand.CommandText = "select * from nemaha order by code";
                da.Fill(ds, "o");
                DGnema.DataSource = ds;
                DGnema.DataMember = "o";
                txtnema.Text = null;

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

        private void button1_Click_1(object sender, EventArgs e)
        {
            string x;
            x = "delete from nemaha where name_nama='" + txtdel.Text + "'";
            SqlCommand cmd = new SqlCommand(x, con);
            con.Open();
            cmd.ExecuteNonQuery();
            con.Close();
            ds.Clear();
            da.SelectCommand.CommandText = "select * from nemaha";
            da.Fill(ds, "c");
            DGnema.DataSource = ds;
            DGnema.DataMember = "c";
            DGnema.Columns[0].HeaderText = "كد";
            DGnema.Columns[1].HeaderText = "نما";
        }

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

            l = "update  nemaha  set name_nama='" + txtnema.Text + "' where code='" + textBox1.Text + "'";
            SqlCommand cmd = new SqlCommand(l, con);
            con.Open();
            cmd.ExecuteNonQuery();
            con.Close();
            ds.Clear();
            da.SelectCommand.CommandText = "select * from nemaha";
            da.Fill(ds, "d");
            DGnema.DataSource = ds;
            DGnema.DataMember = "d";
        }

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