C# and SQL Server (V2) Perform SMART CRUD Operations with SEARCH (Windows Forms Source Code)


Microsoft Visual Studio 2019 : C# and Microsoft SQL Server Database Tutorial (Version 2).
ðŸ‘Ļ‍ðŸŦ 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 SQL Server 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


[-📍-] 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]

ðŸŽŊ Souce Code: http://bit.ly/35R1sLM

#BEGIN

Form: CRUD_Form.cs

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

namespace CS_SMART_CRUD_V2
{
    public partial class CRUD_Form : Form
    {

        private string strConString = "Data Source=TANIN-SANGNGAM\\SQLEXPRESS;Initial Catalog=CS_SMART_CRUD;Integrated Security=True";
        private string sql = string.Empty;
        private string id = string.Empty;
        private int intRow = 0;

        MessageBoxButtons okBtn = MessageBoxButtons.OK;
        MessageBoxIcon infoIco = MessageBoxIcon.Information;

        public CRUD_Form()
        {
            InitializeComponent();
            loadData("");
            resetMe();
           
        }

        private void loadData(string keyword)
        {
            sql = "SELECT AUTO_ID AS ID, FIRST_NAME AS [First Name], LAST_NAME AS [Last Name], [FIRST_NAME] + ' ' + [LAST_NAME] AS [Full Name], " +
                "GENDER AS [Gender] FROM TBL_SMART_CRUD WHERE ([FIRST_NAME] + ' ' + [LAST_NAME]) LIKE '%' + @keyword + '%' OR [GENDER] = @keyword";

            DataTable dt = PerformCRUD(sql, "select");

            if(dt.Rows.Count > 0)
            {
                intRow = Convert.ToInt32(dt.Rows.Count.ToString());
            }
            else
            {
                intRow = 0;
            }
            
            toolStripStatusLabel1.Text = "Number of row(s): " + intRow.ToString();

            dataGridView1.MultiSelect = false;
            dataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect;

            dataGridView1.DataSource = dt;

            dataGridView1.Columns[0].Width = 70;
            dataGridView1.Columns[1].Width = 150;
            dataGridView1.Columns[2].Width = 150;
            dataGridView1.Columns[3].Width = 200;
            dataGridView1.Columns[4].Width = 90;

            if (dataGridView1.Rows.Count > 0)
            {
                dataGridView1.CurrentCell = dataGridView1.Rows[0].Cells[0];
            }

        }

        private void resetMe()
        {
            this.id = string.Empty;

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

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

            updateButton.Text = "Update ()";
            deleteButton.Text = "Delete ()";

            keywordTextBox.Text = "";

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

        }

        private void CRUD_Form_Load(object sender, EventArgs e)
        {
           dataGridView1.CellClick += dataGridView1_SelectionChanged;
        }

