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 Ajans
{
    public partial class Form6 : Form
    {
        String StrConn;
        String StrQury;
        public Form6()
        {
            InitializeComponent();
        }

        private void Form6_Load(object sender, EventArgs e)
        {
            // TODO: This line of code loads data into the 'havapeymaDataSet2.Mosafer' table. You can move, or remove it, as needed.
            this.mosaferTableAdapter.Fill(this.havapeymaDataSet2.Mosafer);

        }

        private void button2_Click(object sender, EventArgs e)
        {

            if (textBox1.Text != "")
            {

                SqlConnection myConn;
                SqlCommand myComm;
                SqlDataReader myDR;
                StrConn = "SERVER=.;DATABASE=Havapeyma;Integrated Security=True";
                StrQury = "Select * From Mosafer Where CodeMosafer=@Code";
                myConn = new SqlConnection(StrConn);
                myComm = new SqlCommand();
                myComm.Connection = myConn;
                myComm.CommandType = CommandType.Text;
                myComm.CommandText = StrQury;
                myComm.Parameters.Add("Code", SqlDbType.NVarChar, 50).Value = this.textBox1.Text;
                myConn.Open();
                myDR = myComm.ExecuteReader();
                if (myDR.Read())
                {
                    
                    this.textBox10.Text = myDR["Name"].ToString();
                    this.textBox2.Text = myDR["Family"].ToString();
                    this.textBox3.Text = myDR["TelNumber"].ToString();
                    this.textBox4.Text = myDR["Age"].ToString();
                    this.LBsex.Text = myDR["Sex"].ToString();
                    if (LBsex.Text == "زن") radioButton1.Checked = true;
                    if (LBsex.Text == "مرد") radioButton2.Checked = true;
                    this.textBox5.Text = myDR["SHarekat"].ToString();
                    this.textBox11.Text = myDR["Tharekat"].ToString();
                    this.textBox6.Text = myDR["MablaghD"].ToString();
                    this.textBox7.Text = myDR["Mabda"].ToString();
                    this.textBox8.Text = myDR["Maghsad"].ToString();
                    this.textBox12.Text = myDR["CodeHavapeyma"].ToString();
                    this.textBox9.Text = myDR["NSandali"].ToString();
                    groupBox2.Enabled = true;
                }
                else
                {
                    this.textBox10.Text = "";
                    this.textBox2.Text = "";
                    this.textBox3.Text = "";
                    this.textBox4.Text = "";
                    this.LBsex.Text = "";
                    this.textBox5.Text = "";
                    this.textBox11.Text = "";
                    this.textBox6.Text = "";
                    this.textBox7.Text = "";
                    this.textBox11.Text = "";
                    this.textBox8.Text = "";
                    this.textBox12.Text = "";
                    this.textBox9.Text = "";
                }
                myConn.Close();
                this.comboBox1.Items.Clear();
            }
            showCode();
            
        }

        private void showCode()
        {
            SqlConnection myConn;
            SqlCommand myComm;
            SqlDataReader myDR;
            StrConn = "SERVER=.;DATABASE=Havapeyma;Integrated Security=True";
            StrQury = "Select CodeHavapeyma From Mosafer Where CodeMosafer=@Code";
            myConn = new SqlConnection(StrConn);
            myComm = new SqlCommand();
            myComm.Connection = myConn;
            myComm.CommandType = CommandType.Text;
            myComm.CommandText = StrQury;
            myComm.Parameters.Add("Code", SqlDbType.NVarChar, 50).Value = this.textBox1.Text;
            myConn.Open();
            myDR = myComm.ExecuteReader();
            while (myDR.Read())
            {
                this.comboBox1.Text = myDR["CodeHavapeyma"].ToString();
              //  this.comboBox1.Items.AddRange(new object[] { myDR["CodeH"].ToString() });
            }
            myConn.Close();
            show2();
        }
        private void show2()
        {
            SqlConnection myConn;
            SqlCommand myComm;
            SqlDataReader myDR;
            StrConn = "SERVER=.;DATABASE=Havapeyma;Integrated Security=True";
            StrQury = "Select CodeH From Havapeyma";
            myConn = new SqlConnection(StrConn);
            myComm = new SqlCommand();
            myComm.Connection = myConn;
            myComm.CommandType = CommandType.Text;
            myComm.CommandText = StrQury;
            myConn.Open();
            myDR = myComm.ExecuteReader();
            while (myDR.Read())
            {
                  this.comboBox1.Items.AddRange(new object[] { myDR["CodeH"].ToString() });
            }
            myConn.Close();
        }
        private void button3_Click(object sender, EventArgs e)
        {
            SqlConnection myConn;
            SqlCommand myComm;
            StrConn = "SERVER=.;DATABASE=Havapeyma;Integrated Security=True";
            StrQury = "Update Mosafer Set Name=@Name,Family=@Family ,TelNumber=@TelNumber,Age=@Age,Sex=@Sex,SHarekat=@SHarekat,Tharekat=@Tharekat,MablaghD=@MablaghD,Mabda=@Mabda,Maghsad=@Maghsad,CodeHavapeyma=@CodeHavapeyma,NSandali=@NSandali Where CodeMosafer=@Code";
            myConn = new SqlConnection(StrConn);
            myComm = new SqlCommand();
            myComm.Connection = myConn;
            myComm.CommandType = CommandType.Text;
            myComm.CommandText = StrQury;
            myComm.Parameters.Add("Code", SqlDbType.NVarChar, 50).Value = this.textBox1.Text ;
            myComm.Parameters.Add("Name", SqlDbType.NVarChar, 50).Value = this.textBox10.Text;
            myComm.Parameters.Add("Family", SqlDbType.NVarChar, 50).Value = this.textBox2.Text;
            myComm.Parameters.Add("TelNumber", SqlDbType.NVarChar, 50).Value = this.textBox3.Text;
            myComm.Parameters.Add("Age", SqlDbType.NVarChar, 50).Value = this.textBox4.Text;
            myComm.Parameters.Add("Sex", SqlDbType.NVarChar, 50).Value = this.LBsex.Text;
            myComm.Parameters.Add("SHarekat", SqlDbType.NVarChar, 50).Value = this.textBox5.Text;
            myComm.Parameters.Add("Tharekat", SqlDbType.NVarChar, 50).Value = this.textBox11.Text;
            myComm.Parameters.Add("MablaghD", SqlDbType.NVarChar, 50).Value = this.textBox6.Text;
            myComm.Parameters.Add("Mabda", SqlDbType.NVarChar, 50).Value = this.textBox7.Text;
            myComm.Parameters.Add("Maghsad", SqlDbType.NVarChar, 50).Value = this.textBox8.Text;
            myComm.Parameters.Add("CodeHavapeyma", SqlDbType.NVarChar, 50).Value = this.comboBox1.Text; ;
            myComm.Parameters.Add("NSandali", SqlDbType.NVarChar, 50).Value = this.textBox9.Text;
            myConn.Open();
            myComm.ExecuteNonQuery();
            myConn.Close();
            show1();
            textBox1.Text = ""; textBox10.Text = ""; textBox11.Text = ""; comboBox1.Text  = ""; textBox2.Text = ""; textBox3.Text = "";
            textBox4.Text = ""; textBox5.Text = ""; textBox6.Text = ""; textBox7.Text = "";
            textBox8.Text = ""; textBox9.Text = "";
            LBTH.Text = "";
            LBsex.Text = "";
            groupBox2.Enabled = false;
            textBox1.Text = "";
            textBox1.Focus();
            MessageBox.Show("ویرایش  اطلاعات مسافر با موفقیت انجام شد","ویرایش اطلاعات مسافران");
        }
        private void show1()
        {
            SqlDataAdapter myDA;
            DataSet myDS;
            StrConn = "SERVER=.;DATABASE=Havapeyma;Integrated Security=True";
            StrQury = "Select * From Mosafer";
            myDA = new SqlDataAdapter(StrQury, StrConn);
            myDS = new DataSet();
            myDA.Fill(myDS, "Mosafer");
            this.dataGridView1.DataSource = myDS;
            this.dataGridView1.DataMember = "Mosafer";
            this.dataGridView1.AutoGenerateColumns = true;
            this.dataGridView1.RightToLeft = RightToLeft.Yes;
            this.dataGridView1.Refresh();
        }

        private void radioButton1_CheckedChanged(object sender, EventArgs e)
        {
            LBsex.Text = "زن";
        }

        private void radioButton2_Click(object sender, EventArgs e)
        {
            LBsex.Text = "مرد";
        }

        private void button1_Click(object sender, EventArgs e)
        {
            textBox1.Text = ""; textBox10.Text = ""; textBox11.Text = ""; textBox12.Text = ""; textBox2.Text = ""; textBox3.Text = "";
            textBox4.Text = ""; textBox5.Text = ""; textBox6.Text = ""; textBox7.Text = "";
            textBox8.Text = ""; textBox9.Text = "";
            LBTH.Text = "";
            LBsex.Text = "";
            groupBox2.Enabled = false;
            textBox1.Text = "";
            textBox1.Focus();
        }
    }
}
