Thursday, December 25, 2014

WPF Search from ListView

In DAL use code like this:

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
//Search Customer from listview
        public List<CustomerInfo> SearchCustomer(string searchValue)
        {
            dataContextObj = new JSMSEntities();

            List<CustomerInfo> customerInfoList = new List<CustomerInfo>();
            foreach (var p in (from c in dataContextObj.tbl_Customer 
                               where c.CustomerName.Contains(searchValue) || c.CustomerMobile.Contains(searchValue)
                               select c).Distinct())
            {
                CustomerInfo _customerInfoObj = new CustomerInfo();
                _customerInfoObj.Id = p.ID;
                _customerInfoObj.CustomerName = p.CustomerName;
                _customerInfoObj.CustomerAddress = p.CustomerAddress;
                _customerInfoObj.CustomerPhone = p.CustomerPhone;
                _customerInfoObj.CustomerMobile = p.CustomerMobile;
                _customerInfoObj.CustomerFax = p.CustomerFax;
                _customerInfoObj.CustomerEmail = p.CustomerEmail;
                _customerInfoObj.CustomerNote = p.CustomerNote;

                customerInfoList.Add(_customerInfoObj);
            }

            return customerInfoList;
        }

In BLL use code like this:

1
2
3
4
5
//BLL Search customer
        public List<CustomerInfo> SearchCustomer(string searchValue)
        {
            return customerInfoGetwayObj.SearchCustomer(searchValue);
        }

In xaml.cs file use code like this:

1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
//Search Customer
        private void txtCustomerSearch_TextChanged(object sender, TextChangedEventArgs e)
        {
            _customerInfoList = new List<CustomerInfo>();
            _customerInfoList = _customerInfManagerObj.SearchCustomer(txtCustomerSearch.Text);
            CustomerInfoListView.Items.Clear();

            if (_customerInfoList.Count > 0)
            {
                foreach (var item in _customerInfoList)
                {
                    CustomerInfoListView.Items.Add(item);
                }
            }
        }

Wednesday, December 24, 2014

WPF ListView Data Display

In DAL write code like this:
 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
public List<SaleInfo> LoadAllSaleList()
        {
            dataContext = new JSMSEntities();
            List<SaleInfo> saleList = new List<SaleInfo>();

            foreach (var p in (from c in dataContext.tbl_Sale 
                               select new 
                               { 
                                   c.SaleDateTime, 
                                   c.InvoiceNumber, 
                                   c.tbl_Customer.CustomerName, c.TotalAmount, 
                                   c.TotalPay, c.TotalDue 
                               }).Distinct())
            {
                SaleInfo allsaleObj = new SaleInfo();

                allsaleObj.SaleDateTime = (DateTime)p.SaleDateTime;
                allsaleObj.InvoiceNumber = p.InvoiceNumber;
                allsaleObj.CustomerName = p.CustomerName;
                allsaleObj.TotalAmount = (int)p.TotalAmount;
                allsaleObj.TotalPay = (int)p.TotalPay;
                allsaleObj.TotalDue = (int)p.TotalDue;

                saleList.Add(allsaleObj);
            }

            return saleList;
        }

In BLL write code like this :

1
2
3
4
public List<SaleInfo> LoadAllSaleList()
        {
            return _saleGetwayObj.LoadAllSaleList();
        }


In XAML add ListView like this:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
        <ListView 
 HorizontalAlignment="Left" 
 Height="509" 
 Margin="10,52,0,0" 
 VerticalAlignment="Top" 
 Width="624"
 x:Name="SaleListView">
            <ListView.View>
                <GridView AllowsColumnReorder="True">
                    <GridViewColumn DisplayMemberBinding="{Binding Id}" Header="" Width="0"/>
                    <GridViewColumn DisplayMemberBinding="{Binding SaleDateTime, StringFormat='yyyy-MM-dd'}" Header="Date" Width="120"/>
                    <GridViewColumn DisplayMemberBinding="{Binding InvoiceNumber}" Header="Invoice" Width="120"/>
                    <GridViewColumn DisplayMemberBinding="{Binding CustomerName}" Header="Name" Width="100"/>
                    <GridViewColumn DisplayMemberBinding="{Binding TotalAmount}" Header="Price" Width="90"/>
                    <GridViewColumn DisplayMemberBinding="{Binding TotalPay}" Header="Total Pay" Width="100"/>
                    <GridViewColumn DisplayMemberBinding="{Binding TotalDue}" Header="Total Due" Width="90"/>
                </GridView>
            </ListView.View>
        </ListView>


