Advertisement
pabloducato

AddCompany

May 12th, 2019
360
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.96 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Net.Sockets;
  5. using System.Text;
  6. using CRUD.Models;
  7. using SQLite;
  8. using Xamarin.Forms;
  9. using CRUD.Views;
  10. using System.Linq;
  11.  
  12.  
  13. namespace CRUD.Views
  14. {
  15.     public class AddCompanyPage : ContentPage
  16.     {
  17.         private Entry _nameEntry;
  18.         private Entry _addressEntry;
  19.         private Button _saveButton;
  20.  
  21.  
  22.  
  23.         string _dbPath = Path.Combine(System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal), "myDB.db3");
  24.  
  25.         public AddCompanyPage()
  26.         {
  27.             this.Title = "Add Company";
  28.  
  29.             StackLayout stackLayout = new StackLayout();
  30.  
  31.             _nameEntry = new Entry();
  32.             _nameEntry.Keyboard = Keyboard.Text;
  33.             _nameEntry.Placeholder = "Company Name";
  34.             stackLayout.Children.Add(_nameEntry);
  35.  
  36.             _addressEntry = new Entry();
  37.             _addressEntry.Keyboard = Keyboard.Text;
  38.             _addressEntry.Placeholder = "Company Address";
  39.             stackLayout.Children.Add(_addressEntry);
  40.  
  41.             _saveButton = new Button();
  42.             _saveButton.Text = "Add";
  43.             _saveButton.Clicked += _saveButton_Clicked;
  44.             stackLayout.Children.Add(_saveButton);
  45.  
  46.             Content = stackLayout;
  47.  
  48.         }
  49.  
  50.         private async void _saveButton_Clicked(object sender, EventArgs e)
  51.         {
  52.             var db = new SQLiteConnection(_dbPath);
  53.             db.CreateTable<Company>();
  54.             var maxPk = db.Table<Company>().OrderByDescending(c => c.Id).FirstOrDefault();
  55.  
  56.             Company company = new Company()
  57.             {
  58.                 Id = (maxPk == null ? 1 : maxPk.Id + 1),
  59.                 Name = _nameEntry.Text,
  60.                 Address = _addressEntry.Text
  61.             };
  62.             db.Insert(company);
  63.             await DisplayAlert(null, company.Name + " Saved", "Ok");
  64.             await Navigation.PopAsync();
  65.  
  66.         }
  67.  
  68.        
  69.     }
  70. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement