-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAssign.aspx.cs
206 lines (172 loc) · 6.33 KB
/
Assign.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
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.Configuration;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class Assign : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
ddlMemberID.DataSource = getMembers();
ddlMemberID.DataBind();
ddlISBN.DataSource = getBooks();
ddlISBN.DataBind();
txtDateBorrow.Text = DateTime.Now.ToString();
if(getMembers().Count == 0 || getBooks().Count == 0)
{
lblErrorMessage.Text = "Please add a member or book to the database to do an assignment";
btnAssignment.Enabled = false;
}
}
}
public void btnAssign(object sender,EventArgs e)
{
assignBookMember();
//Update list of ISBN
ddlISBN.DataSource = getBooks();
ddlISBN.DataBind();
if (getBooks().Count == 0)
{
lblErrorMessage.Text = "No more books are left to assign, please add more";
btnAssignment.Enabled = false;
}
}
public bool checkISBN(String ISBN)
{
//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("Select_Assign_By_ISBN", 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 = ISBN;
//Try to open a connection to the Database using the connection string
con.Open();
SqlDataReader reader = cmd.ExecuteReader();
//If no data was found using the card number then return false
if (!reader.HasRows)
{
con.Close();
return false;
}
else
{
con.Close();
return true;
}
}
catch (Exception ex)
{
Console.Write(ex.Message);
return false;
}
}
public int assignBookMember()
{
//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("Insert_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;
cmd.Parameters.Add("@DateBorrowed", SqlDbType.VarChar).Value = txtDateBorrow.Text;
cmd.Parameters.Add("@NumberDays", SqlDbType.Int).Value = Int32.Parse(txtDays.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();
lblErrorMessage.Text = "";
con.Close();
//Update dataset
GridView1.DataBind();
return Int32.Parse(res);
}
catch (Exception ex)
{
lblErrorMessage.Text = "ERROR : Could not Insert - That Book ISBN is already in the database :)";
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())
{
if (checkISBN((String)dr["ISBN"]) == false)
{
bookList.Add((String)dr["ISBN"]);
}
}
dr.Close();
dr.Dispose();
command.Cancel();
}
catch (Exception err)
{
Console.Write(err.Message);
}
finally
{
connection.Close();
}
return bookList;
}
}