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

namespace check
{
    public partial class bank : Form
    {
        static SqlConnection con = new SqlConnection("Data Source=.\\SQLEXPRESS;AttachDbFilename=|DataDirectory|\\check.mdf;Integrated Security=True");

        public bank()
        {
            InitializeComponent();
        }


        private void clear()
        {
            txt_code.Clear();
            txt_namebank.Clear();
        }


        public void show()
        {
            dgv_bank.Rows.Clear();
            SqlConnection con = new SqlConnection("Data Source=.\\SQLEXPRESS;AttachDbFilename=|DataDirectory|\\check.mdf;Integrated Security=True");
            con.Open();
            SqlCommand c2 = new SqlCommand();
            c2.CommandText = "select * from bank";
            c2.Connection = con;
            SqlDataReader dr = c2.ExecuteReader();
            object[] o1 = new object[2];
            while (dr.Read())
            {
                dr.GetValues(o1);
                dgv_bank.Rows.Add(o1);
            }
            con.Close();
        }

        private void add()
        {
            con.Open();
            string s1 = "insert into bank(code,bankname)values('" + txt_code.Text + "','" + txt_namebank.Text + "')";
            SqlCommand c1 = new SqlCommand(s1, con);
            if (MessageBox.Show("آیا می خواهید این را ثبت کنید", "ثبت", MessageBoxButtons.YesNo) == DialogResult.Yes)
            {
                c1.ExecuteNonQuery();
                MessageBox.Show("ثبت شد");
                show();

            }

            con.Close();
        }



        private void edit()
        {
            con.Open();
            string s2 = "update bank set code='" + txt_code.Text + "',bankname='" + txt_namebank.Text + "' where code='" + txt_code.Text + "'";
            SqlCommand c3 = new SqlCommand(s2, con);
            if (MessageBox.Show("آیا می خواهید این را ویرایش کنید", "ویرایش", MessageBoxButtons.YesNo) == DialogResult.Yes)
            {
                c3.ExecuteNonQuery();
                MessageBox.Show("ویرایش شد");
                show();

            }

            con.Close();
        }


        private void delete()
        {
            con.Open();
            string s3 = "delete from bank where code='" + dgv_bank.CurrentRow.Cells[0].Value.ToString() + "'";
            SqlCommand c4 = new SqlCommand(s3, con);
            if (MessageBox.Show("آیا می خواهید این را حذف کنید", "حذف", MessageBoxButtons.YesNo) == DialogResult.Yes)
            {
                c4.ExecuteNonQuery();
                MessageBox.Show("حذف شد");
                show();

            }

            con.Close();
        }

        private void btn_add_Click(object sender, EventArgs e)
        {
            add();
            show();
            clear();
            txt_code.Focus();
        }

        private void btn_edit_Click(object sender, EventArgs e)
        {
            edit();
            show();
            clear();
            txt_code.Focus();
        }

        private void btn_del_Click(object sender, EventArgs e)
        {
            delete();
            show();
            clear();
            txt_code.Focus();
        }

        private void bank_Load(object sender, EventArgs e)
        {
            show();
        }

        private void dgv_bank_MouseClick(object sender, MouseEventArgs e)
        {
            txt_code.Text = dgv_bank.CurrentRow.Cells[0].Value.ToString();
            txt_namebank.Text = dgv_bank.CurrentRow.Cells[1].Value.ToString();
        }

    }
}
