protected void GrdBook_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            int row     = int.Parse(e.CommandArgument.ToString());
            var issueId = int.Parse(GrdBook.Rows[row].Cells[0].Text.Trim());

            if (e.CommandName == "Submit")
            {
                using (DemoLibraryAppEntities context = new DemoLibraryAppEntities())
                {
                    LibraryDB.User currentUser = Session["CurrentUser"] as LibraryDB.User;
                    var            issuedEntry = context.Issues.Where(x => x.IssueID == issueId).SingleOrDefault();
                    if (issuedEntry.ReturnDate == null)
                    {
                        issuedEntry.ReturnDate = DateTime.Now;
                        context.SaveChanges();
                        BindGrid();
                        Errorlabel.Text = " Submitted Successfully!";
                    }
                    else
                    {
                        Errorlabel.Text = "Already Submitted..!";
                    }
                }
            }
        }
        protected void Register_Click(object sender, EventArgs e)
        {
            try
            {
                string username = Username.Text;

                using (DemoLibraryAppEntities context = new DemoLibraryAppEntities())
                {
                    var user = context.Users.FirstOrDefault(u => u.Username == username);
                    if (user != null)
                    {
                        ErrorMessage.Text = "Username already exists.";
                    }
                    else
                    {
                        User useradd = new User();
                        useradd.Name     = Name.Text;
                        useradd.Username = Username.Text;
                        useradd.Email    = Email.Text;
                        useradd.Address  = Address.Text;
                        useradd.Password = Password.Text;
                        useradd.UserRole = UserRoleDrop.Text;
                        context.Users.Add(useradd);
                        context.SaveChanges();
                        Response.Redirect("Login.aspx");
                    }
                }
            }
            catch (Exception ex)
            {
                Response.Write(ex.Message);
            }
        }
Пример #3
0
        protected void Insert(object sender, EventArgs e)
        {
            using (DemoLibraryAppEntities entities = new DemoLibraryAppEntities())
            {
                LibraryDB.User currentUser = Session["CurrentUser"] as LibraryDB.User;
                Book           NewBook     = new Book
                {
                    Title     = txtNewTitle.Text,
                    Author    = txtAuthor.Text,
                    Category  = txtCategory.Text,
                    Year      = int.Parse(txtYear.Text),
                    Quantity  = int.Parse(txtQuantity.Text),
                    Available = int.Parse(txtQuantity.Text),

                    IsDeleted = 0,
                    CreatedBy = currentUser.Name
                };

                entities.Books.Add(NewBook);
                entities.SaveChanges();
            }

            this.BindGrid();
            txtAuthor.Text   = "";
            txtCategory.Text = "";
            txtYear.Text     = "";
            txtQuantity.Text = "";
        }
Пример #4
0
        protected void OnRowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            GridViewRow row       = GrdBook.Rows[e.RowIndex];
            int         bookid    = Convert.ToInt32(GrdBook.DataKeys[e.RowIndex].Values[0]);
            string      title     = (row.FindControl("txtTitle") as TextBox).Text;
            string      author    = (row.FindControl("txtAuthor") as TextBox).Text;
            string      category  = (row.FindControl("txtCategory") as TextBox).Text;
            string      year      = (row.FindControl("txtYear") as TextBox).Text;
            string      quantity  = (row.FindControl("txtQuantity") as TextBox).Text;
            string      available = (row.FindControl("txtQuantity") as TextBox).Text;

            using (DemoLibraryAppEntities entities = new DemoLibraryAppEntities())
            {
                Book NewBook = (from c in entities.Books
                                where c.BookID == bookid
                                select c).FirstOrDefault();
                NewBook.Title     = title;
                NewBook.Author    = author;
                NewBook.Category  = category;
                NewBook.Year      = int.Parse(year);
                NewBook.Quantity  = int.Parse(quantity);
                NewBook.Available = int.Parse(available);
                entities.SaveChanges();
            }

            GrdBook.EditIndex = -1;
            this.BindGrid();
        }
Пример #5
0
 private void BindGrid()
 {
     using (DemoLibraryAppEntities entities = new DemoLibraryAppEntities())
     {
         GrdBook.DataSource = GetData();
         GrdBook.DataBind();
     }
 }
Пример #6
0
 private List <Book> GetData()
 {
     using (DemoLibraryAppEntities context = new DemoLibraryAppEntities())
     {
         List <Book> books = context.Books.Where(x => x.IsDeleted == 0).ToList();
         return(books ?? new List <Book>());
     }
 }
Пример #7
0
        protected void SearchTitle_Click(object sender, EventArgs e)
        {
            string title = txtTitle.Text;

            using (DemoLibraryAppEntities context = new DemoLibraryAppEntities())
            {
                GrdBook.DataSource = (from b in context.Books where b.Title.Contains(title) && b.IsDeleted == 0 select b).ToList();
                GrdBook.DataBind();
            }
        }
