-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathExpense.cs
131 lines (117 loc) · 4.32 KB
/
Expense.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
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
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace Quick_Start_Finance
{
public partial class Expense : Form
{
// HOME PC database connection
SqlConnection conn = new SqlConnection(@"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=C:\Users\vreed\Documents\QSFDb.mdf;Integrated Security=True;Connect Timeout=30");
// LAPTOP database connection
//SqlConnection conn = new SqlConnection(@"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=C:\Users\ryanl\Documents\QSFDb.mdf;Integrated Security=True;Connect Timeout=30");
public Expense()
{
InitializeComponent();
// Get total expense amount statistic
GetTotalExpense();
}
private void Clear()
{
expenseTitleTextBox.Text = "";
expenseAmountTextBox.Text = "";
expenseCategoryComboBox.SelectedIndex = 0;
expenseDescriptionTextBox.Text = "";
}
private void saveButton_Click(object sender, EventArgs e)
{
if (expenseTitleTextBox.Text == "" || expenseAmountTextBox.Text == "" || expenseDescriptionTextBox.Text == "" || expenseCategoryComboBox.SelectedIndex == -1)
{
MessageBox.Show("Missing information");
}
else
{
try
{
conn.Open();
SqlCommand cmd = new SqlCommand("INSERT INTO ExpenseTbl(Name, Amount, Category, Date, Description, Username) VALUES(@N, @A, @C, @D, @DE, @U)", conn);
cmd.Parameters.AddWithValue("@N", expenseTitleTextBox.Text);
cmd.Parameters.AddWithValue("@A", expenseAmountTextBox.Text);
cmd.Parameters.AddWithValue("@C", expenseCategoryComboBox.SelectedItem.ToString());
cmd.Parameters.AddWithValue("@D", expenseDateTimePicker.Value.Date);
cmd.Parameters.AddWithValue("@DE", expenseDescriptionTextBox.Text);
cmd.Parameters.AddWithValue("@U", Login.User);
cmd.ExecuteNonQuery();
MessageBox.Show("New expense record added!");
conn.Close();
GetTotalExpense();
Clear();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
}
private void GetTotalExpense()
{
try
{
conn.Open();
SqlDataAdapter sda = new SqlDataAdapter("SELECT SUM(Amount) FROM ExpenseTbl WHERE Username = '" + Login.User + "'", conn);
DataTable dt = new DataTable();
sda.Fill(dt);
totalExpenseAmountLabel.Text = $"{dt.Rows[0][0]:C}";
conn.Close();
}
catch (Exception ex)
{
conn.Close();
}
}
private void closeXPictureBox_Click(object sender, EventArgs e)
{
Application.Exit();
}
private void dashboardLabel_Click(object sender, EventArgs e)
{
Dashboard dashboard = new Dashboard();
dashboard.Show();
this.Hide();
}
private void incomeLabel_Click(object sender, EventArgs e)
{
Income income = new Income();
income.Show();
this.Hide();
}
private void viewIncomeLabel_Click(object sender, EventArgs e)
{
ViewIncome viewIncome = new ViewIncome();
viewIncome.Show();
this.Hide();
}
private void viewExpenseLabel_Click(object sender, EventArgs e)
{
ViewExpense viewExpense = new ViewExpense();
viewExpense.Show();
this.Hide();
}
private void logoutLabel_Click(object sender, EventArgs e)
{
Login login = new Login();
login.Show();
this.Hide();
}
private void exitLabel_Click(object sender, EventArgs e)
{
Application.Exit();
}
}
}