In UI add reference of BLL and ENTITY, then write code like this:

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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using BLL;
using ENTITY;

namespace JSMS.UI
{
    /// <summary>
    /// Interaction logic for SaleReportUI.xaml
    /// </summary>
    public partial class SaleReportUI : Window
    {
        List<SaleInfo> _allSaleListObj = new List<SaleInfo>();
        SaleManager _saleManagerObj = new SaleManager();
        public SaleReportUI()
        {
            InitializeComponent();

            LoadAllSaleList();
        }

        private void LoadAllSaleList()
        {
            _allSaleListObj = new List<SaleInfo>();
            _allSaleListObj = _saleManagerObj.LoadAllSaleList();

            SaleListView.Items.Clear();

            if (_allSaleListObj.Count > 0)
            {
                foreach (var item in _allSaleListObj)
                {
                    SaleListView.Items.Add(item);
                }
            }

        }
    }
}


Tuesday, December 23, 2014

WPF XAML Horizontal Menu


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
     <Menu Height="26" Name="topMenu" VerticalAlignment="Top" Margin="0,0,0,-5" FontFamily="arial">
                <MenuItem Header="File" Name="FileMenu">
                    <MenuItem Header="Shop Setting"  Name="mnuShopSetting" Background="Transparent" Click="mnuShopSetting_Click"  />
                    <Separator />
                    <MenuItem Header="Exit" Name="mnuExit" Click="mnuExit_Click"/>
                </MenuItem>

                <MenuItem Header="Supplier" Name="SupplierMenu">
                    <MenuItem x:Name="mnuNewSupplier" Header="New Supplier" Click="mnuNewSupplier_Click"  />
                    <Separator />
                    <MenuItem Header="Supplier Status Report"   />
                </MenuItem>
                </MenuItem>
            </Menu>

WPF ListView StringFormat

In XAML follow string formating like this way:


1
<GridViewColumn DisplayMemberBinding="{Binding PurchaseDate, StringFormat='yyyy-MM-dd'}" Header="Date" Width="100"/>

C# WPF, Access Parent Window Method from Child Window

For access parent window method from child window, first make parent window method as PUBLIC, then follow below code for child window.


 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
//Declear ParentWindow as GlobalPreview
        SupplierDueListUI globalPrewindow;

public SupplierDueCollectionUI(SupplierDueListUI prewindow)
        {
            InitializeComponent();
            globalPrewindow = prewindow; // Initial prewindow
        }

private void btnUpdatePayment_Click(object sender, RoutedEventArgs e)
        {
            PurchaseInfo purchaseInfoObj = new PurchaseInfo();

            purchaseInfoObj.Id = this._purchaseInfoObj.Id;
            purchaseInfoObj.ProductId = this._purchaseInfoObj.ProductId;
            purchaseInfoObj.SupplierId = this._purchaseInfoObj.SupplierId;
            purchaseInfoObj.TotalDue = Convert.ToInt32(txtTotalDue.Text);
            purchaseInfoObj.TotalPay = Convert.ToInt32(txtTotalPay.Text);

            if (purchaseInfoObj.TotalDue <= 0)
            {
                purchaseInfoObj.IsFullPaid = true;
            }

            _purchaseManagerObj.UpdatePurchasePayment(purchaseInfoObj);

            globalPrewindow.LoadAllPurchaseDueBill(); //Load Parent window method

            MessageBox.Show("Successfully Updated", "Success!", MessageBoxButton.OK, MessageBoxImage.Information);

            this.Close();
        }

Saturday, December 20, 2014

C# Generate an Unique code

This method use DateTime Ticks for generate an Unique 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
//Generate Unique code using DateTime
        static string uniqueCode()
        {
            string characters = "K13A56Z89I";
            string time = DateTime.UtcNow.Ticks.ToString();
            var code = "";
            for (var i = 0; i < characters.Length; i += 2)
            {
                if ((i + 2) <= time.Length)
                {
                    var number = int.Parse(time.Substring(i, 2));
                    if (number > characters.Length - 1)
                    {
                        var one = double.Parse(number.ToString().Substring(0, 1));
                        var two = double.Parse(number.ToString().Substring(1, 1));
                        code += characters[Convert.ToInt32(one)];
                        code += characters[Convert.ToInt32(two)];
                    }
                    else
                        code += characters[number];
                }
            }
            return code;
        }

