protected void btnSubmit_Click(object sender, EventArgs e) { try { int id = 0; using (deeptiEntities db = new deeptiEntities()) { if (Session["id"] != null) { id = Convert.ToInt32(Session["id"].ToString()); } var query = (from r in db.Registrations where r.Id == id select r.Password).FirstOrDefault(); if (query != null) { if (txtOldPwd.Text == query) { var q = (from u in db.Registrations where u.Id == id select u).SingleOrDefault(); if (q != null) { q.Password = txtNewPwd.Text; db.SaveChanges(); lblMessage.Text = "Password changed successfully! "; } } } } } catch (Exception ex) { lblMessage.Text = ex.Message; } }
protected void gvBooks_RowCommand(object sender, GridViewCommandEventArgs e) { try { if (e.CommandName == "Delete") { using (deeptiEntities db1 = new deeptiEntities()) { //int ncopies = 0; int id = Convert.ToInt32(e.CommandArgument); //var no = (from k in db1.Books where k.Id == id select k.Copies).SingleOrDefault(); //ncopies = Convert.ToInt32(no); var copy = from c in db1.Copies where c.BookId == id select c; // List<Copy> cp = copy.ToList(); foreach (var cp in copy) { db1.Copies.Remove(cp); } var book = (from b in db1.Books where b.Id == id select b).SingleOrDefault(); db1.Books.Remove(book); db1.SaveChanges(); BindGrid(); lblMessage.Text = "Record deleted !"; } } } catch (Exception ex) { lblMessage.Text = ex.Message; } }
protected void txtSubmit_Click(object sender, EventArgs e) { try { using (deeptiEntities db = new deeptiEntities()) { int memberId = 0; int bookId = Convert.ToInt32(txtBookId.Text); if (Session["id"] != null) { memberId = Convert.ToInt32(Session["id"].ToString()); } var query = (from q in db.Transactions where q.BookId == bookId && q.MemberId == memberId select q).FirstOrDefault(); if (query != null) { query.ReturnDate = (Convert.ToDateTime(txtReturnDate.Text)).Date; var iDate = (from i in db.Transactions where i.BookId == bookId && i.MemberId == memberId select i.IssueDate).First(); if (iDate != null) { txtIssuedDate.Text = iDate.Date.ToString("yyyy-MM-dd"); } var DDate = (from d in db.Transactions where d.BookId == bookId && d.MemberId == memberId select d.DueDate).First(); if (DDate != null) { txtDueDate.Text = DDate.Date.ToString("yyyy-MM-dd"); } int days = (Convert.ToDateTime(txtDueDate.Text) - Convert.ToDateTime(txtReturnDate.Text)).Days; if (days < 0) { query.Fine = days / 2; txtFine.Text = (days / 2).ToString(); } else if (days == 0 || days > 0) { query.Fine = 0; txtFine.Text = "0"; } var copy = (from c in db.Transactions where c.BookId == bookId && c.MemberId == memberId select c.CopyId).First(); if (copy != 0) { var cp = (from k in db.Copies where k.Id == copy select k).SingleOrDefault(); if (cp != null) { cp.Status = "available"; } } db.SaveChanges(); lblMessage.Text = "Book returned successfully . Please check your details!"; } } } catch (Exception ex) { lblMessage.Text = ex.Message; } }
protected void gvBooks_RowUpdating(object sender, GridViewUpdateEventArgs e) { try { int copy = 0; GridViewRow row = gvBooks.Rows[e.RowIndex]; int id = Convert.ToInt32(gvBooks.DataKeys[e.RowIndex].Values[0]); string bTitle = (row.FindControl("txtBookTitle") as TextBox).Text; string category = (row.FindControl("txtCategory") as TextBox).Text; string authName = (row.FindControl("txtAuthName") as TextBox).Text; string copies = (row.FindControl("txtCopies") as TextBox).Text; copy = Convert.ToInt32(copies); using (deeptiEntities db = new deeptiEntities()) { Book bk = (from b in db.Books where b.Id == id select b).SingleOrDefault(); bk.BookTitle = bTitle; bk.Category = category; bk.AuthorName = authName; bk.Copies = copy; db.SaveChanges(); } gvBooks.EditIndex = -1; this.BindGrid(); //Label BId = (Label)gvBooks.Rows[e.RowIndex].FindControl("lblId"); // id = Convert.ToInt32(BId); //TextBox BTitle = (TextBox)gvBooks.Rows[e.RowIndex].FindControl("txtBookTitle"); //TextBox Category = (TextBox)gvBooks.Rows[e.RowIndex].FindControl("txtCategory"); //TextBox AuthName = (TextBox)gvBooks.Rows[e.RowIndex].FindControl("txtAuthName"); //TextBox Copies = (TextBox)gvBooks.Rows[e.RowIndex].FindControl("txtCopies"); //copy = Convert.ToInt32(Copies.Text); //using (deeptiEntities db = new deeptiEntities()) //{ // var bookQuery = (from b in db.Books where b.Id == id select b).SingleOrDefault(); // if(bookQuery!=null) // { // bookQuery.BookTitle = BTitle.Text; // bookQuery.Category = Category.Text; // bookQuery.AuthorName = AuthName.Text; // bookQuery.Copies = copy ; // db.SaveChanges(); // gvBooks.EditIndex = -1; // BindGrid(); // lblMessage.Text = "Record updated!"; // } //} } catch (Exception ex) { lblMessage.Text = ex.Message; } }
protected void txtSubmit_Click(object sender, EventArgs e) { // using command temporarily creates a context that accesses our database using (deeptiEntities db = new deeptiEntities()) { Book objBook = new Book { AuthorName = txtAuthorName.Text, BookTitle = txtBookTitle.Text, Category = txtCategory.Text, Copies = Convert.ToInt32(txtCopies.Text) }; db.Books.Add(objBook); db.SaveChanges(); var query = from b in db.Books where b.BookTitle == txtBookTitle.Text & b.AuthorName == txtAuthorName.Text select b.Id; var c = query.SingleOrDefault(); if (c != 0) { for (int i = 0; i < Convert.ToInt32(txtCopies.Text); i++) { Copy objCopy = new Copy { BookId = c, Status = "available" }; db.Copies.Add(objCopy); } } db.SaveChanges(); lblMessage.Text = " Book added successfully !"; } }
protected void gvUsers_RowCommand(object sender, GridViewCommandEventArgs e) { try { if (e.CommandName == "Delete") { using (deeptiEntities db1 = new deeptiEntities()) { int id = Convert.ToInt32(e.CommandArgument); var user = (from u in db1.Registrations where u.Id == id select u).FirstOrDefault(); db1.Registrations.Remove(user); db1.SaveChanges(); BindGrid(); } } } catch (Exception ex) { lblMessage.Text = ex.Message; } }
protected void txtSubmit_Click(object sender, EventArgs e) { try { using (deeptiEntities db = new deeptiEntities()) { int bookId = Convert.ToInt32(txtBookId.Text); var copy = from p in db.Copies where p.BookId == bookId select p; var cid = from c in db.Copies where c.BookId == bookId select c.Id; var s = copy.FirstOrDefault(); if (s != null) { s.Status = "Unavailable"; } var trans = new Transaction(); trans.BookId = bookId; if (Session["id"] != null) { trans.MemberId = Convert.ToInt32(Session["id"].ToString()); } trans.CopyId = Convert.ToInt32(cid.First()); trans.IssueDate = Convert.ToDateTime(DateTime.Now.Date); DateTime due = trans.IssueDate.AddDays(21); trans.DueDate = due; // adding the transactoin to db collection db.Transactions.Add(trans); // telling the db to save the changes db.SaveChanges(); lblMessage.Text = "Book has been issued and due date is " + due.ToString("d"); } } catch (Exception ex) { lblMessage.Text = ex.Message; } }
protected void btnRegister_Click(object sender, EventArgs e) { try { // using command temporarily creates a context that accesses our database // creating an object of the object context "deeptiEntities" using (deeptiEntities db = new deeptiEntities()) { //creating an object of the Registration class Registration objreg = new Registration(); // set values to the db columns objreg.FirstName = txtFirstName.Text; objreg.LastName = txtLastName.Text; objreg.UserName = txtUserName.Text; objreg.Password = txtPassword.Text; objreg.MobileNo = txtMobileNo.Text; objreg.RegisteredDate = DateTime.Now; objreg.Email = txtEmail.Text; objreg.Role = ddlRole.SelectedItem.Text; // add registration object to the registrations collection in the object context db.Registrations.Add(objreg); //call savechanges method to insert the record into table db.SaveChanges(); lblMessage.Text = " You have registered successfully!"; } } catch (Exception ex) { lblMessage.Text = ex.Message; } }