예제 #1
0
        public Message Save(Book newBook)
        {
            Message message = new Message();
            string isbn = newBook.ISBN.Trim();
            if (isbn.Length != 13)
            {
                message.Status = "warning";
                message.Details = "ISBN Number must be thirteen (13) characters long.";
                return message;
            }
            bool isBookExixts = IsBookExists(newBook);

            if (isBookExixts)
            {
                message.Status = "error";
                message.Details = "ISBN Number is already exists.";
                return message;
            }

            try
            {
                aBookGateway.AddBook(newBook);
                message.Status = "success";
                message.Details = "Book Added Successfully";
            }
            catch (SqlException ex)
            {
                message.Status = "error";
                message.Details = ex.Message;
            }

            return message;
        }
예제 #2
0
        public void AddBook(Book newBook)
        {
            SqlConnection connection = new SqlConnection(connectionString);
            string sql = "INSERT INTO Books (ISBN, Name, Author) VALUES ('" + newBook.ISBN + "', '" + newBook.Name + "', '" + newBook.Author + "')";
            connection.Open();
            SqlCommand command = new SqlCommand(sql, connection);

            command.ExecuteNonQuery();
            connection.Close();
        }
예제 #3
0
        private bool IsBookExists(Book newBook)
        {
            try
            {
                Book aBook = aBookGateway.GetBookByISBN(newBook.ISBN);

                if (aBook != null)
                {
                    return true;
                }
            }
            catch
            {
                return false;
            }
            return false;
        }
예제 #4
0
        protected void saveButton_Click(object sender, EventArgs e)
        {
            string isbn = isbnTextBox.Text;
            string name = nameTextBox.Text;
            string author = authorTextBox.Text;

            Message message = new Message();

            Book aBook = new Book(isbn, name, author);
            message = aBookManager.Save(aBook);

            messageLabel.CssClass = message.Status;
            messageLabel.Text = message.Details;

            if (message.Status == "success")
            {
                ClearTextFields();
            }
        }
예제 #5
0
        public Book GetBookByISBN(string isbn)
        {
            Book aBook = null;
            SqlConnection connection = new SqlConnection(connectionString);
            string sql = "SELECT * FROM Books WHERE ISBN = '" + isbn + "'";
            connection.Open();
            SqlCommand command = new SqlCommand(sql, connection);

            SqlDataReader reader = command.ExecuteReader();

            if (reader.Read())
            {
                aBook = new Book();
                aBook.Id = Convert.ToInt32(reader["Id"].ToString());
                aBook.ISBN = reader["ISBN"].ToString();
                aBook.Name = reader["Name"].ToString();
                aBook.Author = reader["Author"].ToString();
            }
            connection.Close();

            return aBook;
        }