/// <summary> /// Gets userId from the session and pulls the row from the user table for the selected user to be used to dynamically /// populate the users name for the greeting message, and decide which buttons to show the user based on their Account Type /// </summary> /// <param name="sender"></param> /// <param name="e"></param> protected void Page_Load(object sender, EventArgs e) { try { DataRow dr = UserTool.GetUserInfo(Session["User"].ToString()); if (dr == null) { Response.Redirect("default.aspx"); } else { lblWelcome.Text = "Welcome " + dr["FirstName"].ToString() + ","; } if (dr["AccountType"].ToString() == "Administrator") { btnAdminPage.Visible = true; } else if (dr["AccountType"].ToString() == "Librarian") { btnLibrarianPage.Visible = true; } else { btnMemberPage.Visible = true; } } catch { Response.Redirect("default.aspx"); } }
/// <summary> /// Gets userId from the session and pulls the row from the user table for the selected user to be used to dynamically /// populate the page with the user's information /// </summary> /// <param name="sender"></param> /// <param name="e"></param> protected void Page_Load(object sender, EventArgs e) { try { userRow = UserTool.GetUserInfo(Session["User"].ToString()); if (userRow == null) { Response.Redirect("default.aspx"); } else { if (!Page.IsPostBack) { tbFName.Text = userRow["FirstName"].ToString(); tblName.Text = userRow["LastName"].ToString(); tbAddress.Text = userRow["Address"].ToString(); tbEmail.Text = userRow["Email"].ToString(); ddProfilePic.SelectedIndex = Convert.ToInt32(userRow["ProfilePicture"].ToString()); imgProfilePic.ImageUrl = "Images/" + userRow["ProfilePicture"].ToString() + ".png"; } } } catch { Response.Redirect("default.aspx"); } }
/// <summary> /// Sets the amount owing for the label to inform the user /// </summary> private void amountOwing() { try { DataRow dr = UserTool.GetUserInfo(Session["User"].ToString()); if (dr == null) { Response.Redirect("default.aspx"); } else { if (dr["AccountType"].ToString().Equals("Member")) { string sqlCommand = "SELECT AmountOwing FROM [User] WHERE UserId = @UserId"; conn.ConnectionString = conString; SqlCommand cmd = conn.CreateCommand(); int userId = Convert.ToInt32(dr["UserId"].ToString()); try { cmd.CommandText = sqlCommand; conn.Open(); SqlParameter userIdParam = new SqlParameter(); userIdParam.ParameterName = "@UserId"; userIdParam.Value = userId; cmd.Parameters.Add(userIdParam); SqlDataReader reader = cmd.ExecuteReader(); string fee = " "; while (reader.Read()) { fee = reader.GetDouble(0).ToString(); lblOwing.Text = "Amount Owing: $" + fee; } reader.Close(); } catch (Exception ex) { ClientScript.RegisterClientScriptBlock(this.GetType(), "Error", "alert('An Error has occoured')", true); } finally { cmd.Dispose(); conn.Close(); } } else { Response.Redirect("default.aspx"); } } } catch (Exception ex) { Response.Redirect("default.aspx"); } }
/// <summary> /// Assigns the selected book from the grid view to the specific user in the row /// </summary> /// <param name="sender"></param> /// <param name="e"></param> protected void btnAssign_Click(object sender, EventArgs e) { if (GridView1.SelectedIndex == -1) { ClientScript.RegisterClientScriptBlock(this.GetType(), "Error", "alert('An Error has occoured')", true); } else { int requestId = Convert.ToInt32(GridView1.SelectedRow.Cells[1].Text); int issueId = Convert.ToInt32(GridView1.SelectedRow.Cells[12].Text); DataRow dr = UserTool.GetUserInfo(Session["User"].ToString()); if (dr["AccountType"].ToString().Equals("Librarian")) { if (dr == null) { Response.Redirect("default.aspx"); } else { int[] ids = grabRequest(requestId); // grabs info to insert for rental DateTime rentalDate = DateTime.Now; DateTime dueDate = rentalDate.AddDays(7); string sqlCommand = "INSERT INTO Rental (IssueId, UserId, RentalDate, DueDate, Fees) VALUES (" + ids[0] + "," + ids[1] + ",'" + rentalDate.ToString() + "','" + dueDate.ToString() + "'," + 0 + ")"; conn.ConnectionString = conString; SqlCommand cmd = conn.CreateCommand(); try { cmd.CommandText = sqlCommand; conn.Open(); cmd.ExecuteScalar(); } catch (Exception ex) { ClientScript.RegisterClientScriptBlock(this.GetType(), "Error", "alert('An Error has occoured')", true); } finally { cmd.Dispose(); conn.Close(); } issueStatusChange(issueId); deleteRequest(requestId); dataBindGrid(); ClientScript.RegisterClientScriptBlock(this.GetType(), "Request has been Approved", "alert('The Book has been Approved!')", true); } } else { Response.Redirect("default.aspx"); } } }
/// <summary> /// Sets it up to request the book /// </summary> /// <param name="sender"></param> /// <param name="e"></param> protected void btnRequest_Click(object sender, EventArgs e) { try { DataRow dr = UserTool.GetUserInfo(Session["User"].ToString()); if (dr == null) { Response.Redirect("default.aspx"); } else { int memberID = Convert.ToInt32(dr["UserId"].ToString()); int bookRentals = booksRented(memberID); if (bookRentals >= Convert.ToInt32(dr["BookLimit"].ToString())) { ClientScript.RegisterClientScriptBlock(this.GetType(), "Max Book Limit", "alert('You have reached the limit of books rented, please return a book to request this book')", true); } else { DateTime dateReq = DateTime.Now; int bookId = Convert.ToInt32(GridView1.SelectedRow.Cells[1].Text); //grabs the book id from the gridview int issueId = findIssue(bookId); //grabs all issues that have an available issue for the corresponding book // string sqlCommand = "INSERT INTO Request VALUES ('" + dateReq.ToString() + "', " + issueId + ", " + memberID + ")"; conn.ConnectionString = conString; SqlCommand cmd = conn.CreateCommand(); try { cmd.CommandText = sqlCommand; conn.Open(); cmd.ExecuteScalar(); } catch (Exception ex) { ClientScript.RegisterClientScriptBlock(this.GetType(), "Error", "alert('An Error has occoured')", true); } finally { cmd.Dispose(); conn.Close(); } changeStatus(issueId); ClientScript.RegisterClientScriptBlock(this.GetType(), "Book Requested", "alert('The Book has been requested!')", true); } } } catch (Exception ex) { Response.Redirect("default.aspx"); } }
/// <summary> /// changes the status for the issue that its now loaned /// </summary> /// <param name="issueId"></param> private void issueStatusChange(int issueId) { DataRow dr = UserTool.GetUserInfo(Session["User"].ToString()); if (dr["AccountType"].ToString().Equals("Librarian")) { if (dr == null) { Response.Redirect("default.aspx"); } else { int[] ids = grabRequest(issueId); DateTime rentalDate = DateTime.Now; DateTime dueDate = rentalDate.AddDays(7); string sqlCommand = "UPDATE Issue SET Status = 'On Loan' WHERE IssueId = @issueId"; conn.ConnectionString = conString; SqlCommand cmd = conn.CreateCommand(); try { cmd.CommandText = sqlCommand; conn.Open(); SqlParameter issueIdParam = new SqlParameter(); issueIdParam.ParameterName = "@issueId"; issueIdParam.Value = issueId; cmd.Parameters.Add(issueIdParam); cmd.ExecuteScalar(); } catch (Exception ex) { ClientScript.RegisterClientScriptBlock(this.GetType(), "Error", "alert('An Error has occoured')", true); } finally { cmd.Dispose(); conn.Close(); } } } else { Response.Redirect("Default.aspx"); } }
/// <summary> /// finds the issues based off the bookId /// </summary> /// <param name="bookId"></param> /// <returns></returns> private int findIssue(int bookId) { int issueId = 0; try { DataRow dr = UserTool.GetUserInfo(Session["User"].ToString()); if (dr == null) { Response.Redirect("default.aspx"); } else { string sqlCommand = "SELECT * FROM Issue WHERE BookId =" + bookId + " AND Status = 'Available'"; conn.ConnectionString = conString; SqlCommand cmd = conn.CreateCommand(); try { cmd.CommandText = sqlCommand; conn.Open(); SqlDataReader reader = cmd.ExecuteReader(); while (reader.Read()) { issueId = reader.GetInt32(0); } } catch (Exception ex) { ClientScript.RegisterClientScriptBlock(this.GetType(), "Error", "alert('An Error has occoured')", true); } finally { cmd.Dispose(); conn.Close(); } } } catch (Exception ex) { Response.Redirect("default.aspx"); } return(issueId); }
/// <summary> /// loads the history of the user /// </summary> private void loadHistory() { try { DataRow dr = UserTool.GetUserInfo(Session["User"].ToString()); if (dr == null) { Response.Redirect("default.aspx"); } else { if (dr["AccountType"].ToString().Equals("Member")) { string sqlCommand = "SELECT Rental.RentalId, Rental.IssueId, Rental.RentalDate, Rental.DueDate, Rental.Fees, Rental.Comments, Issue.Status, Book.Title, Book.CoverType, Author.FirstName + ', ' + Author.LastName AS Author, Genre.Title AS Genre FROM Rental INNER JOIN Issue ON Rental.IssueId = Issue.IssueId INNER JOIN Book ON Issue.BookId = Book.BookId INNER JOIN Author ON Book.AuthorId = Author.AuthorId INNER JOIN Genre ON Book.GenreId = Genre.GenreId " + " WHERE Rental.UserId = @UserId AND Rental.ReturnDate IS NOT NULL"; conn.ConnectionString = conString; SqlCommand cmd = conn.CreateCommand(); int userId = Convert.ToInt32(dr["UserId"].ToString()); try { cmd.CommandText = sqlCommand; conn.Open(); SqlParameter userIdParam = new SqlParameter(); userIdParam.ParameterName = "@UserId"; userIdParam.Value = userId; cmd.Parameters.Add(userIdParam); SqlDataReader reader = cmd.ExecuteReader(); if (!reader.HasRows) { lblHistoryMissing.Visible = true; } else { lblHistoryMissing.Visible = false; } DataTable dt = new DataTable(); dt.Load(reader); GridView2.DataSource = dt; GridView2.DataBind(); reader.Close(); } catch (Exception ex) { ClientScript.RegisterClientScriptBlock(this.GetType(), "Error", "alert('An Error has occoured')", true); } finally { cmd.Dispose(); conn.Close(); } } else { Response.Redirect("default.aspx"); } } } catch (Exception ex) { Response.Redirect("default.aspx"); } }