Windows Forms: Insert Update Delete View and Search data from MySQL in C#

By FoxLearn 6/1/2017 9:28:54 PM   8.24K
How to Insert Update Delete View and Search data from MySQL in c#

Step 1Click New Project, then select Visual C# on the left, then Windows and then select Windows Forms Application. Name your project "CRUDMySql" and then click OK

my sql c#

Step 2: Design your form as below

my sql c#

Step 3: Create a dataset, then add a customer table to your dataset as below

dataset

Step 4: Add code to handle your form as below

using System;
using System.Data;
using System.Linq;
using System.Windows.Forms;

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

        private void Form1_Load(object sender, EventArgs e)
        {
            // TODO: This line of code loads data into the 'appData.customers' table. You can move, or remove it, as needed.
            this.customersTableAdapter.Fill(this.appData.customers);
            customersBindingSource.DataSource = this.appData.customers;
        }

        private void txtSearch_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == (char)13)
            {
                if (string.IsNullOrEmpty(txtSearch.Text))
                {
                    // TODO: This line of code loads data into the 'appData.customers' table. You can move, or remove it, as needed.
                    this.customersTableAdapter.Fill(this.appData.customers);
                    customersBindingSource.DataSource = this.appData.customers;
                }
                else
                {
                    //using linq to query data
                    var query = from o in this.appData.customers
                                where o.FullName.Contains(txtSearch.Text) || o.Email == txtSearch.Text || o.Address.Contains(txtSearch.Text)
                                select o;
                    customersBindingSource.DataSource = query.ToList();
                }
            }
        }

        private void btnNew_Click(object sender, EventArgs e)
        {
            try
            {
                panel.Enabled = true;
                txtFullname.Focus();
                //Add a new row
                this.appData.customers.AddcustomersRow(this.appData.customers.NewcustomersRow());
                customersBindingSource.MoveLast();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Message", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void btnEdit_Click(object sender, EventArgs e)
        {
            panel.Enabled = true;
            txtFullname.Focus();
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            panel.Enabled = false;
            customersBindingSource.ResetBindings(false);
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            try
            {
                //Save data to mysql database
                customersBindingSource.EndEdit();
                customersTableAdapter.Update(this.appData.customers);
                panel.Enabled = false;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Message", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void dataGridView_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Delete)
            {
                if (MessageBox.Show("Are you sure want to delete this record ?", "Message", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
                    customersBindingSource.RemoveCurrent();
            }
        }
    }
}

VIDEO TUTORIALS