C# Entity, Load Data from Child window to Parent window

In child window create an event and use code like this:

1
2
3
4
5
6
7
8
//Send data to Parent form
        private void CustomerInfoListView_MouseDoubleClick(object sender, MouseButtonEventArgs e)
        {
            if (CustomerInfoListView.SelectedIndex > -1)
            {
                this.DialogResult = true;
            }
        }

Now in Parent window create a button event for show child window and get data. Use code like this:


1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
//Get data from child window and set it to parent window
        private void btnGet_Click(object sender, RoutedEventArgs e)
        {
            CustomerInfoUI customerUI = new CustomerInfoUI(); //Child window object

            if (customerUI.ShowDialog() == true) // Set condition for show child window and do some operation
            {
                CustomerInfo customerObj = new CustomerInfo(); //Child window entity object
                customerObj = customerUI.CustomerInfoListView.SelectedItem as CustomerInfo; //Load child window listview data to entity
                txtCustomerName.Text = customerObj.CustomerName; // set data to parent window text field
                txtCustomerId.Text = customerObj.Id.ToString();// set data to parent window text field
                this._customerInfoObj.Id = customerObj.Id; // set child window ID to parent window ID

                customerUI.Close(); //Close child window
            }
        }


C# with Entity, Send Data from Parent Form to Child Form

In Child Form Create an entity object as public and set all form field with that entity property. Like This code:


1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
//Declare Purchase Entity Object as Public
        public PurchaseInfo _purchaseInfoObj = new PurchaseInfo();


//Load Purchase Data from Parent Window to Child Form
        public void LoadDataFromDueList()
        {
            txtProductID.Text = this._purchaseInfoObj.ProductId.ToString();
            txtProductName.Text = this._purchaseInfoObj.ProductName;
            txtSupplierName.Text = this._purchaseInfoObj.SupplierName;
            txtTotalPurchaseAmount.Text = this._purchaseInfoObj.TotalPurchaseAmount.ToString();
            txtTotalDue.Text = this._purchaseInfoObj.TotalDue.ToString();
        }


Now in Parent Form, create an Event and write code like this:


1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
//Collect Data from Listview and Send it to child form
        private void MakePaymentContexMenu_Click(object sender, RoutedEventArgs e)
        {
            if (PurchaseDueListView.SelectedIndex > -1)
            {
                SupplierDueCollectionUI supplierDueCollectionUI = new SupplierDueCollectionUI();
                PurchaseInfo purchaseObj = new PurchaseInfo();
                purchaseObj = PurchaseDueListView.SelectedItem as PurchaseInfo; // select and load data from listview

                supplierDueCollectionUI._purchaseInfoObj = purchaseObj; // send it to public entity _purchaseInfoObj
                supplierDueCollectionUI.LoadDataFromDueList(); // Call data load method from child form
                supplierDueCollectionUI.ShowDialog(); // show child form
            }
        }

Tuesday, November 25, 2014

WPF C# ComboBox with DataBind

Step - 1 (XAML)

1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
       <ComboBox HorizontalAlignment="Left" Margin="168,54,0,0" VerticalAlignment="Top" Width="197" x:Name="cmbProductCat">
            <ComboBox.ItemTemplate>
                <DataTemplate>
                    <StackPanel Orientation="Horizontal">
                        <TextBlock Text="{Binding CategoryTitle}" Width="60"/>
                        <!--<TextBlock Text="|"/>
                        <TextBlock Text="{Binding DepartmentName}" Width="60"/>-->

                    </StackPanel>
                </DataTemplate>
            </ComboBox.ItemTemplate>
        </ComboBox>

Step - 2 (UI Code)
1
2
3
4
5
6
7
8
9
private void LoadProductCatComboBox()
        {
            cmbProductCat.Items.Clear();
            _productCategoryInfoList = _productCategoryInfoManager.LoadAllProductCategoryInfo();
            foreach (ProductCategoryInfo pCatInf in _productCategoryInfoList)
            {
                cmbProductCat.ItemsSource = _productCategoryInfoList;
            }
        }

