-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBookIssue.aspx.cs
321 lines (287 loc) · 10.4 KB
/
BookIssue.aspx.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
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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
namespace e_Library_mgmt
{
public partial class WebForm7 : System.Web.UI.Page
{
string Conx = ConfigurationManager.ConnectionStrings["cLibrary"].ConnectionString;
protected void Page_Load(object sender, EventArgs e)
{
Session["role"] = "admin";
GridView1.DataBind();
}
//Search Button
protected void Button1_Click(object sender, EventArgs e)
{
GetNames();
}
//Issue Button
protected void Button2_Click(object sender, EventArgs e)
{
if (checkIfBookExist() && checkIfMemberExist())
{
if (checkIfIssueEntryExist())
{
Response.Write("<script>alert('This Member already has this book');</script>");
}
else
{
IssueBook();
}
}
else
{
Response.Write("<script>alert('Wrong Book ID or Member ID');</script>");
}
}
//IssueBooks
void IssueBook()
{
try
{
SqlConnection con = new SqlConnection(Conx);
if (con.State == ConnectionState.Closed)
{
con.Open();
}
SqlCommand cmd = new SqlCommand("INSERT INTO Book_Issue_tbl(MemberId,member_name,book_id,book_name," +
"issue_date,due_date) values(@MemberId,@member_name,@book_id,@book_name,@issue_date,@due_date)", con);
cmd.Parameters.AddWithValue("@MemberId", TextBox1.Text.Trim());
cmd.Parameters.AddWithValue("@member_name", TextBox3.Text.Trim());
cmd.Parameters.AddWithValue("@book_id", TextBox2.Text.Trim());
cmd.Parameters.AddWithValue("@book_name", TextBox4.Text.Trim());
cmd.Parameters.AddWithValue("@issue_date", TextBox5.Text.Trim());
cmd.Parameters.AddWithValue("@due_date", TextBox6.Text.Trim());
cmd.ExecuteNonQuery();
cmd = new SqlCommand("update book_master_tbl set current_stock = current_stock-1 WHERE book_id='" + TextBox2.Text.Trim() + "'", con);
cmd.ExecuteNonQuery();
con.Close();
Response.Write("<script>alert('Book Issued Successfully');</script>");
ClearForm();
GridView1.DataBind();
}
catch (Exception ex)
{
Response.Write("<script>alert('" + "IssueBook" + ex.Message + "');</script>");
}
}
//Return Button
protected void Button3_Click(object sender, EventArgs e)
{
if (checkIfBookExist() && checkIfMemberExist())
{
if (checkIfIssueEntryExist())
{
ReturnBook();
}
else
{
Response.Write("<script>alert('This Entry does not exist');</script>");
}
}
else
{
Response.Write("<script>alert('Wrong Book ID or Member ID');</script>");
}
}
void ReturnBook()
{
try
{
SqlConnection con = new SqlConnection(Conx);
if (con.State == ConnectionState.Closed)
{
con.Open();
}
SqlCommand cmd = new SqlCommand("Delete from book_issue_tbl WHERE book_id='" + TextBox2.Text.Trim() + "' AND MemberId='" + TextBox1.Text.Trim() + "'", con);
int result = cmd.ExecuteNonQuery();
if (result > 0)
{
cmd = new SqlCommand("update book_master_tbl set current_stock = current_stock+1 WHERE book_id='" + TextBox2.Text.Trim() + "'", con);
cmd.ExecuteNonQuery();
con.Close();
Response.Write("<script>alert('Book Returned Successfully');</script>");
ClearForm();
GridView1.DataBind();
con.Close();
}
else
{
Response.Write("<script>alert('Error - Invalid details');</script>");
}
}
catch (Exception ex)
{
Response.Write("<script>alert('" + "ReturnBook" + ex.Message + "');</script>");
}
}
bool checkIfBookExist()
{
try
{
SqlConnection con = new SqlConnection(Conx);
if (con.State == ConnectionState.Closed)
{
con.Open();
}
SqlCommand cmd = new SqlCommand("select * from book_master_tbl WHERE book_id='" + TextBox2.Text.Trim() + "' AND current_stock >0", con);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
da.Fill(dt);
if (dt.Rows.Count >= 1)
{
return true;
}
else
{
return false;
}
}
catch (Exception ex)
{
return false;
}
}
bool checkIfMemberExist()
{
try
{
SqlConnection con = new SqlConnection(Conx);
if (con.State == ConnectionState.Closed)
{
con.Open();
}
SqlCommand cmd = new SqlCommand("select full_name from member_master_tbl WHERE Member_Id='" + TextBox1.Text.Trim() + "'", con);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
da.Fill(dt);
if (dt.Rows.Count >= 1)
{
return true;
}
else
{
return false;
}
}
catch (Exception ex)
{
return false;
}
}
bool checkIfIssueEntryExist()
{
try
{
SqlConnection con = new SqlConnection(Conx);
if (con.State == ConnectionState.Closed)
{
con.Open();
}
SqlCommand cmd = new SqlCommand("select * from book_issue_tbl WHERE MemberId='" + TextBox1.Text.Trim() + "' AND book_id='" + TextBox2.Text.Trim() + "'", con);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
da.Fill(dt);
if (dt.Rows.Count >= 1)
{
return true;
}
else
{
return false;
}
}
catch (Exception ex)
{
return false;
}
}
void GetNames()
{
try
{
SqlConnection con = new SqlConnection(Conx); //Checks for two executed cmds, then return data per cmd
if (con.State == ConnectionState.Closed)
{
con.Open();
}
SqlCommand cmd = new SqlCommand("select book_name from book_master_tbl WHERE book_id='" + TextBox2.Text.Trim() + "'", con);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
da.Fill(dt);
if (dt.Rows.Count >= 1)
{
TextBox4.Text = dt.Rows[0]["book_name"].ToString();
}
else
{
Response.Write("<script>alert('Wrong Book ID');</script>");
}
SqlCommand cmdr = new SqlCommand("select Full_Name from Member_Master_tbl WHERE Member_Id='" + TextBox1.Text.Trim() + "'", con);
SqlDataAdapter dar = new SqlDataAdapter(cmdr);
DataTable dtr = new DataTable();
dar.Fill(dtr);
if (dtr.Rows.Count >= 1)
{
TextBox3.Text = dtr.Rows[0]["Full_Name"].ToString();
}
else
{
Response.Write("<script>alert('Wrong User ID');</script>");
}
SqlCommand cmdd = new SqlCommand("select Full_Name from Book_Issue_tbl WHERE Member_Id='" + TextBox1.Text.Trim() + "'", con);
SqlDataAdapter dda = new SqlDataAdapter(cmdd);
DataTable ddr = new DataTable();
dda.Fill(ddr);
if (ddr.Rows.Count >= 1)
{
TextBox5.Text = ddr.Rows[0]["Issue_Date"].ToString();
TextBox6.Text = ddr.Rows[0]["Due_Date"].ToString();
}
else
{
Response.Write("<script>alert('Date not Found');</script>");
}
}
catch (Exception ex)
{
Response.Write("<script>alert('" + "GetNames" + ex.Message + "');</script>");
}
}
void ClearForm()
{
TextBox1.Text = "";
TextBox2.Text = "";
TextBox4.Text = "";
TextBox5.Text = "";
TextBox6.Text = "";
}
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
try
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
//Check your condition here
DateTime dt = Convert.ToDateTime(e.Row.Cells[5].Text);
DateTime today = DateTime.Today;
if (today > dt)
{
e.Row.BackColor = System.Drawing.Color.Tomato;
}
}
}
catch (Exception ex)
{
Response.Write("<script>alert('" + "DueDate" + ex.Message + "');</script>");
}
}
}
}