Windows Forms: Insert Update Delete Select in SQL Server in C#

By FoxLearn 6/25/2017 4:37:47 PM   6.07K
CRUD Operations: Insert, Update, Delete, Select from SQL Database using C#, Entity Framework Database First

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

c# entity frameworkStep 2: Design your form as below

Name your main form: Form1

c# contact

Name your detail form: frmAddEditContact

c# add contact

You need to create a Contact table with fields correspond the Contact class below

public partial class Contact
{
    public int Id { get; set; }
    public string ContactName { get; set; }
    public string PhoneNumber { get; set; }
    public string Email { get; set; }
    public string Address { get; set; }
}

Step 3: Add an EF model, then add code to handle your form as below

Form1

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace EFCRUD
{
    public partial class Form1 : Form
    {
        ContactEntities db;
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            //Get contact data
            db = new ContactEntities();
            contactBindingSource.DataSource = db.Contacts.ToList();
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            //Open form to add a contact
            using(frmAddEditContact frm = new frmAddEditContact(null))
            {
                if (frm.ShowDialog() == DialogResult.OK)
                    contactBindingSource.DataSource = db.Contacts.ToList();
            }
        }

        private void btnEdit_Click(object sender, EventArgs e)
        {
            if (contactBindingSource.Current == null)
                return;
            using (frmAddEditContact frm = new frmAddEditContact(contactBindingSource.Current as Contact))
            {
                if (frm.ShowDialog() == DialogResult.OK)
                    contactBindingSource.DataSource = db.Contacts.ToList();
            }
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            if (contactBindingSource.Current != null)
            {
                if(MessageBox.Show("Are you sure want to delete this record ?", "Message", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
                {
                    //Delete contact
                    db.Contacts.Remove(contactBindingSource.Current as Contact);
                    contactBindingSource.RemoveCurrent();
                    db.SaveChanges();
                }
            }
        }
    }
}

frmAddEditContact

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace EFCRUD
{
    public partial class frmAddEditContact : Form
    {
        ContactEntities db;
        public frmAddEditContact(Contact obj)
        {
            InitializeComponent();
            db = new ContactEntities();
            if (obj == null)
            {
                contactBindingSource.DataSource = new Contact();
                //Add contact to model, allow insert
                db.Contacts.Add(contactBindingSource.Current as Contact);
            }
            else
            {
                contactBindingSource.DataSource = obj;
                //Attach contact to model, allow edit
                db.Contacts.Attach(contactBindingSource.Current as Contact);
            }
        }

        private void frmAddEditContact_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (DialogResult == DialogResult.OK)
            {
                if (string.IsNullOrEmpty(txtContactName.Text))
                {
                    MessageBox.Show("Please enter your contact name.", "Message", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    txtContactName.Focus();
                    e.Cancel = true;
                    return;
                }
                db.SaveChanges();
                e.Cancel = false;
            }
            e.Cancel = false;
        }
    }
}

VIDEO TUTORIALS