-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUpdateBookForm.cs
More file actions
243 lines (188 loc) · 7.52 KB
/
Copy pathUpdateBookForm.cs
File metadata and controls
243 lines (188 loc) · 7.52 KB
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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
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 MySql.Data.MySqlClient;
namespace Smart_Library_Control
{
public partial class UpdateBookForm : Form
{
public UpdateBookForm()
{
InitializeComponent();
}
private void comboBox4_SelectedIndexChanged(object sender, EventArgs e)
{
MySqlConnection con = new MySqlConnection("server=localhost;user id=root;database=tinylibrary");
MySqlCommand cmd;
MySqlDataReader mdr;
con.Open();
string selectQuery;
if (comboBox4.Text == "Category")
{
selectQuery = "SELECT name FROM category";
cmd = new MySqlCommand(selectQuery, con);
mdr = cmd.ExecuteReader();
comboBox5.Items.Clear();
while (mdr.Read())
{
comboBox5.Items.Add(mdr["name"].ToString());
}
}
else if (comboBox4.Text == "Author")
{
selectQuery = "SELECT DISTINCT writer_name FROM books";
cmd = new MySqlCommand(selectQuery, con);
mdr = cmd.ExecuteReader();
comboBox5.Items.Clear();
while (mdr.Read())
{
comboBox5.Items.Add(mdr["writer_name"].ToString());
}
}
else if (comboBox4.Text == "Entry Date")
{
selectQuery = "SELECT DISTINCT entry_date FROM books";
cmd = new MySqlCommand(selectQuery, con);
mdr = cmd.ExecuteReader();
comboBox5.Items.Clear();
while (mdr.Read())
{
comboBox5.Items.Add(mdr["entry_date"].ToString());
}
}
con.Close();
}
private void comboBox5_SelectedIndexChanged(object sender, EventArgs e)
{
MySqlConnection con = new MySqlConnection("server=localhost;user id=root;database=tinylibrary");
MySqlCommand cmd;
MySqlDataReader mdr;
con.Open();
string selectQuery;
if (comboBox4.Text == "Category")
{
selectQuery = "select books.name as bname from category, books where books.category_id = category.id and category.name = '" + comboBox5.Text + "'";
cmd = new MySqlCommand(selectQuery, con);
mdr = cmd.ExecuteReader();
comboBox6.Items.Clear();
while (mdr.Read())
{
comboBox6.Items.Add(mdr["bname"].ToString());
}
}
else if (comboBox4.Text == "Author")
{
selectQuery = "select name from books where writer_name = '" + comboBox5.Text + "'";
cmd = new MySqlCommand(selectQuery, con);
mdr = cmd.ExecuteReader();
comboBox6.Items.Clear();
while (mdr.Read())
{
comboBox6.Items.Add(mdr["name"].ToString());
}
}
else if (comboBox4.Text == "Entry Date")
{
selectQuery = "select name from books where entry_date = '" + comboBox5.Text + "'";
cmd = new MySqlCommand(selectQuery, con);
mdr = cmd.ExecuteReader();
comboBox6.Items.Clear();
while (mdr.Read())
{
comboBox6.Items.Add(mdr["name"].ToString());
}
}
con.Close();
}
private void comboBox6_SelectedIndexChanged(object sender, EventArgs e)
{
MySqlConnection con = new MySqlConnection("server=localhost;user id=root;database=tinylibrary");
MySqlCommand cmd;
MySqlDataReader mdr;
con.Open();
string selectQuery;
string bookName = comboBox6.Text;
selectQuery = "select books.id as bid, books.name as bname, books.publish_year as pyear, books.writer_name as bauthor, books.quantity as bquantity, category.name as bcategory from books, category where books.category_id = category.id and books.name = '" + bookName + "'";
cmd = new MySqlCommand(selectQuery, con);
mdr = cmd.ExecuteReader();
mdr.Read();
textBox1.Text = mdr["bid"].ToString();
textBox2.Text = mdr["bname"].ToString();
textBox3.Text = mdr["pyear"].ToString();
textBox4.Text = mdr["bauthor"].ToString();
textBox5.Text = mdr["bquantity"].ToString();
comboBox1.Text = mdr["bcategory"].ToString();
con.Close();
}
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
MySqlConnection con = new MySqlConnection("server=localhost;user id=root;database=tinylibrary");
MySqlCommand cmd;
MySqlDataReader mdr;
con.Open();
string selectQuery = "select id from category where name = '" + comboBox1.Text + "'";
cmd = new MySqlCommand(selectQuery, con);
mdr = cmd.ExecuteReader();
while (mdr.Read())
{
textBox6.Text = mdr["id"].ToString();
}
con.Close();
}
private void button1_Click(object sender, EventArgs e)
{
try
{
MySqlConnection con = new MySqlConnection("server=localhost;user id=root;database=tinylibrary");
MySqlCommand cmd;
MySqlDataReader mdr;
con.Open();
string selectQuery = "update books set name = '" + textBox2.Text + "', publish_year = " + textBox3.Text + ", writer_name = '" + textBox4.Text + "', quantity = " + textBox5.Text + ", category_id = " + textBox6.Text + " where id = " + textBox1.Text;
cmd = new MySqlCommand(selectQuery, con);
mdr = cmd.ExecuteReader();
MessageBox.Show("Book Updated!", "Successful", MessageBoxButtons.OK, MessageBoxIcon.Information);
while (mdr.Read())
{ }
con.Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
private void UpdateBookForm_Load(object sender, EventArgs e)
{
comboBox4.Items.Add("Author");
comboBox4.Items.Add("Category");
comboBox4.Items.Add("Entry Date");
MySqlConnection con = new MySqlConnection("server=localhost;user id=root;database=tinylibrary");
MySqlCommand cmd;
MySqlDataReader mdr;
con.Open();
string selectQuery = "select name from category";
cmd = new MySqlCommand(selectQuery, con);
mdr = cmd.ExecuteReader();
comboBox1.Items.Clear();
while (mdr.Read())
{
comboBox1.Items.Add(mdr["name"].ToString());
}
con.Close();
}
private void button3_Click(object sender, EventArgs e)
{
textBox2.Text = "";
textBox3.Text = "";
textBox4.Text = "";
textBox5.Text = "";
textBox6.Text = "";
comboBox1.Text = "";
}
}
}