        private void dataGridView1_SelectionChanged(object sender, EventArgs e)
        {
            try
            {
                if (dataGridView1.SelectedRows.Count > 0)
                {
                    this.id = dataGridView1.SelectedRows[0].Cells[0].Value.ToString();
                    updateButton.Text = "Update (" + this.id + ")";
                    deleteButton.Text = "Delete (" + this.id + ")";

                    DataGridViewRow row = dataGridView1.SelectedRows[0];

                    firstNameTextBox.Text = row.Cells[1].Value.ToString();
                    lastNameTextBox.Text = row.Cells[2].Value.ToString();

                    // Cells[3] = Full Name
                    
                    genderComboBox.SelectedItem = row.Cells[4].Value.ToString();


                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("An error occurred: " + ex.Message, "C# Programming : iBasskung Tutorial", okBtn, MessageBoxIcon.Error);
            }
        }

        private DataTable PerformCRUD(string mySQL, string str)
        {

            using (SqlConnection con = new SqlConnection(strConString))
            {
                using (SqlCommand cmd = new SqlCommand(mySQL, con))
                {
                    cmd.Parameters.Clear();

                    if (str.Equals("select"))
                    {
                        cmd.Parameters.AddWithValue("keyword", keywordTextBox.Text.Trim().ToString());
                    }

                    if (str.Equals("insert") || str.Equals("update"))
                    {
                        cmd.Parameters.AddWithValue("firstName", firstNameTextBox.Text.Trim().ToString());
                        cmd.Parameters.AddWithValue("lastName", lastNameTextBox.Text.Trim().ToString());
                        cmd.Parameters.AddWithValue("gender", genderComboBox.SelectedItem.ToString());
                    }

                    if (str.Equals("update") || str.Equals("delete") && !string.IsNullOrEmpty(this.id))
                    {
                        cmd.Parameters.AddWithValue("id", this.id);
                    }

                    SqlDataAdapter da = default(SqlDataAdapter);
                    DataTable dt = new DataTable();

                    da = new SqlDataAdapter();
                    da.SelectCommand = cmd;
                    da.Fill(dt);

                    return dt;

                }
            }
        }

        private void insertButton_Click(object sender, EventArgs e)
        {
            string caption = "Insert Data : iBasskung Tutorial";

            if (string.IsNullOrEmpty(firstNameTextBox.Text.Trim()) || string.IsNullOrEmpty(lastNameTextBox.Text.Trim()))
            {
                MessageBox.Show("Please input first name and last name.", caption, okBtn, infoIco);
                firstNameTextBox.Select();
                return;
            }

            sql = "INSERT INTO TBL_SMART_CRUD(FIRST_NAME, LAST_NAME, GENDER) VALUES(@firstName, @lastName, @gender)";

            PerformCRUD(sql, "insert");

            MessageBox.Show("The record has been saved!", caption, okBtn, infoIco);

            resetMe();

            loadData("");

        }

        private void updateButton_Click(object sender, EventArgs e)
        {
            string caption = "Update Data : iBasskung Tutorial";

            if (dataGridView1.Rows.Count == 0)
            {
                MessageBox.Show(dataGridView1.Rows.Count.ToString() + " No rows found.", caption, okBtn, infoIco);
                return;
            }

            if (updateButton.Text.Equals("Update ()") || string.IsNullOrEmpty(this.id))
            {
                MessageBox.Show("Please select an item from the list.", caption, okBtn, infoIco);
                return;
            }

            if (string.IsNullOrEmpty(firstNameTextBox.Text.Trim()) || string.IsNullOrEmpty(lastNameTextBox.Text.Trim()))
            {
                MessageBox.Show("Please input first name and last name.", caption, okBtn, infoIco);
                firstNameTextBox.Select();
                return;
            }
            
            sql = "UPDATE TBL_SMART_CRUD SET FIRST_NAME = @firstName, LAST_NAME = @lastName, GENDER = @gender WHERE AUTO_ID = @id";

            PerformCRUD(sql, "update");

            MessageBox.Show("The record has been updated!", caption, okBtn, infoIco);

            resetMe();

            loadData("");

        }

        private void deleteButton_Click(object sender, EventArgs e)
        {

            string caption = "Delete Data : iBasskung Tutorial";

            if (dataGridView1.Rows.Count == 0)
            {
                MessageBox.Show(dataGridView1.Rows.Count.ToString() + " No rows found.", caption, okBtn, infoIco);
                return;
            }

            if (deleteButton.Text.Equals("Delete ()") || string.IsNullOrEmpty(this.id))
            {
                MessageBox.Show("Please select an item from the list.", caption, okBtn, infoIco);
                return;
            }

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

            if (result == DialogResult.Yes)
            {
                sql = "DELETE FROM TBL_SMART_CRUD WHERE AUTO_ID = @id";

                PerformCRUD(sql, "delete");

                MessageBox.Show("The record has been deleted.", caption, okBtn, infoIco);

                resetMe();
                loadData("");

            }

        }

        private void searchButton_Click(object sender, EventArgs e)
        {
            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

#VisualCSharp #CSharp #SQLServer #CRUDOperation

Comments