-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathReturn.aspx.cs
271 lines (226 loc) · 8.11 KB
/
Return.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
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Net.Mail;
using System.Web;
using System.Web.Configuration;
using System.Web.Mail;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class Return : System.Web.UI.Page
{
int daysBorrowed;
DateTime dateBorrowed = DateTime.Now;
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
btnEmail.Enabled = false;
ddlMemberID.DataSource = getMembers();
ddlMemberID.DataBind();
ddlISBN.DataSource = getBooks();
ddlISBN.DataBind();
if(getMembers().Count == 0 || getBooks().Count == 0)
{
lblErrorMessage.Text = "Please add a member or book to the database to do a return";
btnReturnBook.Enabled = false;
}
}
}
public void sendEmail(object sender,EventArgs e)
{
try
{
lblMail.Text = "Sending EMail...";
SmtpClient SmtpServer = new SmtpClient("smtp.live.com",25);
var mail = new System.Net.Mail.MailMessage();
mail.From = new MailAddress("rasheed_andrews@live.com");
mail.To.Add(txtEmail.Text);
mail.Subject = "Overdue Book";
// mail.IsBodyHtml = true;
string htmlBody;
htmlBody = "Your book is overdue, please report to the library";
mail.Body = htmlBody;
//SmtpServer.Port = 587;
SmtpServer.UseDefaultCredentials = false;
SmtpServer.Credentials = new System.Net.NetworkCredential("rasheed_andrews@live.com", "walleagle10");
SmtpServer.EnableSsl = true;
SmtpServer.Send(mail);
lblMail.Text = "Mail Sent";
}
catch(Exception err)
{
Console.Write(err.Message);
Console.Write("FF");
}
}
public void btnReturn(object sender,EventArgs e)
{
getAssignDate();
if (daysBorrowed != -1 && returnBook()!=0)
{
DateTime actualReturnDate = Calendar1.SelectedDate;
DateTime expectedReturnDate = dateBorrowed.AddDays(daysBorrowed);
//No Date Selected
if (Calendar1.SelectedDate.Date == DateTime.MinValue)
{
actualReturnDate = DateTime.Now;
}
int result = DateTime.Compare(actualReturnDate, expectedReturnDate);
txtDateBorrowed.Text = "Date Borrowed : " + dateBorrowed.ToString();
txtDaysBorrowed.Text = "Days Borrowed : " + daysBorrowed.ToString();
txtExpectedReturn.Text = "Expected Return Date : " + expectedReturnDate.ToString();
txtActualReturn.Text = "Actual Return Date : " + actualReturnDate.ToString();
//enable email
btnEmail.Enabled = true;
if (result < 0)
{
txtResult.Text = "Book is NOT overdue";
}
else if (result == 0)
{
txtResult.Text = "Book returned on time";
}
else if (result > 0)
{
txtResult.Text = "Due Date has passed";
}
lblErrorMessage.Text = "";
}
else
{
txtDateBorrowed.Text = "Please select date";
txtDaysBorrowed.Text = "";
txtExpectedReturn.Text = "";
txtActualReturn.Text = "";
txtResult.Text = "";
lblErrorMessage.Text = "Error : Could not return - That Book assignment does not exist :)";
}
}
public int returnBook()
{
//Retrieve Database Connection String from Web Config file
SqlConnection con = new SqlConnection(
WebConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString);
try
{
//Specify the name of the Stored Procedure that we want to use
SqlCommand cmd = new SqlCommand("Delete_Assign", con);
//After Specifying the name, we must also inidicate to the system that we will be executing a Stored Procedure
cmd.CommandType = CommandType.StoredProcedure;
//Pass data from ASP page to our Stored Procedure which will then be stored in Database
cmd.Parameters.Add("@ISBN", SqlDbType.VarChar).Value = ddlISBN.SelectedItem.Text;
cmd.Parameters.Add("@MemberID", SqlDbType.VarChar).Value = ddlMemberID.SelectedItem.Text;
//Try to open a connection to the Database using the connection string
con.Open();
//Execute the Stored Procedure and get the Auto Incremented ID of the customer
String res = cmd.ExecuteNonQuery().ToString();
con.Close();
//Update dataset
GridView1.DataBind();
return Int32.Parse(res);
}
catch (Exception ex)
{
lblErrorMessage.Text = "ERROR : Failed";
Console.Write(ex.Message);
return -1;
}
}
public List<String> getMembers()
{
List<String> memberList = new List<String>();
SqlDataReader dr = null;
const string storedProcedureName = "Select_Member";
SqlConnection connection = new SqlConnection(
WebConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString);
SqlCommand command = new SqlCommand(storedProcedureName, connection);
command.CommandType = CommandType.StoredProcedure;
try
{
connection.Open();
dr = command.ExecuteReader();
while (dr.Read())
{
memberList.Add((String)dr["MemberID"]);
}
dr.Close();
dr.Dispose();
command.Cancel();
}
catch (Exception err)
{
Console.Write(err.Message);
}
finally
{
connection.Close();
}
return memberList;
}
public List<String> getBooks()
{
List<String> bookList = new List<String>();
SqlDataReader dr = null;
const string storedProcedureName = "Select_Book";
SqlConnection connection = new SqlConnection(
WebConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString);
SqlCommand command = new SqlCommand(storedProcedureName, connection);
command.CommandType = CommandType.StoredProcedure;
try
{
connection.Open();
dr = command.ExecuteReader();
while (dr.Read())
{
bookList.Add((String)dr["ISBN"]);
}
dr.Close();
dr.Dispose();
command.Cancel();
}
catch (Exception err)
{
Console.Write(err.Message);
}
finally
{
connection.Close();
}
return bookList;
}
public void getAssignDate()
{
daysBorrowed = -1;
SqlDataReader dr = null;
const string storedProcedureName = "Select_Assign_By_ISBN";
SqlConnection connection = new SqlConnection(
WebConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString);
SqlCommand command = new SqlCommand(storedProcedureName, connection);
command.CommandType = CommandType.StoredProcedure;
command.Parameters.Add("@ISBN", SqlDbType.VarChar).Value = ddlISBN.SelectedItem.Text;
try
{
connection.Open();
dr = command.ExecuteReader();
while (dr.Read())
{
daysBorrowed = (int)dr["NumberDays"];
dateBorrowed = (DateTime)dr["DateBorrowed"];
}
dr.Close();
dr.Dispose();
command.Cancel();
}
catch (Exception err)
{
Console.Write(err.Message);
}
finally
{
connection.Close();
}
}
}