C# and Access 2016 Database Perform SMART CRUD Operations with SEARCH (Windows Forms Source Code)


Microsoft Visual Studio 2019 : Visual C# (C# and Microsoft Access 2016 Database Tutorial).
👨‍🏫 How to Perform SMART CRUD (Create Read Update & Delete) Operations with Search functionality.

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

👨‍🏫 How to connect Access database using VB.NET
In computer programming, create, read, update, and delete (CRUD) are the four basic functions of persistent storage. Alternate words are sometimes used when defining the four basic functions of CRUD, such as retrieve instead of read, modify instead of update, or destroy instead of delete.
Read more: Wikipedia

Screenshot 1

Screenshot 2

YouTube Part 1/2

YouTube Part 2/2

[-📍-] Recommended Videos [-📍-]

🚀 4K | How to Download and Install Visual Studio 2019, SQL Server 2017 and SQL Server Management Studio (SSMS).
📺 https://youtu.be/ZA19qSiPcOE

🚀 4K | How to Download and Install PostgreSQL on Windows 10 and NpgSQL - ADO.NET Data Provider for PostgreSQL.
📺 https://youtu.be/KCxWG2g22yo

🚀 4K | How to download and install XAMPP for Windows 10 and MySQL Connector/NET.
📺 https://youtu.be/yHYyMMlYRjk

[-📍-] Bonus Video Tutorials [-📍-]

-- Microsoft SQL Server --
Microsoft SQL Server is a relational database management system developed by Microsoft. As a database server, it is a software product with the primary function of storing and retrieving data as requested by other software applications—which may run either on the same computer or on another computer across a network. Wikipedia

🚀 4K | VB.NET and SQL Server Tutorial | Perform SMART CRUD (Create Read Update & Delete) Operations with Search functionality.
📺 Version 1: https://youtu.be/V7zZ92qS7Bg
📺 Version 2: https://youtu.be/jIyGTcccM94

🚀 4K | C# and SQL Server Tutorial | Perform SMART CRUD (Create Read Update & Delete) Operations with Search functionality.
📺 Version 1: https://youtu.be/ZA4wr2DW88E
📺 Version 2: https://youtu.be/i_yQHRbFBAA

-- PostgreSQL --
PostgreSQL, also known as Postgres, is a free and open-source relational database management system emphasizing extensibility and technical standards compliance. It is designed to handle a range of workloads, from single machines to data warehouses or Web services with many concurrent users. Wikipedia

🚀 4K | VB.NET and PostgreSQL Tutorial | Perform SMART CRUD (Create Read Update & Delete) Operations with Search functionality.
📺 https://youtu.be/kqyeaTWol9k
🚀 4K | C# and PostgreSQL Tutorial | Perform SMART CRUD (Create Read Update & Delete) Operations with Search functionality.
📺 https://youtu.be/SnpW2hAuTmk

-- MySQL --
MySQL is an open-source relational database management system. Its name is a combination of "My", the name of co-founder Michael Widenius's daughter, and "SQL", the abbreviation for Structured Query Language. Wikipedia

🚀 4K | VB.NET and MySQL Tutorial | Perform SMART CRUD (Create Read Update & Delete) Operations with Search functionality.
📺 https://youtu.be/J2e9q9CgxVI
🚀 4K | C# and MySQL Tutorial | Perform SMART CRUD (Create Read Update & Delete) Operations with Search functionality.
📺 https://youtu.be/tvd6tnQcDGA

-- Microsoft Access --
Microsoft Access is a database management system from Microsoft that combines the relational Microsoft Jet Database Engine with a graphical user interface and software-development tools. It is a member of the Microsoft Office suite of applications, included in the Professional and higher editions or sold separately. Wikipedia

🚀 Full-HD | VB.NET and Access Database Tutorial | Perform SMART CRUD (Create Read Update & Delete) Operations with Search functionality.
📺 Part 1/2: https://youtu.be/LfHSZ7vLwyU
📺 Part 2/2: https://youtu.be/PMZXbvQ5X5Y

🚀 Full-HD | C# and Access Database Tutorial | Perform SMART CRUD (Create Read Update & Delete) Operations with Search functionality.
📺 Part 1/2: https://youtu.be/TNOdAwOo4Kk
📺 Part 2/2: https://youtu.be/wonsL3PpOY4

