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 1: Click 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
Step 2: Design your form as below
Name your main form: Form1
Name your detail form: frmAddEditContact
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
- Insert Update Delete and View data from SQL Database using ORM Lite in C#
- How to Insert Update Delete Search Records in C#
- How to Insert Update Delete From Database in VB.NET
- How to Insert Update Delete and View data from SQL Database in C# using ADO.NET
- How to Insert Update Delete View data from SQL Database in C#
- Windows Forms: Insert Update Delete data in Database from DataGridView in C#
- Windows Forms: Insert Update Delete Search data from local database in C#
- Windows Forms: Insert Update Delete View data in SQL Server using 3 Tiers in C#
Categories
Popular Posts
Responsive Animated Login Form
11/11/2024
Carpatin Admin Dashboard Template
11/17/2024
Plus Admin Dashboard Template
11/18/2024