using System;
using System.Windows.Forms;
//----------------------------------
using System.Data;
using System.Data.SqlClient;

namespace _17_3
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        string strConn = "Server=.;" + "DataBase=MyTestDB;" +
                         "Integrated Security=True";

        SqlConnection sCon;
        SqlCommand sc;

        private void NewRecord()
        {
            groupBox1.Enabled = true;
            MessageBox.Show("Enter New Record and Press Save");
        }

        private void SaveRecord()
        {
            string strInsert = "Insert Into Info(Student_Number, Last_Name, First_Name, Sex, Average, Number_of_Unit) Values(@Student_Number, @Last_Name, @First_Name, @Sex, @Average, @Number_of_Unit)";

            sCon = new SqlConnection();
            sCon.ConnectionString = strConn;

            sc = new SqlCommand();
            sc.CommandType = CommandType.Text;
            sc.CommandText = strInsert;

            sc.Parameters.AddWithValue("@Student_Number",
                               Convert.ToInt32(textBox1.Text.Trim()));

            sc.Parameters.AddWithValue("@Last_Name",
                               textBox2.Text.Trim());

            sc.Parameters.AddWithValue("@First_Name",
                               textBox3.Text.Trim());

            sc.Parameters.AddWithValue("@Sex", checkBox1.Checked);

            sc.Parameters.AddWithValue("@average",
                               Convert.ToSingle(textBox4.Text.Trim()));

            sc.Parameters.AddWithValue("@Number_of_Unit",
                               Convert.ToInt32(textBox5.Text.Trim()));

            sc.Connection = sCon;
            sc.Connection.Open();
            sc.ExecuteNonQuery();
            sc.Connection.Close();

            groupBox1.Enabled = false;
            MessageBox.Show("Record Added");
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            groupBox1.Enabled = false;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            NewRecord();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            SaveRecord();
        }
    }
}