using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace ch11_1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        string strCon = "Data Source = va-pc; Initial Catalog = University; Integrated Security = True";
        string sqlStr;
        DataSet ds = new DataSet();
        SqlDataAdapter adapter = null;
        SqlConnection connect = null;
        SqlCommandBuilder cb = null;
        DataRow newRow = null;
        private void clearTextBox()
        {
            textBox1.Text = "";
            textBox2.Text = "";
            textBox3.Text = "";
            textBox4.Text = "";
        }
        public void displayAll()
        {
            ds.Clear();
            connect = new SqlConnection(strCon);
            sqlStr = "select * from Students";
            connect.Open();
            adapter = new SqlDataAdapter(sqlStr, connect);
            cb = new SqlCommandBuilder(adapter);
            adapter.Fill(ds, "Students");
            listBox1.Items.Clear();
            for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
            {
                string rowStr = "";

                for (int j = 0; j < ds.Tables[0].Rows[i].ItemArray.Length; j++)
                {
                    rowStr += ds.Tables[0].Rows[i].ItemArray[j].ToString();
                    rowStr += "   ";
                }
                listBox1.Items.Add(rowStr);
            }
        }
        private void button1_Click(object sender, EventArgs e)
        {
            textBox5.Text = "RecNo = ";
            int currentPos = this.BindingContext[ds, "Students"].Position;
            int count = this.BindingContext[ds, "Students"].Count;
            if (currentPos < count)
            {
                this.BindingContext[ds, "Students"].Position += 1;
                textBox5.Text += this.BindingContext[ds, "Students"].Position.ToString();
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            textBox5.Text = "RecNo = ";
            int count = this.BindingContext[ds, "Students"].Count;
            this.BindingContext[ds, "Students"].Position = count;
            textBox5.Text += this.BindingContext[ds, "Students"].Position.ToString();
        }

        private void button3_Click(object sender, EventArgs e)
        {
            textBox5.Text = "RecNo = ";
            int currentPos = this.BindingContext[ds, "Students"].Position;
            int count = this.BindingContext[ds, "Students"].Count;
            if (currentPos > 0)
            {
                this.BindingContext[ds, "Students"].Position -= 1;
                textBox5.Text += this.BindingContext[ds, "Students"].Position.ToString();
            }
        }

        private void button4_Click(object sender, EventArgs e)
        {
            textBox5.Text = "RecNo = ";
            this.BindingContext[ds, "Students"].Position = 0;
            textBox5.Text += this.BindingContext[ds, "Students"].Position.ToString();
        }

        private void button5_Click(object sender, EventArgs e)
        {
            if (textBox1.Text != "")
            {
                connect = new SqlConnection(strCon);
                sqlStr = "select * from Students";
                adapter = new SqlDataAdapter(sqlStr, connect);
                cb = new SqlCommandBuilder(adapter);
                adapter.Fill(ds, "Students");
                newRow = ds.Tables[0].NewRow();
                newRow["studentId"] = textBox1.Text;
                newRow["firstName"] = textBox2.Text;
                newRow["lastName"] = textBox3.Text;
                newRow["sex"] = textBox4.Text;
                ds.Tables[0].Rows.Add(newRow);
                cb = new SqlCommandBuilder(adapter);
                adapter.Update(ds.Tables[0]);
                MessageBox.Show("Record inserted ");
                ds.Clear();
                connect.Close();
                displayAll();
                clearTextBox();
            }
            else
               MessageBox.Show("Enter an student .");  
         }

        private void button6_Click(object sender, EventArgs e)
        {
            connect = new SqlConnection(strCon);
            connect.Open();
            if (textBox6.Text != "")
            {
                sqlStr = "delete from Students where studentId = " + textBox6.Text;
                SqlCommand cmd = new SqlCommand();
                cmd.CommandText = sqlStr;
                cmd.Connection = connect;
                cmd.ExecuteNonQuery();
                int x = this.BindingContext["Students"].Position;
                textBox4.Text = x.ToString();
                connect.Close();
                MessageBox.Show("Record deleted ");
                displayAll();
                textBox6.Text = "";
            }
            else
                MessageBox.Show("Enter a studentId in TextBox6 ");
        }

        private void button7_Click(object sender, EventArgs e)
        {
            connect = new SqlConnection(strCon);
            connect.Open();
            sqlStr = "select * from Students";
            adapter = new SqlDataAdapter(sqlStr, connect);
            cb = new SqlCommandBuilder(adapter);
            adapter.Fill(ds, "Students");
            if (textBox6.Text != "")
            {
                int stNo = Convert.ToInt32(textBox6.Text);
                bool found = false;
                for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
                {
                    if (stNo == (int)ds.Tables[0].Rows[i].ItemArray[0])
                    {
                        found = true;
                        break;
                    }
                }
                if (found)
                    MessageBox.Show("Record found");
                else
                    MessageBox.Show("Recod not found");
                textBox6.Text = "";
            }
            else
                MessageBox.Show("Enter a student number");
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            textBox5.Text = "RecNo = ";
            connect = new SqlConnection(strCon);
            connect.Open();
            sqlStr = "select * from Students";
            adapter = new SqlDataAdapter(sqlStr, connect);
            cb = new SqlCommandBuilder(adapter);
            adapter.Fill(ds, "Students");
            if (ds.Tables[0].Rows.Count != 0)
            {
                textBox1.DataBindings.Add(new Binding("Text", ds, "Students.studentId"));
                textBox2.DataBindings.Add(new Binding("Text", ds, "Students.firstName"));
                textBox3.DataBindings.Add(new Binding("Text", ds, "Students.lastName"));
                textBox4.DataBindings.Add(new Binding("Text", ds, "Students.sex"));
                textBox5.Text += this.BindingContext[ds.Tables[0]].Position.ToString();
                displayAll();
            }
            else
                clearTextBox();
        }
           
        private void button8_Click(object sender, EventArgs e)
        {
        }

        private void textBox6_TextChanged(object sender, EventArgs e)
        {

        }
    }
}
