Global Declaration Access

Design

    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.OleDb;

    namespace Insert_Update_Delete_Search
    {
        public partial class Form1 : Form
        {
            OleDbConnection con = new OleDbConnection();
            OleDbCommand cmd = new OleDbCommand();
            public Form1()
            {
                InitializeComponent();
                cmd.Connection = con;
                con.ConnectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\AIT_PC_5\Documents\Student.accdb";
                con.Open();
            }
            private void Form1_Load(object sender, EventArgs e)
            {
                // TODO: This line of code loads data into the 'studentDataSet.Batch1' table. You can move, or remove it, as needed.
                this.batch1TableAdapter.Fill(this.studentDataSet.Batch1);
            }
            private void button1_Click(object sender, EventArgs e)
            {
                cmd.CommandText = "Insert Into Batch1 Values ("+textBox1.Text+",'"+textBox2.Text+"','"+textBox3.Text+"',"+textBox4.Text+")";
                cmd.ExecuteNonQuery();
                MessageBox.Show("Record Insert");
            }
            private void button2_Click(object sender, EventArgs e)
            {
                cmd.CommandText = "Update Batch1 Set Student_Course = '" + textBox3.Text + "' Where Student_ID = "+textBox1.Text+"";
                cmd.ExecuteNonQuery();
                MessageBox.Show("Record Update");
            }
            private void button3_Click(object sender, EventArgs e)
            {       
                cmd.CommandText = "Delete From Batch1 Where Student_ID ="+textBox1.Text+"";
                cmd.ExecuteNonQuery();
                MessageBox.Show("Record Delete");
            }
            private void button4_Click(object sender, EventArgs e)
            {
                cmd.CommandText = "Select * From Batch1 Where Student_ID ="+textBox1.Text+"";
                OleDbDataReader dr;
                dr = cmd.ExecuteReader();

                if (dr.Read())
                {
                    textBox1.Text = dr[0].ToString();
                    textBox2.Text = dr[1].ToString();
                    textBox3.Text = dr[2].ToString();
                    textBox4.Text = dr[3].ToString();
                }
                MessageBox.Show("Record Search");
           }
            private void textBox5_TextChanged(object sender, EventArgs e)
            {
                cmd.CommandText = "Select * From Batch1 Where Student_Name Like '%"+textBox5.Text+"%'";
                OleDbDataReader dr;
                dr = cmd.ExecuteReader();

                DataTable dt = new DataTable();
                dt.Load(dr);

                dataGridView1.DataSource = dt ;
                dataGridView1.Refresh();    
            }       
            private void button5_Click(object sender, EventArgs e)
            {
                cmd.CommandText = "Select * From Batch1 ";
                OleDbDataReader dr;

                DataTable dt = new DataTable();

                dataGridView1.DataSource = dt;
                dataGridView1.Refresh();
            }
        }   
    }
                

Output