Local Declaration SQL

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.SqlClient;
                            
    namespace SqlConect
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }
            private void Form1_Load(object sender, EventArgs e)
            {
                // TODO: This line of code loads data into the 'firstSQLDataSet.Details' table. You can move, or remove it, as needed.
                this.detailsTableAdapter.Fill(this.firstSQLDataSet.Details);
            }
            private void button1_Click(object sender, EventArgs e)
            {
                SqlCommand cmd = new SqlCommand();
                SqlConnection con = new SqlConnection();

                cmd.Connection = con;
                con.ConnectionString = @"Data Source=DESKTOP-73VBH0K\SQLEXPRESS;Initial Catalog=FirstSQL;Integrated Security=True";
                con.Open();
                 
                cmd.CommandText = "Insert Into Details Values ("+textBox1.Text+",'"+textBox2.Text+"','"+textBox3.Text+"',"+textBox4.Text+")";
                cmd.ExecuteNonQuery();
                MessageBox.Show("Record Insert");
            }
            private void button2_Click(object sender, EventArgs e)
            { 
                SqlCommand cmd = new SqlCommand();
                SqlConnection con = new SqlConnection();

                cmd.Connection = con;
                con.ConnectionString = @"Data Source=DESKTOP-73VBH0K\SQLEXPRESS;Initial Catalog=FirstSQL;Integrated Security=True";
                con.Open();
                
                cmd.CommandText = "Update Details Set Student_Course = '"+textBox3.Text+"' Where Student_ID ="+textBox1.Text+"";
                cmd.ExecuteNonQuery();
                MessageBox.Show("Record Update");
            }
            private void button3_Click(object sender, EventArgs e)
            { 
                SqlCommand cmd = new SqlCommand();
                SqlConnection con = new SqlConnection();

                cmd.Connection = con;
                con.ConnectionString = @"Data Source=DESKTOP-73VBH0K\SQLEXPRESS;Initial Catalog=FirstSQL;Integrated Security=True";
                con.Open();
                
                cmd.CommandText = "Delete From Details Where Student_ID =" + textBox1.Text + "";
                cmd.ExecuteNonQuery();
                MessageBox.Show("Record Delete"); 
            }
            private void button4_Click(object sender, EventArgs e)
            {
                SqlCommand cmd = new SqlCommand();
                SqlConnection con = new SqlConnection();

                cmd.Connection = con;
                con.ConnectionString = @"Data Source=DESKTOP-73VBH0K\SQLEXPRESS;Initial Catalog=FirstSQL;Integrated Security=True";
                con.Open();
                
                cmd.CommandText = "Select * From Details Where Student_ID =" + textBox1.Text + "";
                cmd.ExecuteNonQuery();

                SqlDataReader 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)
            {
                SqlCommand cmd = new SqlCommand();
                SqlConnection con = new SqlConnection();

                cmd.Connection = con;
                con.ConnectionString = @"Data Source=DESKTOP-73VBH0K\SQLEXPRESS;Initial Catalog=FirstSQL;Integrated Security=True";
                con.Open();
                
                cmd.CommandText = "Select * From Details Where Student_Name Like'%"+textBox5.Text+"%'";
                SqlDataReader dr;
                dr = cmd.ExecuteReader();

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

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

Output