Step - 3 (BLL)
1
2
3
4
public List<ProductCategoryInfo> LoadAllProductCategoryInfo()
        {
            return productCatInfoGetway.LoadAllProductCategoryInfo();
        }

Step -4 (DAL)

1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
public List<ProductCategoryInfo> LoadAllProductCategoryInfo()
        {
            List<ProductCategoryInfo> productCategoryInfoList = new List<ProductCategoryInfo>();

            foreach (var p in (from c in dataContext.tbl_ProductCategories select c).Distinct())
            {
                ProductCategoryInfo productCatInfoObj = new ProductCategoryInfo();
                productCatInfoObj.Id = p.ID;
                productCatInfoObj.CategoryTitle = p.CategoriesTitle;

                productCategoryInfoList.Add(productCatInfoObj);
            }
            return productCategoryInfoList;
        }

WPF C# Data Save, Update and Delete with 3 layer architecture - Part 4(UI) - Final Part

In this Final Part we will design our UI and complete our Save, Delete & Update operation.
Our Final UI will look like the image below.

Here is XAML 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
<Grid>
  <Grid.RowDefinitions>
   <RowDefinition/>
  </Grid.RowDefinitions>
  <Grid.ColumnDefinitions>
   <ColumnDefinition/>
  </Grid.ColumnDefinitions>

  <Button x:Name="btnNewCustomer" Content="New Customer" HorizontalAlignment="Left" Margin="572,646,0,0" Width="125" Height="30" Click="btnNewCustomer_Click" VerticalAlignment="Top"/>
  <Button x:Name="btnSaveCustomer" Content="Save Customer" HorizontalAlignment="Left" Margin="727,646,0,0" Width="125" Height="30" Click="btnSaveCustomer_Click" VerticalAlignment="Top"/>
  <Button x:Name="btnDeleteCustomer" Content="Delete Customer" HorizontalAlignment="Left" Margin="891,646,0,0" Width="125" Height="30" Click="btnDeleteCustomer_Click" VerticalAlignment="Top"/>
  <Button x:Name="btnClose" Content="Close" HorizontalAlignment="Left" Margin="1057,646,0,0" Width="125" Height="30" Click="btnClose_Click" VerticalAlignment="Top"/>
  <Label Content="Customer Information Form" HorizontalAlignment="Left" Margin="447,9,0,0" VerticalAlignment="Top" Background="{x:Null}" Foreground="#FF41B1E1" FontFamily="Lucida Calligraphy" FontSize="34" FontWeight="Bold" FontStyle="Italic" Width="642" Height="55"/>
  <GroupBox Header="Customer List" HorizontalAlignment="Left" Margin="378,70,0,0" VerticalAlignment="Top" Height="557" Width="957"/>
  <ListView 
   HorizontalAlignment="Left" 
   Height="473" 
   Margin="399,139,0,0" 
   VerticalAlignment="Top" 
   Width="921"
   x:Name="CustomerInfoListView" MouseDoubleClick="CustomerInfoListView_MouseDoubleClick">
   <ListView.ContextMenu>
    <ContextMenu x:Name="lstCustomerInfo" StaysOpen="True" Background="WhiteSmoke">
     <ContextMenu.BitmapEffect>
      <BitmapEffectGroup/>
     </ContextMenu.BitmapEffect>
     <MenuItem Header="Edit" x:Name="EditProgramContexMenu" Click="EditProgramContexMenu_Click"/>
     <MenuItem Header="Rmove" x:Name="RemoveProgramContexMenu" Click="RemoveProgramContexMenu_Click"/>
    </ContextMenu>
   </ListView.ContextMenu>
   <ListView.View>
    <GridView AllowsColumnReorder="True">
     <GridViewColumn DisplayMemberBinding="{Binding Id}" Header="" Width="0"/>
     <GridViewColumn DisplayMemberBinding="{Binding CustomerName}" Header="Customer Name" Width="220"/>
     <GridViewColumn DisplayMemberBinding="{Binding CustomerAddress}" Header="Customer Address" Width="280"/>
     <GridViewColumn DisplayMemberBinding="{Binding CustomerMobile}" Header="Customer Mobile Number" Width="160"/>
     <GridViewColumn DisplayMemberBinding="{Binding CustomerEmail}" Header="Customer Email Address" Width="220"/>
    </GridView>
   </ListView.View>
  </ListView>
  <Label Content="Search Customer :" HorizontalAlignment="Left" Margin="396,101,0,0" VerticalAlignment="Top"/>
        <TextBox x:Name="txtCustomerSearch" Controls:TextboxHelper.Watermark="Search by Name" Controls:TextboxHelper.ClearTextButton="True" HorizontalAlignment="Left" Height="23" Margin="514,105,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="160"/>
  <GroupBox Header="Customer Information" HorizontalAlignment="Left" Height="557" Margin="10,70,0,0" VerticalAlignment="Top" Width="363">
   <Grid HorizontalAlignment="Left" Height="514" Margin="0,0,-2,0" VerticalAlignment="Top" Width="353">
    <Label Content="Customer Name :" HorizontalAlignment="Left" Margin="6,3,0,0" VerticalAlignment="Top" Height="26" Width="102" Foreground="Black" Background="{x:Null}" FontFamily="Arial Unicode MS"/>
                <TextBox x:Name="txtCustomerName" Controls:TextboxHelper.Watermark="Kazi Obaydollah" Controls:TextboxHelper.ClearTextButton="True" HorizontalAlignment="Left" Height="23" TextWrapping="Wrap" VerticalAlignment="Top" Width="200" Margin="147,6,0,0" TextChanged="txtCustomerName_TextChanged"/>
    <Label Content="Customer Address :" HorizontalAlignment="Left" Margin="6,34,0,0" VerticalAlignment="Top" Foreground="Black" Background="{x:Null}" FontFamily="Arial Unicode MS" Height="26" Width="116"/>
                <TextBox x:Name="txtCustomerAddress" Controls:TextboxHelper.Watermark="Dhaka" Controls:TextboxHelper.ClearTextButton="True" HorizontalAlignment="Left" Height="75" Margin="147,37,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="200" AcceptsReturn="True"/>
    <Label Content="Phone Number :" HorizontalAlignment="Left" Margin="6,116,0,0" VerticalAlignment="Top" Foreground="Black" Background="{x:Null}" FontFamily="Arial Unicode MS" Height="26" Width="97"/>
                <TextBox x:Name="txtCustomerPhone" Controls:TextboxHelper.Watermark="12345678912" Controls:TextboxHelper.ClearTextButton="True" HorizontalAlignment="Left" Height="23" Margin="147,119,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="200"/>
    <Label Content="Mobile Number :" HorizontalAlignment="Left" Margin="6,147,0,0" VerticalAlignment="Top" Foreground="Black" Background="{x:Null}" FontFamily="Arial Unicode MS" Height="26" Width="98"/>
                <TextBox x:Name="txtCustomerMobile" Controls:TextboxHelper.Watermark="12345678912" Controls:TextboxHelper.ClearTextButton="True" HorizontalAlignment="Left" Height="23" Margin="147,150,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="200"/>
    <Label Content="FAX Number :" HorizontalAlignment="Left" Margin="6,178,0,0" VerticalAlignment="Top" Foreground="Black" Background="{x:Null}" FontFamily="Arial Unicode MS" Height="26" Width="86"/>
                <TextBox x:Name="txtCustomerFax" Controls:TextboxHelper.Watermark="12345678912" Controls:TextboxHelper.ClearTextButton="True" HorizontalAlignment="Left" Height="23" Margin="147,181,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="200"/>
    <Label Content="Email Address :" HorizontalAlignment="Left" Margin="6,209,0,0" VerticalAlignment="Top" Foreground="Black" Background="{x:Null}" FontFamily="Arial Unicode MS" Height="26" Width="94"/>
                <TextBox x:Name="txtCustomerEmail" Controls:TextboxHelper.Watermark="info@4ditsolution.com" Controls:TextboxHelper.ClearTextButton="True" HorizontalAlignment="Left" Height="23" Margin="147,212,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="200"/>
    <Label Content="Note :" HorizontalAlignment="Left" Margin="6,240,0,0" VerticalAlignment="Top" Foreground="Black" Background="{x:Null}" FontFamily="Arial Unicode MS" Height="26" Width="42"/>
                <TextBox x:Name="txtCustomerNote" Controls:TextboxHelper.Watermark="Type Your Note Here" Controls:TextboxHelper.ClearTextButton="True" HorizontalAlignment="Left" Height="77" Margin="147,244,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="200" AcceptsReturn="True"/>
   </Grid>
  </GroupBox>

 </Grid>

