-
Notifications
You must be signed in to change notification settings - Fork 1
/
AddBranch.cs
81 lines (72 loc) · 2.27 KB
/
AddBranch.cs
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
using DatabaseCodeFirst.contextDB;
using DatabaseCodeFirst.Models;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using testProj.Models;
namespace testProj
{
public partial class AddBranch : MetroFramework.Forms.MetroForm
{
DatabaseContext ctx = new DatabaseContext();
LoginUser loggedUser;
public AddBranch(LoginUser _loggedUser)
{
InitializeComponent();
loggedUser = _loggedUser;
}
private void metroButton1_Click(object sender, EventArgs e)
{
bool exists = false;
var Ids = ctx.Branches.Select(b => b.Id).ToArray();
int newId = Ids.Max() + 1;
foreach (var branch in ctx.Branches.ToList())
{
if (branch.BranchName == txtBranchName.Text)
{
exists = true;
}
}
if (exists)
{
MessageBox.Show("This branch exists");
}
else
{
ctx.Branches.Add(new Branch() { Id = newId, BranchName = txtBranchName.Text });
try
{
ctx.SaveChanges();
var messagaeResult = MessageBox.Show("Added successfully", null, MessageBoxButtons.OK);
if (messagaeResult == DialogResult.OK)
{
Dashboard dashboard = new Dashboard(loggedUser);
this.Hide();
dashboard.ShowDialog();
this.Close();
}
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
}
}
private void AddBranch_Load(object sender, EventArgs e)
{
}
private void AddBranch_FormClosed(object sender, FormClosedEventArgs e)
{
Dashboard dashboard = new Dashboard(loggedUser);
this.Hide();
dashboard.ShowDialog();
this.Close();
}
}
}