Visual C# (VS 2015) How to Search data in an Access database (Windows Forms Source Code)


Visual Studio 2015-2019 (Visual C#) | How to Search data in an Access database (Windows Forms Application | Free Source Code).

🎓 Master Visual Basic .NET and Access Database By Building the Point Of Sale System (POS).
ðŸ“ē Enroll Now: https://bit.ly/2WcbRhX

Visual Studio 2015-2019 | Visual C#


📚 YouTube (Part 1)


[Free Source Code by iBasskung]

#BEGIN

using System;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class FormEmployee : Form
    {
        public FormEmployee()
        {
            InitializeComponent();
            // Hide ID Column :)
            this.dataGridView1.Columns[0].Visible = false;
        }

        private void buttonExit_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private void FormEmployee_Load(object sender, EventArgs e)
        {
            // TODO: This line of code loads data into the 'employeeDb_2019_DataSet.Employees' table. You can move, or remove it, as needed.
            this.employeesTableAdapter.Fill(this.employeeDb_2019_DataSet.Employees);

        }

        private void FormEmployee_FormClosing(object sender, FormClosingEventArgs e)
        {
            string msg = "Do you want to exit the application?";
            string caption = "Exit Application : iBasskung Tutorial";
            MessageBoxButtons buttons = MessageBoxButtons.YesNo;
            MessageBoxIcon ico = MessageBoxIcon.Question;

            DialogResult result;

            result = MessageBox.Show(this, msg, caption, buttons, ico);

            if (result == DialogResult.Yes)
            {
                // Close the form.
                // MessageBox.Show("Goodbye, it was nice to meet you.", "iBasskung Tutorial", MessageBoxButtons.OK, MessageBoxIcon.Information);
                e.Cancel = false;
            }
            else
            {
                e.Cancel = true;
            }

        }

        private void buttonPrevious_Click(object sender, EventArgs e)
        {
            this.employeesBindingSource.MovePrevious();
        }

        private void buttonNext_Click(object sender, EventArgs e)
        {
            this.employeesBindingSource.MoveNext();
        }

        private void buttonAddNew_Click(object sender, EventArgs e)
        {
            this.employeesBindingSource.AddNew();
        }

        private void buttonSave_Click(object sender, EventArgs e)
        {
            // TODO :)

            try
            {

                string msg = "Do you want to save | update the selected record?";
                string caption = "Save | Update : iBasskung Tutorial";
                MessageBoxButtons buttons = MessageBoxButtons.YesNo;
                MessageBoxIcon ico = MessageBoxIcon.Question;

                DialogResult result;

                result = MessageBox.Show(this, msg, caption, buttons, ico);

                if (result == DialogResult.Yes)
                {
                    // Save | Update
                    this.employeesBindingSource.EndEdit();
                    this.employeesTableAdapter.Update(this.employeeDb_2019_DataSet.Employees);

                    // Refresh data
                    this.employeesTableAdapter.Fill(this.employeeDb_2019_DataSet.Employees);

                    MessageBox.Show("The record has been saved successfully.", "Save Data : iBasskung Tutorial", MessageBoxButtons.OK, MessageBoxIcon.Information);

                }
                else
                {
                    return;
                }

            }
            catch (Exception ex)
            {
                MessageBox.Show("Save | Update data failed : " + ex.Message.ToString(), "Save Data : iBasskung Tutorial", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }

        }

        private void buttonDelete_Click(object sender, EventArgs e)
        {
            // TODO :)

            try
            {

                string msg = "Do you want to permanently delete the selected records?";
                string caption = "Delete Data : iBasskung Tutorial";
                MessageBoxButtons buttons = MessageBoxButtons.YesNo;
                MessageBoxIcon ico = MessageBoxIcon.Question;

                DialogResult result;

                result = MessageBox.Show(this, msg, caption, buttons, ico);

                if (result == DialogResult.Yes)
                {
                    // Delete
                    this.employeesBindingSource.RemoveCurrent();

                    // Save the change :)
                    this.employeesBindingSource.EndEdit();
                    this.employeesTableAdapter.Update(this.employeeDb_2019_DataSet.Employees);

                    // Refresh data
                    this.employeesTableAdapter.Fill(this.employeeDb_2019_DataSet.Employees);

                    MessageBox.Show("The record has been deleted.", "Delete Data : iBasskung Tutorial", MessageBoxButtons.OK, MessageBoxIcon.Information);

                }
                else
                {
                    return;
                }

            }
            catch (Exception ex)
            {
                MessageBox.Show("Delete data failed : " + ex.Message.ToString(), "Delete Data : iBasskung Tutorial", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }

        }

        private void bindingNavigatorAddNewItem_Click(object sender, EventArgs e)
        {
            buttonAddNew.PerformClick();
        }

        private void bindingNavigatorDeleteItem_Click(object sender, EventArgs e)
        {
            buttonDelete.PerformClick();
        }

        private void toolStripButtonUpdateItem_Click(object sender, EventArgs e)
        {
            buttonSave.PerformClick();
        }

        private void toolStripButtonRefresh_Click(object sender, EventArgs e)
        {
            refreshData();
        }

        private void refreshData()
        {
            employeesBindingSource.Filter = null;
            this.employeesTableAdapter.Fill(this.employeeDb_2019_DataSet.Employees);

            if (keywordTextBox.CanSelect)
            {
                keywordTextBox.Clear();
                keywordTextBox.Select();
            }
        }

        private void searchButton_Click(object sender, EventArgs e)
        {
            searchAccessDatabase();
        }

        private void searchAccessDatabase()
        {

            if (string.IsNullOrEmpty(keywordTextBox.Text.Trim()))
            {
                refreshData();
                return;
            }

            string strKeyword = keywordTextBox.Text.Trim().ToString();

            StringBuilder sb = new StringBuilder();

            sb.AppendFormat("(Convert(Employee_ID, 'System.String') LIKE '" + "{0}" + "')", strKeyword);
            sb.AppendFormat("OR (First_Name LIKE '*" + "{0}" + "*')", strKeyword);
            sb.AppendFormat("OR (Last_Name LIKE '*" + "{0}" + "*')", strKeyword);
            sb.AppendFormat("OR (First_Name + ' ' + Last_Name LIKE '%" + "{0}" + "%')", strKeyword);
            sb.AppendFormat("OR (Phone LIKE '" + "{0}" + "')", strKeyword);
            sb.AppendFormat("OR (Email LIKE '*" + "{0}" + "*')", strKeyword);
            sb.AppendFormat("OR (YouTube LIKE '%" + "{0}" + "%')", strKeyword);

            string strFilter = sb.ToString();
            employeesBindingSource.Filter = strFilter;

            if (employeesBindingSource.Count != 0)
            {
                dataGridView1.DataSource = employeesBindingSource;
            }
            else
            {

                MessageBox.Show("Sorry, No records found.", "Search Result : iBasskung Tutorial", MessageBoxButtons.OK, MessageBoxIcon.Information);

                refreshData();
                return;

            }

        }
    }
}

#END

⛳ Follow me around ⛳
✔ Want to get updates on new courses or other cool free stuff? Just follow me on social media if that's your thing!

📚 Pages:

📚 YouTube:

📚 Udemy (Online Course):

📚 Twitter:

📚 Blogger:

ðŸ’Ŋ THANK YOU SO MUCH! ðŸ’Ŋ

Tags: visual studio 2019, visual basic 2019, vb.net, vb.net tutorial for beginners visual studio 2019, visual basic, vb, visual studio 2019 visual basic tutorial, visual studio 2019 tutorial, how to use visual studio 2019, visual basic tutorial 2019, access, visual studio

#EmployeeTrainingManagement #EmployeeManagement #EmployeeManagementSystem #EmployeeTrainingRecords #Software #CodeAMinute #iBasskung #iBasskungTutorial #VisualCSharp #CSharp #Programming #MicrosoftAccess #AccessDatabase #SQLServer #CrystalReports #Udemy #OnlineCourses #YouTube 

Comments