And here is C# 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
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using MahApps.Metro.Controls;
using MahApps.Metro.Controls.Dialogs;
using BLL;
using ENTITY;

namespace JSMS.UI
{
    /// <summary>
    /// Interaction logic for CustomerInfo.xaml
    /// </summary>
    public partial class CustomerInfoUI : MetroWindow
    {
        CustomerInfo _customerInfoObj = new CustomerInfo();
        CustomerInfoManager _customerInfManagerObj = new CustomerInfoManager();
        List<CustomerInfo> _customerInfoList = new List<CustomerInfo>();


        public CustomerInfoUI()
        {
            InitializeComponent();
            LoadAllCustomerInfo();
            btnSaveCustomer.IsEnabled = false;
            btnDeleteCustomer.IsEnabled = false;
        }

        //Close Button Event
        private void btnClose_Click(object sender, RoutedEventArgs e)
        {
            test testWindow = new test();
            testWindow.Show();
            this.Close();
        }

        //New Button Event
        private void btnNewCustomer_Click(object sender, RoutedEventArgs e)
        {
            txtCustomerName.Text = "";
            txtCustomerAddress.Text = "";
            txtCustomerPhone.Text = "";
            txtCustomerMobile.Text = "";
            txtCustomerFax.Text = "";
            txtCustomerEmail.Text = "";
            txtCustomerNote.Text = "";
        }