[FREE SOURCE CODE by iBASSKUNG]

#BEGIN

Class: CRUD.cs

using System.Data;
using System.Windows.Forms;
using System.Data.OleDb;

namespace CSAccess_SMART_CRUD_V1
{
    class CRUD
    {

        private static string getConnectionString()
        {
            string conString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + Application.StartupPath;
                   conString += "\\cs_smart_crud.accdb;Persist Security Info=False;";
            return conString;
        }

        public static OleDbConnection con = new OleDbConnection(getConnectionString());
        public static OleDbCommand cmd = default(OleDbCommand);
        public static string sql = string.Empty;

        public static DataTable PerformCRUD(OleDbCommand com)
        {
            OleDbDataAdapter da = default(OleDbDataAdapter);
            DataTable dt = new DataTable();
            try
            {
                da = new OleDbDataAdapter();
                da.SelectCommand = com;
                da.Fill(dt);
                return dt;
            }
            catch (System.Exception ex)
            {
                MessageBox.Show("An error occurred: " + ex.Message,
                    "Perform CRUD Operations Failed : iBasskung Tutorial",
                    MessageBoxButtons.OK,MessageBoxIcon.Error);
                dt = null;
            }
            return dt;
        }
    }
}

Form: Form1.cs

using System;
using System.Data;
using System.Data.OleDb;
using System.Windows.Forms;

namespace CSAccess_SMART_CRUD_V1
{
    public partial class Form1 : Form
    {

        private string id = "";
        private int row = 0;

        public Form1()
        {
            InitializeComponent();
            resetMe();
        }

        private void resetMe()
        {

            this.id = "";

            firstNameTextBox.Text = "";
            lastNameTextBox.Text = "";

            if (genderComboBox.Items.Count > 0)
            {
                genderComboBox.SelectedIndex = 0;
            }

            updateButton.Text = "UPDATE ()";
            deleteButton.Text = "DELETE ()";

            keywordTextBox.Clear();
            keywordTextBox.Select();

        }

        private void Form1_Load(object sender, EventArgs e)
        {
            loadData("");
        }

        private void execute(string mySQL, string param)
        {
            CRUD.cmd = new OleDbCommand(mySQL, CRUD.con);
            AddParameters(param);
            CRUD.PerformCRUD(CRUD.cmd);
        }

        private void AddParameters(string str)
        {

            CRUD.cmd.Parameters.Clear();

            if (str == "Delete" && !string.IsNullOrEmpty(this.id))
            {
                CRUD.cmd.Parameters.AddWithValue("id", this.id);
            }

            CRUD.cmd.Parameters.AddWithValue("firstName", firstNameTextBox.Text.Trim());
            CRUD.cmd.Parameters.AddWithValue("lastName", lastNameTextBox.Text.Trim());
            CRUD.cmd.Parameters.AddWithValue("gender", genderComboBox.SelectedItem.ToString().Trim());

            if (str == "Update" && !string.IsNullOrEmpty(this.id))
            {
                CRUD.cmd.Parameters.AddWithValue("id", this.id);
            }

        }

