/// <summary> /// Create Author Logic /// </summary> public static string UpdateAuthor(int id, string refrence, string fullName, string image, string country) { try { using (DbContext = new LibraryAppDbEntities()) { if (string.IsNullOrEmpty(refrence) || string.IsNullOrEmpty(fullName) || string.IsNullOrEmpty(image) || string.IsNullOrEmpty(country)) { return("something is empty"); } else { // Check if The New Author is Already Exist var updatedAuthor = DbContext.Authors.FirstOrDefault(a => a.Id == id); if (updatedAuthor != null) { updatedAuthor.AuthorRef = refrence; updatedAuthor.AuthorFullName = fullName; updatedAuthor.Picture = image; updatedAuthor.Country = country; DbContext.SaveChanges(); return("OK"); } else { return("Reference Must Be Unique"); } } } } catch (Exception) { } return("OK"); }
/// <summary> /// This Method "ConncterUser" allows you to check if user Exist in DataBase /// With "email" that passed as parameter /// </summary> public static bool ConncterUser(string email, string mdp) { try { using (DbContext = new LibraryAppDbEntities()) { // Find The User in DataBase Who has this specific "email" var user = DbContext.UserInfoes .Where(u => u.PassWord == mdp && u.Email == email) .FirstOrDefault <UserInfo>(); // if User Exist if (user != null) { // Create a Session=>UserName and Pass to it value of UserName Column HttpContext.Current.Session["UserName"] = user.UserName; // Redirect User To Crud Author Page HttpContext.Current.Response.Redirect("Home.aspx"); } else { // if you remember we need this bool value for Show and Hide Error Message return(false); } } } catch (Exception) { } return(true); }
protected void btnDelete_Click(object sender, EventArgs e) { // noDeleteMessage.Visible = false; try { using (DbContext = new LibraryAppDbEntities()) { var deletedAuthor = DbContext.Authors.FirstOrDefault(a => a.AuthorRef == txtDeleteAuthor.Value); if (deletedAuthor == null) { findOperaMessage.InnerText = "You Try To Delete Record Doesn't Exist..."; findOperaMessage.Visible = true; } else { // => Remove Author Image From Server File.Delete(Server.MapPath("~/Images/") + deletedAuthor.Picture); //=> Delete Author From Database DbContext.Authors.Remove(deletedAuthor); DbContext.SaveChanges(); LoadAuthorGridView(); } ClearControls("delete"); } } catch (Exception) { throw; } }
/// <summary> /// Load Grid View /// </summary> private void LoadAuthorGridView() { using (DbContext = new LibraryAppDbEntities()) { gvAuthor.DataSource = (from author in DbContext.Authors select new { author.AuthorRef, author.AuthorFullName, author.Picture, author.Country }).ToList(); gvAuthor.DataBind(); } }
/// <summary> /// Create Author Logic /// </summary> public static string AddAuthor(string refrence, string fullName, string image, string country) { try { using (DbContext = new LibraryAppDbEntities()) { if (string.IsNullOrEmpty(refrence) || string.IsNullOrEmpty(fullName) || string.IsNullOrEmpty(image) || string.IsNullOrEmpty(country)) { return("something is empty"); } else { // Check if The New Author is Already Exist var AuthorAlreadyExist = DbContext.Authors.FirstOrDefault(a => a.AuthorRef == refrence); if (AuthorAlreadyExist == null) { Author newAuthor = new Author { AuthorRef = refrence, AuthorFullName = fullName, Picture = image, Country = country }; DbContext.Authors.Add(newAuthor); DbContext.SaveChanges(); return("OK"); } else { return("Already Exist"); } } } } catch (Exception) { } return("OK"); }
/// <summary> /// Button Find Method /// </summary> protected void btnFind_Click(object sender, EventArgs e) { try { using (DbContext = new LibraryAppDbEntities()) { if (string.IsNullOrEmpty(txtRefAuthorFinded.Value)) { Response.Write("Oops"); } else { var findedAuthor = DbContext.Authors.Where(a => a.AuthorRef.Contains(txtRefAuthorFinded.Value)).FirstOrDefault(); if (findedAuthor == null) { findOperaMessage.InnerText = "What You locking For Doesn't Exist Please try Again..."; findOperaMessage.Visible = true; } else { ViewState["AuthorId"] = findedAuthor.Id; txtAuthorRef.Text = findedAuthor.AuthorRef; txtAuthorFullName.Text = findedAuthor.AuthorFullName; txtAuthorCountry.Text = findedAuthor.Country; // With Image We Need To Remove it in Server Side Because User Will Upload New Image File.Delete(Server.MapPath("~/Images/") + findedAuthor.Picture); btnUpdate.Enabled = true; btnAdd.Enabled = false; } } ClearControls("find"); } } catch (Exception) { } }
/// <summary> /// Download XML File With This Way User Can Use File /// </summary> protected void btnDownloadXML_Click(object sender, EventArgs e) { using (DbContext = new LibraryAppDbEntities()) { List <Author> authors = DbContext.Authors.ToList(); if (authors.Count > 0) { var XElement = new XElement("Authors", from author in authors select new XElement("Author", new XAttribute("Ref", author.AuthorRef), new XElement("FullName", author.AuthorFullName), new XElement("Picture", author.Picture), new XElement("Country", author.Country) )); HttpContext context = HttpContext.Current; context.Response.Write(XElement); context.Response.ContentType = "application/xml"; context.Response.AppendHeader("Content-Disposition", "attachment; filename=AuthorData.xml"); context.Response.End(); } } }