        //Save Customer
        private void btnSaveCustomer_Click(object sender, RoutedEventArgs e)
        {
            if (btnSaveCustomer.Content.ToString() == "Save Customer")
            {
                _customerInfoObj.CustomerName = txtCustomerName.Text;
                _customerInfoObj.CustomerAddress = txtCustomerAddress.Text;
                _customerInfoObj.CustomerPhone = txtCustomerPhone.Text;
                _customerInfoObj.CustomerMobile = txtCustomerMobile.Text;
                _customerInfoObj.CustomerFax = txtCustomerFax.Text;
                _customerInfoObj.CustomerEmail = txtCustomerEmail.Text;
                _customerInfoObj.CustomerNote = txtCustomerNote.Text;

                _customerInfManagerObj.SaveCustomerInfo(_customerInfoObj);

                txtCustomerName.Text = "";
                txtCustomerAddress.Text = "";
                txtCustomerPhone.Text = "";
                txtCustomerMobile.Text = "";
                txtCustomerFax.Text = "";
                txtCustomerEmail.Text = "";
                txtCustomerNote.Text = "";

                this.ShowMessageAsync("Confirmation", "Customer data is successfully saved");
                //MessageBox.Show("Customer data is successfully saved", "Confirmation");
                LoadAllCustomerInfo();
            }

            if (btnSaveCustomer.Content.ToString() == "Update Customer") // Update Customer
            {
                _customerInfoObj.CustomerName = txtCustomerName.Text;
                _customerInfoObj.CustomerAddress = txtCustomerAddress.Text;
                _customerInfoObj.CustomerPhone = txtCustomerPhone.Text;
                _customerInfoObj.CustomerMobile = txtCustomerMobile.Text;
                _customerInfoObj.CustomerFax = txtCustomerFax.Text;
                _customerInfoObj.CustomerEmail = txtCustomerEmail.Text;
                _customerInfoObj.CustomerNote = txtCustomerNote.Text;

                _customerInfManagerObj.UpdateCustomerInfo(_customerInfoObj);

                txtCustomerName.Text = "";
                txtCustomerAddress.Text = "";
                txtCustomerPhone.Text = "";
                txtCustomerMobile.Text = "";
                txtCustomerFax.Text = "";
                txtCustomerEmail.Text = "";
                txtCustomerNote.Text = "";

                this.ShowMessageAsync("Confirmation", "Customer data is successfully Update");
                //MessageBox.Show("Customer data is successfully Update", "Confirmation");
                btnSaveCustomer.Content = "Save Customer";
                btnSaveCustomer.IsEnabled = false;
                
            }
            
        }

        //Load data to ListView
        private void LoadAllCustomerInfo()
        {
            _customerInfoList = new List<CustomerInfo>();

            _customerInfoList = _customerInfManagerObj.LoadAllCustomerInfo();
            CustomerInfoListView.Items.Clear();

            if (_customerInfoList.Count > 0)
            {
                foreach (var item in _customerInfoList)
                {
                    CustomerInfoListView.Items.Add(item);
                }
            }
        }