        private void insertButton_Click(object sender, EventArgs e)
        {

            if (string.IsNullOrEmpty(firstNameTextBox.Text.Trim()) || 
                string.IsNullOrEmpty(lastNameTextBox.Text.Trim()))
            {
                MessageBox.Show("Please input first name and last name.", 
                    "Insert Data : iBasskung Tutorial",
                    MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                return;
            }

            CRUD.sql = "INSERT INTO tb_smart_crud(first_name, last_name, gender) VALUES(@firstName, @lastName, @gender)";
            execute(CRUD.sql, "Insert");

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

            loadData("");

            resetMe();

        }

        private void loadData(string keyword)
        {

            CRUD.sql = "SELECT auto_id, first_name, last_name, first_name + ' ' + last_name AS full_name, gender FROM tb_smart_crud " +
                "WHERE first_name + ' ' + last_name LIKE @keyword1 OR gender = @keyword2 ORDER BY auto_id ASC";

            string strKeyword = string.Format("%{0}%", keyword);

            CRUD.cmd = new OleDbCommand(CRUD.sql, CRUD.con);

            CRUD.cmd.Parameters.Clear();
            CRUD.cmd.Parameters.AddWithValue("keyword1", strKeyword);
            CRUD.cmd.Parameters.AddWithValue("keyword2", keyword);

            DataTable dt = CRUD.PerformCRUD(CRUD.cmd);

            if(dt.Rows.Count > 0)
            {
                row = Convert.ToInt32(dt.Rows.Count.ToString());
            }
            else
            {
                row = 0;
            }

            toolStripStatusLabel1.Text = "Number of row(s): " + row.ToString();

            DataGridView dgv = dataGridView1;

            dgv.MultiSelect = false;
            dgv.AutoGenerateColumns = true;
            dgv.SelectionMode = DataGridViewSelectionMode.FullRowSelect;

            dgv.DataSource = dt;

            dgv.Columns[0].HeaderText = "ID";
            dgv.Columns[1].HeaderText = "First Name";
            dgv.Columns[2].HeaderText = "Last Name";
            dgv.Columns[3].HeaderText = "Full Name";
            dgv.Columns[4].HeaderText = "Gender";

            dgv.Columns[0].Width = 80;
            dgv.Columns[1].Width = 170;
            dgv.Columns[2].Width = 170;
            dgv.Columns[3].Width = 210;
            dgv.Columns[4].Width = 100;

        }

        private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            if(e.RowIndex != -1)
            {

                DataGridView dgv = dataGridView1;

                this.id = Convert.ToString(dgv.CurrentRow.Cells[0].Value);
                updateButton.Text = "UPDATE (" + this.id + ")";
                deleteButton.Text = "DELETE (" + this.id + ")";

                firstNameTextBox.Text = Convert.ToString(dgv.CurrentRow.Cells[1].Value).Trim();
                lastNameTextBox.Text = Convert.ToString(dgv.CurrentRow.Cells[2].Value).Trim();
                // index 3 = Full Name
                genderComboBox.SelectedItem = Convert.ToString(dgv.CurrentRow.Cells[4].Value).Trim();

            }
        }

        private void updateButton_Click(object sender, EventArgs e)
        {

            if (dataGridView1.Rows.Count == 0)
            {
                return;
            }

            if (string.IsNullOrEmpty(this.id))
            {
                MessageBox.Show("Please select an item from the list.", "Update Data : iBasskung Tutorial",
                    MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                return;
            }

            if (string.IsNullOrEmpty(firstNameTextBox.Text.Trim()) ||
                string.IsNullOrEmpty(lastNameTextBox.Text.Trim()))
            {
                MessageBox.Show("Please input first name and last name.", "Update Data : iBasskung Tutorial",
                    MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                return;
            }

            CRUD.sql = "UPDATE tb_smart_crud SET first_name = @firstName, last_name = @lastName, gender = @gender WHERE auto_id = @id";
            execute(CRUD.sql, "Update");

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

            loadData("");

            resetMe();

        }

        private void deleteButton_Click(object sender, EventArgs e)
        {

            if (dataGridView1.Rows.Count == 0)
            {
                return;
            }

            if (string.IsNullOrEmpty(this.id))
            {
                MessageBox.Show("Please select an item from the list.", "Delete Data : iBasskung Tutorial",
                    MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                return;
            }

            if (MessageBox.Show("Do you want to delete the selected record?", "Delete Data : iBasskung Tutorial",
                MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
            {

                CRUD.sql = "DELETE FROM tb_smart_crud WHERE auto_id = @id";
                execute(CRUD.sql, "Delete");

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

                loadData("");

                resetMe();

            }

        }

        private void searchButton_Click(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(keywordTextBox.Text.Trim()))
            {
                loadData(this.keywordTextBox.Text.Trim());
            }
            else
            {
                loadData("");
            }

            resetMe();

        }
    }
}

#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:
📍 https://www.facebook.com/CodeAMinute
📍 https://www.facebook.com/IbasskungTutorial
📍 https://www.facebook.com/codewithibasskung

📺 YouTube:
📍 https://www.youtube.com/c/iBasskung

📺 Udemy (Online Course):
📍 https://www.udemy.com/vbnet-crystal-reports

📺 Twitter:
📍 https://twitter.com/#!/IBasskung

📺 Blogger:
📍 https://codeaminute.blogspot.com
📍 http://bit.ly/2wjuwvs

💯 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

#CSharp #VisualCSharp #AccessDatabase #CRUDOperation

Comments