using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
namespace _0_2
{
  public partial class Form1 : Form
  {
     public Form1()
     {
        InitializeComponent();
     }
     bool newMode;
     DataSet ds = new DataSet();
     string strSql;
     string strCon = "Data Source=RAYANEH-C5E4683\\SQLEXPRESS;Initial Catalog=Student;Integrated Security=True";
     SqlDataAdapter da;
     SqlConnection con;
     DataRow newRow;
     SqlCommandBuilder cb;
     private void empty()
     {
        textRecord.Text = "";
        textBox2.Text = "";
        textBox3.Text = "";
        textBox4.Text = "";
        textBox5.Text = "";
     }
     private void Form1_Load(object sender, EventArgs e)
     {
        newMode = false;
        strSql = "SELECT * FROM stTable";
        con = new SqlConnection(strCon);
        con.Open();
        da = new SqlDataAdapter(strSql, con);
        da.Fill(ds, "stTable");
        textBox2.DataBindings.Add(new Binding("Text", ds, "stTable.stNo"));
        textBox3.DataBindings.Add(new Binding("Text", ds, "stTable.Name"));
        textBox4.DataBindings.Add(new Binding("Text", ds, "stTable.Ave"));
        checkBox1.DataBindings.Add(new Binding("Checked", ds, "stTable.Sex"));
        textBox5.DataBindings.Add(new Binding("Text", ds, "stTable.numUnit"));
        con.Close();
        textBox2.Text = "3";
        int k = Convert.ToInt16(textBox2.Text); 
     }
     private void Insert_Click(object sender, EventArgs e)
     {
        newRow = ds.Tables["stTable"].NewRow();
        newMode = true;
        textRecord.BackColor = Color.Red;
        textRecord.Text = "New Record";
        MessageBox.Show("Enter new record and Press Save ");
        empty();
     }
     private void SqlInsert_Click(object sender, EventArgs e)
     {
        int st;
        double av;
        int un;
        SqlCommand cmdClasf;
        string strSql1;
        string strSql2;
        st = Convert.ToInt16(textBox2.Text);
        av = Convert.ToDouble(textBox4.Text);
        un = Convert.ToInt16(textBox5.Text);
        strSql1 = "INSERT INTO stTable(" + "stNo, " +
                  "Name, " +
                  "Ave, " +
                  "numUnit, " +
                  "Sex " +
                  ") VALUES ('" + st + "', '" +
                  textBox3.Text + "','" +
                  av + "', '" + un +
                  "', 1 )";
        strSql2 = "INSERT INTO stTable(" + "stNo, " +
                  "Name, " +
                  "Ave, " +
                  "numUnit, " +
                  "Sex " +
                  ") VALUES ('" + st + "', '" +
                  textBox3.Text + "','" +
                  av + "', '" + un +
                  "', 0 )";
        con.Open();
        if (checkBox1.Checked)
           cmdClasf = new SqlCommand(strSql1, con);
        else
           cmdClasf = new SqlCommand(strSql2, con);
        cmdClasf.ExecuteNonQuery();
        con.Close();
        MessageBox.Show("Insert Completed .");
        ds.Clear();
        da.Fill(ds, "stTable");
        con.Close();
     }
     private void Delete_Click(object sender, EventArgs e)
     {
        string sn;
        SqlCommand cmd = new SqlCommand();
        string sqlDel;
        con.Open();
        sn = textBox2.Text;
        sqlDel = "DELETE FROM stTable WHERE stno = " + sn;
        cmd.CommandText = sqlDel;
        cmd.Connection = con;
        cmd.ExecuteNonQuery();
        ds.Clear();
        da.Fill(ds, "stTable");
        con.Close();
     }
     private void Save_Click(object sender, EventArgs e)
     {
        if (newMode == true)
        {
           newRow["stNo"] = Convert.ToInt16(textBox2.Text);
           newRow["Name"] = textBox3.Text;
           newRow["Sex"] = checkBox1.Checked;
           newRow["Ave"] = Convert.ToInt16(textBox4.Text);
           newRow["numUnit"] = Convert.ToInt16(textBox5.Text);
           ds.Tables["stTable"].Rows.Add(newRow);
           cb = new SqlCommandBuilder(da);
           da = cb.DataAdapter;
           da.Update(ds, "stTable");
           con.Close();
        }
        else
        {
           cb = new SqlCommandBuilder(da);
           da.Update(ds, "stTable");
        }
     }
     private void Exit_Click(object sender, EventArgs e)
     {
        if (ds.HasChanges())
        {
            DialogResult b1;
            b1 = MessageBox.Show("Dataset has changed, Saved ", "", System.Windows.Forms.MessageBoxButtons.YesNo);
            if (b1 == DialogResult.Yes)
               cb = new SqlCommandBuilder(da);
            da.Update(ds, "stTable");
            con.Close();
            Close();
        }
        else
            Close();
     }
     private void Button1_Click(object sender, EventArgs e)
     {
        this.BindingContext[ds, "stTable"].Position = 0;
        textRecord.Text = Convert.ToString(this.BindingContext[ds, "stTable"].Position + 1);
     }
     private void Button2_Click(object sender, EventArgs e)
     {
        int iPos;
        iPos = this.BindingContext[ds, "stTable"].Position + 1;
        if (iPos > 0)
        {
           this.BindingContext[ds, "stTable"].Position -= 1;
           textRecord.Text = Convert.ToString(this.BindingContext[ds, "stTable"].Position + 1);
        }
     }
     private void Button3_Click(object sender, EventArgs e)
     {
        int iPos;
        int iCount;
        iPos = this.BindingContext[ds, "stTable"].Position + 1;
        iCount = this.BindingContext[ds, "stTable"].Count;
        if (iPos < iCount)
        {
           this.BindingContext[ds, "stTable"].Position += 1;
           textRecord.Text = Convert.ToString(this.BindingContext[ds, "stTable"].Position + 1);
        }
     }
     private void Button4_Click(object sender, EventArgs e)
     {
        this.BindingContext[ds, "stTable"].Position = this.BindingContext[ds, "stTable"].Count - 1;
        textRecord.Text = Convert.ToString(this.BindingContext[ds, "stTable"].Position + 1);
     }
   }
}
