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 university
{
    public partial class tagir : Form
    {
        public tagir()
        {
            InitializeComponent();
        }
        bool newMode;
        DataSet ds = new DataSet();
        string strSql;
        string strCon = "SERVER=.;DATABASE=university;Integrated Security=True";
        SqlDataAdapter da;
        SqlConnection con;
        DataRow newRow;
        SqlCommandBuilder cb;
        private void empty()
        {
            textBox1.Text = "";
            textBox2.Text = "";
        }
        private void tagir_Load(object sender, EventArgs e)
        {
            

            newMode = false;
            strSql = "SELECT * FROM ubur";
            con = new SqlConnection(strCon);
            con.Open();
            da = new SqlDataAdapter(strSql, con);
            da.Fill(ds, "ubur");
            textBox1.DataBindings.Add(new Binding("Text", ds, "ubur.kalame"));
            textBox2.DataBindings.Add(new Binding("Text", ds, "ubur.password"));

            dataGridView1.DataBindings.Add(new Binding("DataSource", ds, "ubur"));
            con.Close();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            newRow = ds.Tables["ubur"].NewRow();
            newMode = true;

            empty();
            MessageBox.Show("Insert data and press save..");
            textBox1.Focus(); 
        }

        private void button2_Click(object sender, EventArgs e)
        {
            if (newMode == true)
            {
                newRow["kalame"] = textBox1.Text;
                newRow["password"] = textBox2.Text;


                ds.Tables["ubur"].Rows.Add(newRow);
                cb = new SqlCommandBuilder(da);
                da = cb.DataAdapter;
                da.Update(ds, "ubur");
                con.Close();
                MessageBox.Show("Save is successful..");
            }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            string sn;
            SqlCommand cmd = new SqlCommand();
            string sqlDel;
            con.Open();
            sn = textBox1.Text;
            sqlDel = "DELETE FROM ubur WHERE kalame=" + sn;
            cmd.CommandText = sqlDel;
            cmd.Connection = con;
            cmd.ExecuteNonQuery();
            ds.Clear();
            da.Fill(ds, "ubur");
            con.Close();
            MessageBox.Show("Delete is successful");
        }

        private void button5_Click(object sender, EventArgs e)
        {
            this.BindingContext[ds, "ubur"].Position = 0;
            textBox15.Text = Convert.ToString(this.BindingContext[ds, "ubur"].Position + 1); 
        }

        private void button6_Click(object sender, EventArgs e)
        {
            int iPos;
            iPos = this.BindingContext[ds, "ubur"].Position + 1;
            if (iPos > 0)
            {
                this.BindingContext[ds, "ubur"].Position -= 1;
                textBox15.Text = Convert.ToString(this.BindingContext[ds, "ubur"].Position + 1);
            }
        }

        private void button7_Click(object sender, EventArgs e)
        {
            int iPos;
            int iCount;
            iPos = this.BindingContext[ds, "ubur"].Position + 1;
            iCount = this.BindingContext[ds, "ubur"].Count;
            if (iPos < iCount)
            {
                this.BindingContext[ds, "ubur"].Position += 1;
                textBox15.Text = Convert.ToString(this.BindingContext[ds, "ubur"].Position + 1);
            }
        }

        private void button8_Click(object sender, EventArgs e)
        {
            int iCount;
            iCount = this.BindingContext[ds, "ubur"].Count;
            this.BindingContext[ds, "ubur"].Position = iCount;
            textBox15.Text = Convert.ToString(this.BindingContext[ds, "ubur"].Position + 1);
        }

        

       

        private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
        {
            char ch;
            ch = e.KeyChar;
            if (ch == '\r')
                textBox2.Focus();
        }

        private void textBox2_KeyPress(object sender, KeyPressEventArgs e)
        {
            char ch;
            ch = e.KeyChar;
            if (ch == '\r')
                button2.Focus();  
        }
    }
}
