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

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

        }

        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (textBox1.Text == "" || textBox2.Text == "")
            {
                MessageBox.Show("لطفاً اطلاعات را به صورت کامل وارد کنید ", "خطا");

            }
            else
            {
                SqlConnection myConn;
                SqlCommand myComm;
                StrConn = "SERVER=.;DATABASE=Havapeyma;Integrated Security=True";
                StrQury = "Insert Into Admin (UserName,Password) Values (@U,@P)";
                myConn = new SqlConnection(StrConn);
                myComm = new SqlCommand();
                myComm.Connection = myConn;
                myComm.CommandType = CommandType.Text;
                myComm.CommandText = StrQury;
                myComm.Parameters.Add("U", SqlDbType.NVarChar, 50).Value = this.textBox1.Text;
                myComm.Parameters.Add("P", SqlDbType.NVarChar, 50).Value = this.textBox2.Text;
                myConn.Open();
                myComm.ExecuteNonQuery();
                myConn.Close();
                Show1();

                MessageBox.Show(" عملیات اضافه کردن کاربر با موفقیت انجام شد");

                textBox1.Text = ""; textBox2.Text = "";


            }
        }

        private void Show1()
        {
            SqlDataAdapter myDA;
            DataSet myDS;

            StrConn = "SERVER=.;DATABASE=Havapeyma;Integrated Security=True";
            StrQury = "Select * From Admin";
            myDA = new SqlDataAdapter(StrQury, StrConn);

            myDS = new DataSet();

            myDA.Fill(myDS, "Admin");

            this.dataGridView1.DataSource = myDS;
            this.dataGridView1.DataMember = "Admin";
            this.dataGridView1.AutoGenerateColumns = true;
            this.dataGridView1.RightToLeft = RightToLeft.Yes;
            this.dataGridView1.Refresh();
           
        }

        private void button2_Click(object sender, EventArgs e)
        {
            SqlConnection myConn;
            SqlCommand myComm;

            StrConn = "SERVER=.;DATABASE=Havapeyma;Integrated Security=True";
            StrQury = "Delete Admin Where UserName=@N And Password=@P";

            myConn = new SqlConnection(StrConn);

            myComm = new SqlCommand();
            myComm.Connection = myConn;
            myComm.CommandType = CommandType.Text;
            myComm.CommandText = StrQury;

            myComm.Parameters.Add("N", SqlDbType.NVarChar, 50).Value = this.textBox4.Text;
            myComm.Parameters.Add("P", SqlDbType.NVarChar, 50).Value = this.textBox3.Text;

            myConn.Open();
            myComm.ExecuteNonQuery();
            myConn.Close();
            MessageBox.Show(" عملیات حذف با موفقیت انجام شد");
            Show1();
        }
    }
}
