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;
using DevComponents.DotNetBar;

namespace academy
{
    public partial class fshahriyeh : DevComponents.DotNetBar.OfficeForm
    {
        public fshahriyeh()
        {
            InitializeComponent();
        }

        SqlConnection objconnection = new SqlConnection("server=(local);database=school;integrated security=true");
        SqlCommand objcommand = new SqlCommand();
        DataSet ds = new DataSet();
        //------------------------------------------------------------------------------------------------------------//


        public void search_code()
        {
            dgvfshahriyeh.Rows.Clear();
            SqlConnection con = new SqlConnection("server=(local);database=school;integrated security=true");
            SqlCommand c1 = new SqlCommand();
            con.Open();
            c1.CommandText = "select * from fshahriyeh where id= '" + txt_serach.Text + "'";
            c1.Connection = con;
            SqlDataReader dr = c1.ExecuteReader();
            object[] o1 = new object[7];
            while (dr.Read())
            {
                dr.GetValues(o1);
                dgvfshahriyeh.Rows.Add(o1);
            }

            con.Close();
        }

        public void search_name()
        {
            dgvfshahriyeh.Rows.Clear();
            SqlConnection con = new SqlConnection("server=(local);database=school;integrated security=true");
            SqlCommand c1 = new SqlCommand();
            con.Open();
            c1.CommandText = "select * from fshahriyeh where name= '" + txt_serach.Text + "'";
            c1.Connection = con;
            SqlDataReader dr = c1.ExecuteReader();
            object[] o1 = new object[7];
            while (dr.Read())
            {
                dr.GetValues(o1);
                dgvfshahriyeh.Rows.Add(o1);
            }

            con.Close();
        }

        public void search_family()
        {
            dgvfshahriyeh.Rows.Clear();
            SqlConnection con = new SqlConnection("server=(local);database=school;integrated security=true");
            SqlCommand c1 = new SqlCommand();
            con.Open();
            c1.CommandText = "select * from fshahriyeh where family= '" + txt_serach.Text + "'";
            c1.Connection = con;
            SqlDataReader dr = c1.ExecuteReader();
            object[] o1 = new object[7];
            while (dr.Read())
            {
                dr.GetValues(o1);
                dgvfshahriyeh.Rows.Add(o1);
            }

            con.Close();
        }

        public void search_cmeli()
        {
            dgvfshahriyeh.Rows.Clear();
            SqlConnection con = new SqlConnection("server=(local);database=school;integrated security=true");
            SqlCommand c1 = new SqlCommand();
            con.Open();
            c1.CommandText = "select * from fshahriyeh where cmeli= '" + txt_serach.Text + "'";
            c1.Connection = con;
            SqlDataReader dr = c1.ExecuteReader();
            object[] o1 = new object[7];
            while (dr.Read())
            {
                dr.GetValues(o1);
                dgvfshahriyeh.Rows.Add(o1);
            }

            con.Close();
        }

        public void search_namedars()
        {
            dgvfshahriyeh.Rows.Clear();
            SqlConnection con = new SqlConnection("server=(local);database=school;integrated security=true");
            SqlCommand c1 = new SqlCommand();
            con.Open();
            c1.CommandText = "select * from fshahriyeh where namedars= '" + txt_serach.Text + "'";
            c1.Connection = con;
            SqlDataReader dr = c1.ExecuteReader();
            object[] o1 = new object[7];
            while (dr.Read())
            {
                dr.GetValues(o1);
                dgvfshahriyeh.Rows.Add(o1);
            }

            con.Close();
        }

        public void search_price()
        {
            dgvfshahriyeh.Rows.Clear();
            SqlConnection con = new SqlConnection("server=(local);database=school;integrated security=true");
            SqlCommand c1 = new SqlCommand();
            con.Open();
            c1.CommandText = "select * from fshahriyeh where price= '" + txt_serach.Text + "'";
            c1.Connection = con;
            SqlDataReader dr = c1.ExecuteReader();
            object[] o1 = new object[7];
            while (dr.Read())
            {
                dr.GetValues(o1);
                dgvfshahriyeh.Rows.Add(o1);
            }

            con.Close();
        }

