-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathForm5.cs
179 lines (146 loc) · 5.89 KB
/
Form5.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
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
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 Group_11___COSC_31112
{
public partial class deleteAcc : Form
{
public deleteAcc()
{
InitializeComponent();
}
private void btnDelAcc_Click(object sender, EventArgs e)
{
DataRowView selectedRow = (DataRowView)cmbAccName.SelectedItem;
string accountName = selectedRow["Account_name"].ToString();
string selected_Acc = cmbAccName.Text;
int acc_id = GetAccountId(accountName);
string connectionString = "Data Source=(localdb)\\Local;Initial Catalog=projectDatabase;Integrated Security=True";
using (SqlConnection connection = new SqlConnection(connectionString))
{
connection.Open();
deleteAccount(selected_Acc, connection, acc_id);
connection.Close();
}
}
private void deleteAcc_Load(object sender, EventArgs e)
{
//Load the database
string connectionString = "Data Source=(localdb)\\Local;Initial Catalog=projectDatabase;Integrated Security=True";
using (SqlConnection connection = new SqlConnection(connectionString))
{
try
{
connection.Open();
string sqlquery = "SELECT Account_name FROM Account";
using (SqlCommand command = new SqlCommand(sqlquery, connection))
{
using (SqlDataReader reader = command.ExecuteReader())
{
// Load data into a DataTable
DataTable dataTable = new DataTable();
dataTable.Load(reader);
// Set the DataSource of the ComboBox to the DataTable
cmbAccName.DataSource = dataTable;
cmbAccName.DisplayMember = "Account_name";
cmbAccName.ValueMember = "Account_name";
}
}
connection.Close(); // Close the connection with the database
}
catch (Exception ex)
{
MessageBox.Show("Error: " + ex.Message, "Database Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
}
private void deleteAccount(string AccName, SqlConnection connection, int AccId)
{
SqlTransaction transaction = null;
try
{
transaction = connection.BeginTransaction();
string querySubtaskDelete = "DELETE FROM Subtask WHERE Account_id=@accountId";
string queryAccountDelete = "DELETE FROM Account WHERE Account_name = @accountName";
using (SqlCommand cmd = new SqlCommand(querySubtaskDelete, connection, transaction))
{
cmd.Parameters.AddWithValue("@accountId", AccId);
cmd.ExecuteNonQuery();
}
using (SqlCommand cmd = new SqlCommand(queryAccountDelete, connection, transaction))
{
cmd.Parameters.AddWithValue("@accountName", AccName);
cmd.ExecuteNonQuery();
}
transaction.Commit();
MessageBox.Show("Account Deleted successfully.", "Account Deleted", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
catch (Exception e)
{
transaction?.Rollback();
MessageBox.Show("Error occured in deleteAccount() method : " + e.ToString());
}
finally
{
transaction?.Dispose();
}
}
private int GetAccountId(string accountName)
{
int accId = 0;
string connectionString = "Data Source=(localdb)\\Local;Initial Catalog=projectDatabase;Integrated Security=True";
using (SqlConnection connection = new SqlConnection(connectionString))
{
connection.Open();
using (SqlCommand cmd = new SqlCommand("SELECT Account_id FROM Account WHERE Account_name = @accountName", connection))
{
cmd.Parameters.AddWithValue("@accountName", accountName);
object result = cmd.ExecuteScalar();
if (result != null && result != DBNull.Value)
{
accId = Convert.ToInt32(result);
}
}
}
//MessageBox.Show("The Account Id is" + accId);
return accId;
}
private void newAccountToolStripMenuItem_Click(object sender, EventArgs e)
{
this.Hide();
Home form2 = new Home();
form2.Show();
}
private void newAccountToolStripMenuItem1_Click(object sender, EventArgs e)
{
this.Hide();
addNewAcc form3 = new addNewAcc();
form3.Show();
}
private void addSubTaskToolStripMenuItem_Click(object sender, EventArgs e)
{
this.Hide();
frmAddNewSubT form4 = new frmAddNewSubT();
form4.Show();
}
private void deleteSubtaskToolStripMenuItem_Click(object sender, EventArgs e)
{
this.Hide();
Form6 form6 = new Form6();
form6.Show();
}
private void getDetailedReportToolStripMenuItem_Click_1(object sender, EventArgs e)
{
this.Hide();
Home form2 = new Home();
form2.Show();
}
}
}