Monday, May 28, 2012

Insert, Delete and Update in generic list in C#

Introduction
This Code for Insert Update and Delete in Generic List in ASP.Net with C#

Using This Code
Insert Update and Delete in List

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace GenericList
{
    public partial class frmContact : Form
    {
        List contacts;
        public frmContact()
        {
            InitializeComponent();
        }

        # region Insert Operation

        private void btnInsert_Click(object sender, EventArgs e)
        {
            Customer cnt = new Customer();
            cnt.Name = txtName.Text.Trim();
            cnt.Mobile = txtMobile.Text.Trim();
            contacts.Add(cnt);
            CurrencyManager cm = (CurrencyManager)this.BindingContext[contacts];
            cm.Refresh();
            dgcontacts.DataSource = contacts;
        }
        # endregion

        #region Update Operation

        private void btnUpdate_Click(object sender, EventArgs e)
        {
            List select = contacts.FindAll(new Predicate(HasName));
            if (select.Count > 0) // record found
            {
                foreach (Customer cnt in select)
                {
                    cnt.Name = txtName.Text.Trim();
                    cnt.Mobile = txtMobile.Text.Trim();
                }
                CurrencyManager cm = (CurrencyManager)this.BindingContext[contacts];
                cm.Refresh();
                dgcontacts.DataSource = contacts;
            }
            else
            {
                MessageBox.Show("No records found");
            }
        }
        # endregion
        //This function will check for the person name
        bool HasName(Customer c)
        {
            return (c.Name == txtName.Text);
        }

        # region Delete Operation

        private void btnDelete_Click(object sender, EventArgs e)
        {
            List select = contacts.FindAll(new Predicate(HasName));
            if (select.Count > 0) // record found
            {
                foreach (Customer cnt in select)
                {
                    contacts.Remove(cnt);
                }
                CurrencyManager cm = (CurrencyManager)this.BindingContext[contacts];
                cm.Refresh();
                dgcontacts.DataSource = contacts;
            }
            else
            {
                MessageBox.Show("No records found");
            }
        }
        # endregion
        private void frmContact_Load(object sender, EventArgs e)
        {
            contacts = new List();
        }
    }

    //Customer Class

    public class Customer

    {

        private string _Name;

        private string _Mobile;

        public string Name

        {

            get { return _Name; }

            set { _Name = value; }

        }

        public string Mobile

        {

            get { return _Mobile; }

            set { _Mobile = value; }

        }

    }

}

1 comments:

  • Unknown says:
    May 11, 2014 at 2:42 AM

    sql insert , Update , delete using c#
    with code , easy explanation and demo app
    http://geeksprogrammings.blogspot.com/2014/05/sql-insert-update-delete-using-c.html

Post a Comment

Followers