        public void search_datesodor()
        {
            dgvfshahriyeh.Rows.Clear();
            SqlConnection con = new SqlConnection("server=(local);database=school;integrated security=true");
            SqlCommand c1 = new SqlCommand();
            con.Open();
            c1.CommandText = "select * from fshahriyeh where datesodor= '" + txt_serach.Text + "'";
            c1.Connection = con;
            SqlDataReader dr = c1.ExecuteReader();
            object[] o1 = new object[7];
            while (dr.Read())
            {
                dr.GetValues(o1);
                dgvfshahriyeh.Rows.Add(o1);
            }

            con.Close();
        }


        private void clear()
        {
            txt_code.Clear();
            txt_datesodor.Clear();

        }

        private void add()
        {
            SqlConnection con = new SqlConnection("server=(local);database=school;integrated security=true");
            con.Open();
            string s1 = "insert into fshahriyeh(id,name,family,cmeli,namedars,price,datesodor)values('" + txt_code.Text + "','" + txt_name.Text + "','" + txt_family.Text + "','" + txt_cmeli.Text + "','" + cmb_namedore.Text + "','" + txt_price.Text + "','" + txt_datesodor.Text + "')";
            SqlCommand c2 = new SqlCommand(s1, con);
            if (MessageBox.Show("آیا می خواهید این را ثبت کنید؟", "اخطار ثبت", MessageBoxButtons.YesNo) == DialogResult.Yes)
            {
                c2.ExecuteNonQuery();
                MessageBox.Show("ثبت شد");
                show();
            }



            con.Close();
        }


        private void edit()
        {
            SqlConnection con = new SqlConnection("server=(local);database=school;integrated security=true");
            con.Open();
            string s1 = "update fshahriyeh set id ='" + txt_code.Text + "',name='" + txt_name.Text + "',family='" + txt_family.Text + "',cmeli='" + txt_cmeli.Text + "',namedars='" + cmb_namedore.Text + "',price='" + txt_price.Text + "',datesodor='" + txt_datesodor.Text + "' where id='" + txt_code.Text + "'";
            SqlCommand c2 = new SqlCommand(s1, con);
            if (MessageBox.Show("آیا می خواهید این را ویرایش کنید؟", "اخطار ویرایش", MessageBoxButtons.YesNo) == DialogResult.Yes)
            {
                try
                {
                    c2.ExecuteNonQuery();
                    MessageBox.Show("ویرایش شد");
                    show();
                }

                catch
                {
                    MessageBox.Show("خطا در ویرایش");
                }
            }

            con.Close();

        }


        private void delete()
        {
            SqlConnection con = new SqlConnection("server=(local);database=school;integrated security=true");
            con.Open();
            string s1 = "delete from fshahriyeh where id='" + txt_code.Text + "'";
            SqlCommand c2 = new SqlCommand(s1, con);
            if (MessageBox.Show("آیا می خواهید این را پاک کنید؟", "اخطار حذف", MessageBoxButtons.YesNo) == DialogResult.Yes)
            {
                c2.ExecuteNonQuery();
                MessageBox.Show("حذف شد");
                show();
            }

            con.Close();

        }


        private void show()
        {
            dgvfshahriyeh.Rows.Clear();
            SqlConnection con = new SqlConnection();
            con.ConnectionString = "server=(local);database=school;integrated security=true";
            SqlCommand c1 = new SqlCommand();
            con.Open();
            c1.CommandText = "select * from fshahriyeh";
            c1.Connection = con;
            SqlDataReader dr = c1.ExecuteReader();
            object[] o1 = new object[7];
            while (dr.Read())
            {
                dr.GetValues(o1);
                dgvfshahriyeh.Rows.Add(o1);
            }
            con.Close();
        }

