Windows Forms: Insert Update Delete View and Search data from MS Access in C#
By FoxLearn 6/1/2017 9:41:23 PM 20.49K
How to Insert Update Delete View and Search data from MS Access in C#
Step 1: Click New Project, then select Visual C# on the left, then Windows and then select Windows Forms Application. Name your project "CRUDMsAccess" and then click OK
Step 2: Design your form as below
Step 3: Create an employee table, then create a dataset and add an employee table to your dataset as below
Step 4: Add code to handle your form as below
using System; using System.Data; using System.Drawing; using System.Linq; using System.Windows.Forms; namespace CRUDMsAccess { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void txtSearch_KeyPress(object sender, KeyPressEventArgs e) { if (e.KeyChar == (char)13) { if (string.IsNullOrEmpty(txtSearch.Text)) { //Fill data to datatable this.employeesTableAdapter.Fill(this.appData.Employees); employeesBindingSource.DataSource = this.appData.Employees; //dataGridView.DataSource = employeesBindingSource; } else { //using linq to query data var query = from o in this.appData.Employees where o.FullName.Contains(txtSearch.Text) || o.PhoneNumber == txtSearch.Text || o.Email == txtSearch.Text || o.Address.Contains(txtSearch.Text) select o; employeesBindingSource.DataSource = query.ToList(); //dataGridView.DataSource = query.ToList(); } } } 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) employeesBindingSource.RemoveCurrent(); } } private void btnBrowse_Click(object sender, EventArgs e) { try { using (OpenFileDialog ofd = new OpenFileDialog() { Filter = "JPEG|*.jpg", ValidateNames = true, Multiselect = false }) { if (ofd.ShowDialog() == DialogResult.OK) pictureBox.Image = Image.FromFile(ofd.FileName);//Load image from file to picturebox } } catch (Exception ex) { MessageBox.Show(ex.Message, "Message", MessageBoxButtons.OK, MessageBoxIcon.Error); } } private void btnNew_Click(object sender, EventArgs e) { try { panel.Enabled = true; txtFullName.Focus(); //Add new row this.appData.Employees.AddEmployeesRow(this.appData.Employees.NewEmployeesRow()); employeesBindingSource.MoveLast(); } catch (Exception ex) { MessageBox.Show(ex.Message, "Message", MessageBoxButtons.OK, MessageBoxIcon.Error); employeesBindingSource.ResetBindings(false); } } private void btnEdit_Click(object sender, EventArgs e) { panel.Enabled = true; txtFullName.Focus(); } private void btnCancel_Click(object sender, EventArgs e) { panel.Enabled = false; employeesBindingSource.ResetBindings(false); } private void btnSave_Click(object sender, EventArgs e) { try { employeesBindingSource.EndEdit(); employeesTableAdapter.Update(this.appData.Employees); panel.Enabled = false; } catch (Exception ex) { MessageBox.Show(ex.Message, "Message", MessageBoxButtons.OK, MessageBoxIcon.Error); employeesBindingSource.ResetBindings(false); } } private void Form1_Load(object sender, EventArgs e) { // TODO: This line of code loads data into the 'appData.Employees' table. You can move, or remove it, as needed. this.employeesTableAdapter.Fill(this.appData.Employees); employeesBindingSource.DataSource = this.appData.Employees; } } }
VIDEO TUTORIALS
- How to Insert Update Delete and View data from SQL Database using NPoco ORM in C#
- 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#
Categories
Popular Posts
Material Lite Admin Template
11/14/2024
Freedash bootstrap lite
11/13/2024
RuangAdmin Template
11/17/2024
Responsive Animated Login Form
11/11/2024