Пример #8
0
 private void BindGrid()
 {
     using (DemoLibraryAppEntities entities = new DemoLibraryAppEntities())
     {
         GrdBook.DataSource = (from book in entities.Books
                               where book.IsDeleted == 0
                               select book).ToList();
         GrdBook.DataBind();
     }
 }
Пример #9
0
 private bool IsAlreadyIssued(int userId, int bookId)
 {
     using (DemoLibraryAppEntities context = new DemoLibraryAppEntities())
     {
         var user = context.Users.Where(x => x.UserID == userId).SingleOrDefault();
         if (user != null)
         {
             return(user.Issues.Any(x => x.BookID == bookId));
         }
     }
     return(false);
 }
 private void BindGrid()
 {
     using (DemoLibraryAppEntities entities = new DemoLibraryAppEntities())
     {
         LibraryDB.User currentUser = Session["CurrentUser"] as LibraryDB.User;
         GrdBook.DataSource = (from t1 in entities.Issues
                               join t2 in entities.Books
                               on t1.BookID equals t2.BookID
                               select new { t1.IssueID, t1.UserID, currentUser.Name, t1.BookID, t2.Title, t2.Author, t1.IssueDate, t1.ReturnDate }).ToList();
         GrdBook.DataBind();
     }
 }
Пример #11
0
 private void BindGrid()
 {
     using (DemoLibraryAppEntities entities = new DemoLibraryAppEntities())
     {
         LibraryDB.User currentUser = Session["CurrentUser"] as LibraryDB.User;
         GrdBook.DataSource = (from t1 in entities.Issues
                               join t2 in entities.Books
                               on t1.BookID equals t2.BookID
                               where t1.UserID == currentUser.UserID
                               orderby t1.IssueDate descending
                               select t2).Take(5).ToList();
         GrdBook.DataBind();
     }
 }
Пример #12
0
        protected void OnRowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            int bookid = Convert.ToInt32(GrdBook.DataKeys[e.RowIndex].Values[0]);

            using (DemoLibraryAppEntities entities = new DemoLibraryAppEntities())
            {
                Book NewBook = (from c in entities.Books
                                where c.BookID == bookid
                                select c).FirstOrDefault();
                NewBook.IsDeleted = 1;

                entities.SaveChanges();
            }
            this.BindGrid();
        }
Пример #13
0
        protected void Login_Click(object sender, EventArgs e)
        {
            try
            {
                string uname = Username.Text;
                string pass  = Password.Text;

                using (DemoLibraryAppEntities context = new DemoLibraryAppEntities())
                {
                    var user = context.Users.FirstOrDefault(u => u.Username == uname);
                    if (user != null)
                    {
                        if (user.Password == pass)
                        {
                            Session["CurrentUser"] = user;
                            LibraryDB.User currentUser = Session["CurrentUser"] as LibraryDB.User;
                            if (currentUser.UserRole == "User")
                            {
                                Response.Redirect("HomeUser.aspx");
                            }
                            else
                            {
                                Response.Redirect("BookDB.aspx");
                            }
                        }
                        else
                        {
                            Response.Redirect("Register.aspx");
                        }
                    }
                    else
                    {
                        Response.Redirect("Register.aspx");
                    }
                }
            }
            catch (Exception ex)
            {
                Response.Write(ex.Message);
            }
        }
Пример #14
0
        private bool IssueBook(int bookId)
        {
            int resultCount = 0;

            using (DemoLibraryAppEntities context = new DemoLibraryAppEntities())
            {
                LibraryDB.User currentUser = Session["CurrentUser"] as LibraryDB.User;
                Book           bookToIssue = context.Books.Where(x => x.BookID == bookId).SingleOrDefault();

                if (!IsAlreadyIssued(currentUser.UserID, bookId))
                {
                    bookToIssue.Issues.Add(new Issue
                    {
                        BookID    = bookToIssue.BookID,
                        UserID    = currentUser.UserID,
                        IssueDate = DateTime.Now
                    });
                    --bookToIssue.Available;
                    resultCount = context.SaveChanges();
                }
            }
            return(resultCount > 0);
        }
Пример #15
0
 protected void GrdBook_RowCommand(object sender, GridViewCommandEventArgs e)
 {
     using (DemoLibraryAppEntities context = new DemoLibraryAppEntities())
     {
         int  row         = int.Parse(e.CommandArgument.ToString());
         var  bookid      = int.Parse(GrdBook.Rows[row].Cells[0].Text.Trim());
         Book bookToIssue = context.Books.Where(x => x.BookID == bookid).SingleOrDefault();
         if (bookToIssue.Issues.Where(x => x.ReturnDate != null).Count() < bookToIssue.Quantity)
         {
         }
         if (e.CommandName == "Issue")
         {
             var issuedResult = IssueBook(bookid);
             if (!issuedResult)
             {
                 Errorlabel.Text = "Already issued";
             }
             else
             {
                 Errorlabel.Text = "Issued Successfully.";
             }
         }
     }
 }