        private void fshahriyeh_Load(object sender, EventArgs e)
        {
            DataTable dt1 = new DataTable();
            DataBase db1 = new DataBase();
            dt1 = db1.MySelect("select * from priceclass");
            cmb_namedore.DataSource = dt1;
            cmb_namedore.DisplayMember = "namedars";
            Font f = new System.Drawing.Font("Tahoma", 8);
            cmb_namedore.ListElement.Font = f;
            show();
        }

        private void txt_code_TextChanged(object sender, EventArgs e)
        {
            dgvfshahriyeh.Rows.Clear();
            SqlConnection con = new SqlConnection();
            con.ConnectionString = "server=(local);database=school;integrated security=true";
            SqlCommand c1 = new SqlCommand();
            con.Open();
            c1.CommandText = "select * from fstudent where id='" + txt_code.Text + "'";
            c1.Connection = con;
            SqlDataReader dr = c1.ExecuteReader();
            if (dr.Read())
            {
                txt_code.Text = dr["id"].ToString();
                txt_name.Text = dr["name"].ToString();
                txt_family.Text = dr["family"].ToString();
                txt_cmeli.Text = dr["cmeli"].ToString();

            }


            con.Close();
            show();
        }

        private void cmb_namedore_TextChanged(object sender, EventArgs e)
        {
            dgvfshahriyeh.Rows.Clear();
            SqlConnection con = new SqlConnection();
            con.ConnectionString = "server=(local);database=school;integrated security=true";
            SqlCommand c1 = new SqlCommand();
            con.Open();
            c1.CommandText = "Select price from priceclass where namedars='" + cmb_namedore.Text + "'";
            c1.Connection = con;
            SqlDataReader dr = c1.ExecuteReader();
            if (dr.Read())
            {
                txt_price.Text = dr["price"].ToString();

            }


            con.Close();
            show();
        }

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

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

        private void btn_delete_Click(object sender, EventArgs e)
        {
            delete();
            show();
            clear();
        }

        private void dgvfshahriyeh_MouseClick(object sender, MouseEventArgs e)
        {
            txt_code.Text = dgvfshahriyeh.CurrentRow.Cells[0].Value.ToString();
            txt_name.Text = dgvfshahriyeh.CurrentRow.Cells[1].Value.ToString();
            txt_family.Text = dgvfshahriyeh.CurrentRow.Cells[2].Value.ToString();
            txt_cmeli.Text = dgvfshahriyeh.CurrentRow.Cells[3].Value.ToString();
            cmb_namedore.Text = dgvfshahriyeh.CurrentRow.Cells[4].Value.ToString();
            txt_price.Text = dgvfshahriyeh.CurrentRow.Cells[5].Value.ToString();
            txt_datesodor.Text = dgvfshahriyeh.CurrentRow.Cells[6].Value.ToString();
        }

        private void btn_all_Click(object sender, EventArgs e)
        {
            show();
            txt_serach.Clear();
        }

        private void btn_back_Click(object sender, EventArgs e)
        {
            main m = new main();
            m.Show();
            this.Hide();
        }

        private void btn_searchs_Click(object sender, EventArgs e)
        {
            if (cmbsearch.Text == "کد")
            {
                search_code();
            }
            if (cmbsearch.Text == "نام")
            {
                search_name();
            }
            if (cmbsearch.Text == "نام خانوادگی")
            {
                search_family();
            }
            if (cmbsearch.Text == "کدملی")
            {
                search_cmeli();
            }
            if (cmbsearch.Text == "نام دوره")
            {
                search_namedars();
            }
            if (cmbsearch.Text == "مبلغ شهریه")
            {
                search_price();
            }
            if (cmbsearch.Text == "تاریخ صدور شهریه")
            {
                search_datesodor();
            }
        }

 

    }
}
