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

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

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

            con.Close();
        }

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

            con.Close();
        }

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

            con.Close();
        }

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

            con.Close();
        }

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

            con.Close();
        }

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

            con.Close();
        }

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

            con.Close();
        }

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

            con.Close();
        }

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

            con.Close();
        }

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

            con.Close();
        }

        private void show()
        {
            dgvstudent.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 student";
            c1.Connection = con;
            SqlDataReader dr = c1.ExecuteReader();
            object[]o1=new object[10];
            while (dr.Read())
            {
                dr.GetValues(o1);
                dgvstudent.Rows.Add(o1);
            }
            con.Close();
        }

        private void clear()
        {
            txt_code.Clear();
            txt_name.Clear();
            txt_family.Clear();
            txt_fname.Clear();
            txt_shenasname.Clear();
            txt_cmeli.Clear();
            txt_dore.Clear();
            txt_elevel.Clear();
            txt_tel.Clear();
            txt_mob.Clear();

        }

        private void add()
        {
            SqlConnection con = new SqlConnection("server=(local);database=school;integrated security=true");
            con.Open();
            string s1 = "insert into student(id,name,family,fname,shenasname,cmeli,dore,elevel,tel,mob)values('" + txt_code.Text + "','" + txt_name.Text + "','" + txt_family.Text + "','" + txt_fname.Text + "','" + txt_shenasname.Text + "','" + txt_cmeli.Text + "','" + txt_dore.Text + "','" + txt_elevel.Text + "','" + txt_tel.Text + "','"+txt_mob.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 student set id ='" + txt_code.Text + "',name='" + txt_name.Text + "',family='" + txt_family.Text + "',fname='" + txt_fname.Text + "',shenasname='" + txt_shenasname.Text + "',cmeli='" + txt_cmeli.Text + "',dore='" + txt_dore.Text + "',elevel='" + txt_elevel.Text + "',tel='" + txt_tel.Text + "',mob='" + txt_mob.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 student 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 btn_add_Click(object sender, EventArgs e)
        {
            add();
            show();
            clear();
        }

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

        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 dgvstudent_MouseClick(object sender, MouseEventArgs e)
        {
            txt_code.Text = dgvstudent.CurrentRow.Cells[0].Value.ToString();
            txt_name.Text = dgvstudent.CurrentRow.Cells[1].Value.ToString();
            txt_family.Text = dgvstudent.CurrentRow.Cells[2].Value.ToString();
            txt_fname.Text = dgvstudent.CurrentRow.Cells[3].Value.ToString();
            txt_shenasname.Text = dgvstudent.CurrentRow.Cells[4].Value.ToString();
            txt_cmeli.Text = dgvstudent.CurrentRow.Cells[5].Value.ToString();
            txt_dore.Text = dgvstudent.CurrentRow.Cells[6].Value.ToString();
            txt_elevel.Text = dgvstudent.CurrentRow.Cells[7].Value.ToString();
            txt_tel.Text = dgvstudent.CurrentRow.Cells[8].Value.ToString();
            txt_mob.Text = dgvstudent.CurrentRow.Cells[9].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_fname();
            }
            if (cmbsearch.Text == "شماره شناسنامه")
            {
                search_shenasname();
            }
            if (cmbsearch.Text == "کد ملی")
            {
                search_cmeli();
            }
            if (cmbsearch.Text == "نام دوره")
            {
                search_dore();
            }
            if (cmbsearch.Text == "مدرک تحصیلی")
            {
                search_elevel();
            }
            if (cmbsearch.Text == "تلفن")
            {
                search_tel();
            }
            if (cmbsearch.Text == "موبایل")
            {
                search_mob();
            }
        }

        

       
    }
}
