//EnableSortingAndPagingCallbacks to minimize postbacks? private void SortGridView(string sortExpression, string direction) { GridBAL booksLogic = new GridBAL(); //DataView myDataView = new DataView(booksLogic.GetData()); DataView myDataView = new DataView(); if (ViewState["username"].ToString() == "") { if (Convert.ToByte(ViewState["filter"]) == 1) { myDataView = booksLogic.GetData(Convert.ToByte(ViewState["filter"])).AsDataView(); } else { myDataView = booksLogic.GetData().AsDataView(); } } else { myDataView = booksLogic.GetDataByUsername(ViewState["username"].ToString()).AsDataView(); } myDataView.Sort = sortExpression + direction; BooksGrid.DataSource = myDataView; BooksGrid.DataBind(); }
protected void TakenBooks_Click(object sender, EventArgs e) { ViewState["filter"] = 0; if (Session["email"] != null) { ViewState["username"] = (string)(Session["email"]); } else { ViewState["username"] = ""; } //ViewState["username"] = "******"; GridBAL booksLogic = new GridBAL(); if (ViewState["username"].ToString() != "") { BooksGrid.DataSource = booksLogic.GetDataByUsername(ViewState["username"].ToString()); BooksGrid.DataBind(); } else { TestLabel.Text = "You are not logged in"; } }
protected void Page_Load(object sender, EventArgs e) { //BooksBAL booksLogic = new BooksBAL(); GridBAL booksLogic = new GridBAL(); BooksGrid.DataSource = booksLogic.GetData(); BooksGrid.DataBind(); }
protected void AvailableBooks_Click(object sender, EventArgs e) { ViewState["filter"] = 1; ViewState["username"] = ""; GridBAL booksLogic = new GridBAL(); BooksGrid.DataSource = booksLogic.GetData(Convert.ToByte(ViewState["filter"])); BooksGrid.DataBind(); }
protected void SaveDelete_Click(object sender, EventArgs e) { var id = int.Parse(BookDelete.DataKey.Value.ToString()); LibrarySystemDbContext data = new LibrarySystemDbContext(); var book = data.Books.FirstOrDefault(x => x.Id == id); data.Books.Remove(book); data.SaveChanges(); BookDelete.DataSource = null; BookDelete.DataBind(); BooksGrid.DataBind(); }
public void BindDetails() { using (SqlConnection connection = conn) { connection.Open(); SqlCommand cmdSelectBooks = new SqlCommand("Select * from DS_Library.[dbo].Books;", connection); SqlCommand cmdSelectUsers = new SqlCommand("Select Name, Email, Address from DS_Library.[dbo].Users;", connection); SqlCommand cmdSelectBooksAndUsers = new SqlCommand("select a.Name, a.Email,a.Address, c.Title FROM DS_Library.[dbo].[Users] a Inner JOIN DS_Library.[dbo].[Rentals] b ON a.Email = b.UserEmail Inner JOIN DS_Library.[dbo].[Books] c ON b.BookID = c.BookID; ", connection); using (SqlCommand command = cmdSelectBooks) { //Command 1 using (SqlDataReader reader = command.ExecuteReader()) { BooksGrid.DataSource = reader; BooksGrid.DataBind(); } } using (SqlCommand command = cmdSelectUsers) { //Command 2 using (SqlDataReader reader = command.ExecuteReader()) { UsersGrid.DataSource = reader; UsersGrid.DataBind(); } } using (SqlCommand command = cmdSelectBooksAndUsers) { //Command 3 using (SqlDataReader reader = command.ExecuteReader()) { UserRent.DataSource = reader; UserRent.DataBind(); } } } }
protected void SaveCreate_Click(object sender, EventArgs e) { var title = (BookCreate.FindControl("BookTitle") as TextBox).Text; var authors = (BookCreate.FindControl("BookAuthors") as TextBox).Text; var ISBN = (BookCreate.FindControl("BookIsbn") as TextBox).Text; var site = (BookCreate.FindControl("BookSite") as TextBox).Text; var description = (BookCreate.FindControl("BookDescription") as TextBox).Text; var categoryId = int.Parse((BookCreate.FindControl("BookCategory") as DropDownList).SelectedValue); if (string.IsNullOrEmpty(title)) { Error_Handler_Control.ErrorSuccessNotifier.AddErrorMessage("Title cannot be empty"); return; } else if (title.Length < 5 || title.Length > 50) { Error_Handler_Control.ErrorSuccessNotifier.AddErrorMessage("Title must be between 5 and 50 symbols"); return; } else if (string.IsNullOrEmpty(authors)) { Error_Handler_Control.ErrorSuccessNotifier.AddErrorMessage("Authors cannot be empty"); return; } else if (authors.Length < 5 || authors.Length > 50) { Error_Handler_Control.ErrorSuccessNotifier.AddErrorMessage("Authors must be between 5 and 50 symbols"); return; } else if (ISBN.Length > 50) { Error_Handler_Control.ErrorSuccessNotifier.AddErrorMessage("ISBN must be less than 50 symbols"); return; } else if (site.Length > 50) { Error_Handler_Control.ErrorSuccessNotifier.AddErrorMessage("ISBN must be less than 50 symbols"); return; } else if (description.Length > 2000) { Error_Handler_Control.ErrorSuccessNotifier.AddErrorMessage("Description must be less than 2000 symbols"); return; } else if (categoryId == -1) { Error_Handler_Control.ErrorSuccessNotifier.AddErrorMessage("Authors cannot be empty"); return; } LibrarySystemDbContext context = new LibrarySystemDbContext(); context.Books.Add(new Book { Title = title, Authors = authors, ISBN = ISBN, WebSite = site, Description = description, CategoryId = categoryId }); context.SaveChanges(); BookCreate.DataSource = null; BookCreate.DataBind(); BooksGrid.DataBind(); }
protected void SaveEdit_Click(object sender, EventArgs e) { var title = (BookEdit.FindControl("BookTitle") as TextBox).Text; var authors = (BookEdit.FindControl("BookAuthors") as TextBox).Text; var ISBN = (BookEdit.FindControl("BookIsbn") as TextBox).Text; var site = (BookEdit.FindControl("BookSite") as TextBox).Text; var description = (BookEdit.FindControl("BookDescription") as TextBox).Text; var categoryId = int.Parse((BookEdit.FindControl("BookCategory") as DropDownList).SelectedValue); if (string.IsNullOrEmpty(title)) { Error_Handler_Control.ErrorSuccessNotifier.AddErrorMessage("Title cannot be empty"); return; } else if (title.Length < 5 || title.Length > 50) { Error_Handler_Control.ErrorSuccessNotifier.AddErrorMessage("Title must be between 5 and 50 symbols"); return; } else if (string.IsNullOrEmpty(authors)) { Error_Handler_Control.ErrorSuccessNotifier.AddErrorMessage("Authors cannot be empty"); return; } else if (authors.Length < 5 || authors.Length > 50) { Error_Handler_Control.ErrorSuccessNotifier.AddErrorMessage("Authors must be between 5 and 50 symbols"); return; } else if (ISBN.Length > 50) { Error_Handler_Control.ErrorSuccessNotifier.AddErrorMessage("ISBN must be less than 50 symbols"); return; } else if (site.Length > 50) { Error_Handler_Control.ErrorSuccessNotifier.AddErrorMessage("ISBN must be less than 50 symbols"); return; } else if (description.Length > 2000) { Error_Handler_Control.ErrorSuccessNotifier.AddErrorMessage("Description must be less than 2000 symbols"); return; } else if (categoryId == -1 || categoryId == null) { Error_Handler_Control.ErrorSuccessNotifier.AddErrorMessage("Authors cannot be empty"); return; } var id = int.Parse(BookEdit.DataKey.Value.ToString()); LibrarySystemDbContext data = new LibrarySystemDbContext(); var book = data.Books.FirstOrDefault(x => x.Id == id); book.Title = title; book.Authors = authors; book.ISBN = ISBN; book.WebSite = site; book.Description = description; book.CategoryId = categoryId; data.SaveChanges(); BookEdit.DataSource = null; BookEdit.DataBind(); BooksGrid.DataBind(); }