        //Edit Context Menu code
        private void EditProgramContexMenu_Click(object sender, RoutedEventArgs e)
        {
            btnSaveCustomer.IsEnabled = true;

            _customerInfoObj = new CustomerInfo();
            _customerInfoObj = ((CustomerInfo)CustomerInfoListView.SelectedItem);
            txtCustomerName.Text = _customerInfoObj.CustomerName;
            txtCustomerAddress.Text = _customerInfoObj.CustomerAddress;
            txtCustomerPhone.Text = _customerInfoObj.CustomerPhone;
            txtCustomerMobile.Text = _customerInfoObj.CustomerMobile;
            txtCustomerFax.Text = _customerInfoObj.CustomerFax;
            txtCustomerEmail.Text = _customerInfoObj.CustomerEmail;
            txtCustomerNote.Text = _customerInfoObj.CustomerNote;
            btnSaveCustomer.Content = "Update Customer";
        }

        //Remove data from database
        private void RemoveProgramContexMenu_Click(object sender, RoutedEventArgs e)
        {
            _customerInfoObj = new CustomerInfo();
            _customerInfoObj = ((CustomerInfo)CustomerInfoListView.SelectedItem);
            _customerInfManagerObj.DeleteCustomerInfo(_customerInfoObj);
            LoadAllCustomerInfo();

        }

        //Remove data from database
        private void btnDeleteCustomer_Click(object sender, RoutedEventArgs e)
        {
            _customerInfoObj = new CustomerInfo();
            _customerInfoObj = ((CustomerInfo)CustomerInfoListView.SelectedItem);
            _customerInfManagerObj.DeleteCustomerInfo(_customerInfoObj);
            LoadAllCustomerInfo();
        }


        private void txtCustomerName_TextChanged(object sender, TextChangedEventArgs e)
        {
            btnSaveCustomer.IsEnabled = true;
        }

        //Send data to another form
        private void CustomerInfoListView_MouseDoubleClick(object sender, MouseButtonEventArgs e)
        {
            if (CustomerInfoListView.SelectedIndex > -1)
            {
                this.DialogResult = true;
            }
        }
    }
}

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

In BLL class library, 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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using DAL;
using ENTITY;

namespace BLL
{
    public class CustomerInfoManager
    {
        CustomerInfoGetway customerInfoGetwayObj = new CustomerInfoGetway();
        public void SaveCustomerInfo(CustomerInfo _customerInfoObj)
        {
            customerInfoGetwayObj.SaveCustomerInfo(_customerInfoObj);
        }

        public List<CustomerInfo> LoadAllCustomerInfo()
        {
            return customerInfoGetwayObj.LoadAllCustomerInfo();
        }
        public void UpdateCustomerInfo(CustomerInfo _customerInfoObj)
        {
            customerInfoGetwayObj.UpdateCustomerInfo(_customerInfoObj);
        }

        public void DeleteCustomerInfo(CustomerInfo _customerInfoObj)
        {
            customerInfoGetwayObj.DeleteCustomerInfo(_customerInfoObj);
        }
    }
}

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();
        }

    }
}


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

In ENTITY class library project, add a class and name it CustomerInfo.cs or whatever you want.
Now Use this code to your Entity class.



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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ENTITY
{
    public class CustomerInfo
    {
        private int id;

        public int Id
        {
            get { return id; }
            set { id = value; }
        }

        private string customerName;

        public string CustomerName
        {
            get { return customerName; }
            set { customerName = value; }
        }

        private string customerAddress;

        public string CustomerAddress
        {
            get { return customerAddress; }
            set { customerAddress = value; }
        }

        private string customerPhone;

        public string CustomerPhone
        {
            get { return customerPhone; }
            set { customerPhone = value; }
        }

        private string customerMobile;

        public string CustomerMobile
        {
            get { return customerMobile; }
            set { customerMobile = value; }
        }

        private string customerFax;

        public string CustomerFax
        {
            get { return customerFax; }
            set { customerFax = value; }
        }

        private string customerEmail;

        public string CustomerEmail
        {
            get { return customerEmail; }
            set { customerEmail = value; }
        }

        private string customerNote;

        public string CustomerNote
        {
            get { return customerNote; }
            set { customerNote = value; }
        }

    }
}