Tuesday, November 25, 2014

WPF C# Data Save, Update and Delete with 3 layer architecture - Part 2(DAL Class)

In DAL Class Library Project, add a class and name it CustomerInfoGetway.cs
First add two references of DAL & ENTITY. Now use this code.


1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using DATA;
using ENTITY;

namespace DAL
{
    public class CustomerInfoGetway
    {
        //Create object of my EntityFramework data model
        JSMSEntities dataContextObj = new JSMSEntities();

        //Saving Customer Information 
        public void SaveCustomerInfo(CustomerInfo _customerInfoObj)
        {
            var newCustomerInfo = new tbl_Customer
            {
                CustomerName = _customerInfoObj.CustomerName,
                CustomerAddress = _customerInfoObj.CustomerAddress,
                CustomerPhone = _customerInfoObj.CustomerPhone,
                CustomerMobile = _customerInfoObj.CustomerMobile,
                CustomerFax = _customerInfoObj.CustomerFax,
                CustomerEmail = _customerInfoObj.CustomerEmail,
                CustomerNote = _customerInfoObj.CustomerNote,
            };

            dataContextObj.tbl_Customer.Add(newCustomerInfo);
            dataContextObj.SaveChanges();
        }

        //Call data from database to ListView 
        public List<CustomerInfo> LoadAllCustomerInfo()
        {
            dataContextObj = new JSMSEntities();
            List<CustomerInfo> customerInfoList = new List<CustomerInfo>();
            foreach (var c in (from p in dataContextObj.tbl_Customer select p).Distinct())
            {
                CustomerInfo _customerInfoObj = new CustomerInfo();
                _customerInfoObj.Id = c.ID;
                _customerInfoObj.CustomerName = c.CustomerName;
                _customerInfoObj.CustomerAddress = c.CustomerAddress;
                _customerInfoObj.CustomerPhone = c.CustomerPhone;
                _customerInfoObj.CustomerMobile = c.CustomerMobile;
                _customerInfoObj.CustomerFax = c.CustomerFax;
                _customerInfoObj.CustomerEmail = c.CustomerEmail;
                _customerInfoObj.CustomerNote = c.CustomerNote;

                customerInfoList.Add(_customerInfoObj);
            }
            return customerInfoList;
        }

        //Delete Customer Information
        public void DeleteCustomerInfo(CustomerInfo _customerInfoObj)
        {
            tbl_Customer customer = dataContextObj.tbl_Customer.First(c => c.ID == _customerInfoObj.Id);
            dataContextObj.tbl_Customer.Remove(customer);
            dataContextObj.SaveChanges();
        }

        //Update Customer Information
        public void UpdateCustomerInfo(CustomerInfo _customerInfoObj)
        {
            var customerinfo = dataContextObj.tbl_Customer.First(c => c.ID == _customerInfoObj.Id);
            customerinfo.CustomerName = _customerInfoObj.CustomerName;
            customerinfo.CustomerAddress = _customerInfoObj.CustomerAddress;
            customerinfo.CustomerPhone = _customerInfoObj.CustomerPhone;
            customerinfo.CustomerMobile = _customerInfoObj.CustomerMobile;
            customerinfo.CustomerFax = _customerInfoObj.CustomerFax;
            customerinfo.CustomerEmail = _customerInfoObj.CustomerEmail;
            customerinfo.CustomerNote = _customerInfoObj.CustomerNote;
            dataContextObj.SaveChanges();
        }

    }
}


No comments:

Post a Comment