private void btnDelete_Click(object sender, EventArgs e)
        {
            var id = StudentByMatricNumber.GetMatricNumber(txtSearch.Text);
            var r = MessageBox.Show(@"Are you sure you want to DELETE? DELETING this record, deletes records in RELATED tables",
                                    @"Nacoss eVoting System",
                                    MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2,
                                    MessageBoxOptions.DefaultDesktopOnly);

            if (r == DialogResult.Yes)
            {
                using (_cnn = new SqlConnection(NacossVotingSystem.Properties.Settings.Default.DBConn))
                {
                    _cnn.Open();
                    _myTransaction = _cnn.BeginTransaction();
                    using (_cmd = new SqlCommand("spDeleteCandidateById", _cnn))
                    {
                        _cmd.CommandType = CommandType.StoredProcedure;
                        _cmd.Transaction = _myTransaction;
                        _cmd.Parameters.AddWithValue("@candidateID", id);

                        var getCandidateById = new GetAllRecordById();
                        var deleteById = getCandidateById.GetCandidatesById(txtSearch.Text);

                        try
                        {
                            if (deleteById)
                            {
                                _cmd.ExecuteNonQuery();
                                _myTransaction.Commit();
                                MessageBox.Show(@"Record Successfully deleted", @"Nacoss eVoting System");
                                ClearAll();
                                notifyIcon1.BalloonTipIcon = ToolTipIcon.Info;
                                notifyIcon1.BalloonTipTitle = @"Delete Data";
                                notifyIcon1.BalloonTipText = @"Record deleted successfully";
                                notifyIcon1.ShowBalloonTip(3000);
                            }
                            else
                                MessageBox.Show(@"Record not found in the Database", @"Nacoss eVoting System");
                        }
                        catch (SqlException ex)
                        {
                            MessageBox.Show(@"The transaction rollbacked due to " +ex.Message);
                            _myTransaction.Rollback();
                        }
                    }
                }
            }
        }
        private void btnUpdate_Click(object sender, EventArgs e)
        {
            var getVoterById = new GetAllRecordById();
            var id =  StudentByMatricNumber.GetMatricNumber(txtSearch.Text);
            bool getId = getVoterById.GetVotersById(id.ToString());
            if (getId)
            {
                using (_cnn = new SqlConnection(Settings.Default.DBConn))
                {
                    _cnn.Open();
                    _myTransaction = _cnn.BeginTransaction();
                    using (_cmd = new SqlCommand("spUpdateVotersById", _cnn))
                    {
                        _cmd.CommandType = CommandType.StoredProcedure;
                        _cmd.Transaction = _myTransaction;
                        _cmd.Parameters.AddWithValue("@firstname", txtFirstName.Text);
                        _cmd.Parameters.AddWithValue("@lastname", txtLastName.Text);
                        _cmd.Parameters.AddWithValue("@sex", cmbGender.Text);
                        _cmd.Parameters.AddWithValue("@dob", dtDOB.Value);
                        _cmd.Parameters.AddWithValue("@levelID", cmbLevel.Text);
                        _cmd.Parameters.AddWithValue("@matricnumber", txtMatricNumber.Text);
                        _cmd.Parameters.AddWithValue("@phonenumber", txtPhoneNumber.Text);
                        _cmd.Parameters.AddWithValue("@email", txtEmailAddress.Text);
                        _cmd.Parameters.AddWithValue("@voterID", id);

                        MemoryStream ms = new MemoryStream();
                        picImage.Image.Save(ms, ImageFormat.Bmp);
                        byte[] data = ms.ToArray();
                        _cmd.Parameters.AddWithValue("@image", data);

                        //try
                        //{
                            var r = MessageBox.Show(@"Are you sure you want to SUBMIT?", @"Nacoss eVoting System",
                                        MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2,
                                        MessageBoxOptions.DefaultDesktopOnly);

                            if (r == DialogResult.Yes && IsValidateData(true))
                            {
                                int rowsAffected = _cmd.ExecuteNonQuery();
                                _myTransaction.Commit();
                                MessageBox.Show(@"Record has been Updated!", @"Nacoss eVoting System");
                                notifyIcon1.BalloonTipIcon = ToolTipIcon.Info;
                                notifyIcon1.BalloonTipTitle = @"Updated";
                                notifyIcon1.BalloonTipText = @"Record has been updated Successfully";
                                notifyIcon1.ShowBalloonTip(3000);
                            }
                        //}
                        //catch (SqlException ex)
                        //{
                        //    MessageBox.Show(ex.Message);
                        //    _myTransaction.Rollback();
                        //}
                    }
                }
            }
            else
            {
                MessageBox.Show(@"Record cannot be update, It does not exist", @"Nacoss eVoting System");
                notifyIcon1.BalloonTipIcon = ToolTipIcon.Warning;
                notifyIcon1.BalloonTipTitle = @"Update Data";
                notifyIcon1.BalloonTipText = @"No record to update" + Environment.NewLine +
                    @"Enter the right data";
                notifyIcon1.ShowBalloonTip(3000);
            }
        }
        private void btnDelete_Click(object sender, EventArgs e)
        {
            var id = StudentByMatricNumber.GetMatricNumber(txtSearch.Text);
            var r = MessageBox.Show(@"Are you sure you want to DELETE? DELETING this record, deletes records in RELATED tables",
                                    @"Nacoss eVoting System",
                                    MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2,
                                    MessageBoxOptions.DefaultDesktopOnly);

            if (r == DialogResult.Yes)
            {
                using (_cnn = new SqlConnection(Settings.Default.DBConn))
                {
                    _cnn.Open();
                    _myTransaction = _cnn.BeginTransaction();
                    using (_cmd = new SqlCommand("spDeleteVotersById", _cnn))
                    {
                        _cmd.CommandType = CommandType.StoredProcedure;
                        _cmd.Transaction = _myTransaction;
                        _cmd.Parameters.AddWithValue("@voterID", id);

                        var getAll = new GetAllRecordById();
                        var getById = getAll.GetVotersById(txtSearch.Text);
                        if (getById)
                        {
                            try
                            {
                                _cmd.ExecuteNonQuery();
                                _myTransaction.Commit();
                                MessageBox.Show(@"Record has been deleted!", @"Nacoss eVoting System");
                                ClearAll();
                                notifyIcon1.BalloonTipIcon = ToolTipIcon.Info;
                                notifyIcon1.BalloonTipTitle = @"Delete Data";
                                notifyIcon1.BalloonTipText = @"Data has been deleted" + Environment.NewLine +
                                                             @"Successfully";
                                notifyIcon1.ShowBalloonTip(3000);
                            }
                            catch (SqlException ex)
                            {
                                MessageBox.Show(ex.Message);
                                _myTransaction.Rollback();
                            }
                        }
                        else
                        {
                            MessageBox.Show(@"Record cannot be deleted!, Voters Id not found", @"Nacoss eVoting System");
                            notifyIcon1.BalloonTipIcon = ToolTipIcon.Error;
                            notifyIcon1.BalloonTipTitle = @"Delete Data";
                            notifyIcon1.BalloonTipText = @"Record cannot be deleted";
                            notifyIcon1.ShowBalloonTip(3000);
                        }
                    }
                }
            }
        }
        private void btnUpdate_Click(object sender, EventArgs e)
        {
            var id = StudentByMatricNumber.CandidateMatricNumber(txtSearch.Text);
            var r = MessageBox.Show(@"Are you sure you want to UPDATE THIS RECORD?",
                                    @"Nacoss eVoting System",
                                    MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2,
                                    MessageBoxOptions.DefaultDesktopOnly);

            if (r == DialogResult.Yes && IsValidateData())
            {
                using (_cnn = new SqlConnection(NacossVotingSystem.Properties.Settings.Default.DBConn))
                {
                    var getCandidateById = new GetAllRecordById();
                    var getById = getCandidateById.GetCandidatesById(id.ToString());
                    _cnn.Open();
                    _myTransaction = _cnn.BeginTransaction();
                    using (_cmd = new SqlCommand("spUpdateCandiddatesById", _cnn))
                    {
                        _cmd.CommandType = CommandType.StoredProcedure;
                        _cmd.Transaction = _myTransaction;
                        _cmd.Parameters.AddWithValue("@firstname", txtFirstName.Text);
                        _cmd.Parameters.AddWithValue("@lastname", txtLastName.Text);
                        _cmd.Parameters.AddWithValue("@Gender", cmbGender.Text);
                        _cmd.Parameters.AddWithValue("@dob", dtDOB.Value);
                        _cmd.Parameters.AddWithValue("@levelID", cmbLevel.Text);
                        _cmd.Parameters.AddWithValue("@matricnumber", txtMatricNumber.Text);
                        _cmd.Parameters.AddWithValue("@phonenumber", txtPhone.Text);
                        _cmd.Parameters.AddWithValue("@email", txtEmailAddress.Text);
                        _cmd.Parameters.AddWithValue("@candidateID",  id);
                        _cmd.Parameters.AddWithValue("@position", cmbPost.Text);

                        MemoryStream ms = new MemoryStream();
                        picImage.Image.Save(ms, ImageFormat.Bmp);
                        byte[] data = ms.ToArray();
                        _cmd.Parameters.AddWithValue("@image", data);

                        try
                        {
                            int rowsAffected = _cmd.ExecuteNonQuery();
                            _myTransaction.Commit();
                            MessageBox.Show(@"Data Updated Successfully!", @"Nacoss eVoting System", MessageBoxButtons.OK,
                                MessageBoxIcon.Information, MessageBoxDefaultButton.Button1);
                            notifyIcon1.BalloonTipIcon = ToolTipIcon.Info;
                            notifyIcon1.BalloonTipTitle = @"Data Update";
                            notifyIcon1.BalloonTipText = @"Data Updated Successfully" ;
                            notifyIcon1.ShowBalloonTip(3000);
                        }
                        catch (SqlException ex)
                        {
                            MessageBox.Show(@"Transaction rollbacked due to " + ex.Message);
                            _myTransaction.Rollback();
                        }
                    }
                }
            }
        }