Exemple #1
0
        private void selialize_btn_Click(object sender, RoutedEventArgs e)
        {
            List <Book> listBooks = ReadFromDatabase.ReadAllBooks();

            // BinaryFormatter:
            FileStream      fs, fs1, fs2;
            BinaryFormatter formatter = new BinaryFormatter();

            using (fs = new FileStream("ListBooks.txt", FileMode.OpenOrCreate))
            {
                formatter.Serialize(fs, listBooks);
                fs.Close();
                Title = "Collection of a PC is Serialize";
            }

            // XmlSerializer:
            XmlSerializer xml_formatter = new XmlSerializer(typeof(Book));

            using (fs1 = new FileStream("list_books.xml", FileMode.OpenOrCreate))
            {
                formatter.Serialize(fs1, listBooks);
                fs1.Close();
            }

            // JsonSerializer:
            using (fs2 = new FileStream("books_serialize.json", FileMode.OpenOrCreate))
            {
                // JsonSerializer.Serialize<List<Book>>(fs2, listBooks);
                Title = "Data has been saved to file";
                fs2.Close();
            }
        }
Exemple #2
0
 private void show_books_Click(object sender, RoutedEventArgs e)
 {
     listBox1.Items.Clear();
     Book[] books = ReadFromDatabase.ReadAllBooks().ToArray();
     foreach (Book b in books)
     {
         listBox1.Items.Add(b);
     }
 }
Exemple #3
0
        private void add_btn_Click(object sender, EventArgs e)
        {
            if (mode.Equals("Add")) // mode == "Add"
            {
                // Если мы оставили все поля пустыми:
                if (title_txt.Text == "" && age_txt.Text == "" && author_txt.Text == "" && genre_txt.Text == "")
                {
                    MessageBox.Show("Вы оставили все поля пустыми", "Все поля пустые...", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                }
                // Если мы оставили все поля пустыми:
                else if (title_txt.Text == "" || age_txt.Text == "" || author_txt.Text == "" || genre_txt.Text == "")
                {
                    MessageBox.Show("Какое то пеле оставили пустым", "Пустое к-ето поле...", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                }
                else
                {
                    try
                    {
                        Book book = new Book
                        {
                            Id           = autoIncrement,
                            Title        = title_txt.Text,
                            Age_Release  = int.Parse(age_txt.Text),
                            Id_Author    = int.Parse(author_txt.Text),
                            Id_Genre     = int.Parse(genre_txt.Text),
                            Date_Updated = DateTime.Now
                        };

                        // Add to Books table of databese:
                        string msg = InsertToDatabase.InsertBook(book);
                        MessageBox.Show(msg, "Added");

                        (this.Owner as Main_Form).listBox1.Items.Add(book);
                        (this.Owner as Main_Form).listBox1.Items.Clear();
                        (this.Owner as Main_Form).listBox1.Items.AddRange(ReadFromDatabase.ReadAllBooks().ToArray());
                        ClearFields();
                        this.Close();
                    }
                    catch { MessageBox.Show("Вы ввели символи, или строку вместо целого числа", "Не правильный формат", MessageBoxButtons.OK, MessageBoxIcon.Warning); }
                }
            }
            else if (mode.Equals("Edit"))
            {
                // Если мы оставили все поля пустыми:
                if (title_txt.Text == "" && age_txt.Text == "" && author_txt.Text == "" && genre_txt.Text == "")
                {
                    MessageBox.Show("Вы оставили все поля пустыми", "Все поля пустые...", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                }
                // Если мы оставили все поля пустыми:
                else if (title_txt.Text == "" || age_txt.Text == "" || author_txt.Text == "" || genre_txt.Text == "")
                {
                    MessageBox.Show("Какое то пеле оставили пустым", "Пустое к-ето поле...", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                }
                else
                {
                    int  index   = (this.Owner as Main_Form).listBox1.Items.IndexOf(this.book);
                    Book updated = new Book();
                    updated.Id           = index + 1;
                    updated.Title        = title_txt.Text;
                    updated.Age_Release  = int.Parse(age_txt.Text);
                    updated.Id_Author    = int.Parse(author_txt.Text);
                    updated.Id_Genre     = int.Parse(genre_txt.Text);
                    updated.Date_Updated = DateTime.Now;

                    string msg = UpdateFromDatrabase.EditBook(updated);
                    MessageBox.Show(msg, "Updated");

                    (this.Owner as Main_Form).listBox1.Items.RemoveAt(index);
                    (this.Owner as Main_Form).listBox1.Items.Insert(index, updated);
                    (this.Owner as Main_Form).listBox1.Items.Clear();
                    (this.Owner as Main_Form).listBox1.Items.AddRange(ReadFromDatabase.ReadAllBooks().ToArray());
                    this.Close();
                }
            }
            else if (mode.Equals("ServiceWCF"))
            {
                // Если мы оставили все поля пустыми:
                if (title_txt.Text == "" && age_txt.Text == "" && author_txt.Text == "" && genre_txt.Text == "")
                {
                    MessageBox.Show("Вы оставили все поля пустыми", "Все поля пустые...", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                }
                // Если мы оставили все поля пустыми:
                else if (title_txt.Text == "" || age_txt.Text == "" || author_txt.Text == "" || genre_txt.Text == "")
                {
                    MessageBox.Show("Какое то пеле оставили пустым", "Пустое к-ето поле...", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                }
                else
                {
                    try
                    {
                        BookService service = new BookService();
                        service.Title        = title_txt.Text;
                        service.Age_Release  = int.Parse(age_txt.Text);
                        service.Id_Author    = int.Parse(author_txt.Text);
                        service.Id_Genre     = int.Parse(genre_txt.Text);
                        service.Date_Updated = DateTime.Now;

                        client.Insert_Book(service);

                        ClearFields();
                        Close();
                    }
                    catch (DataException dex)
                    {
                        MessageBox.Show(dex.Message, "Something went wrong...", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    }
                }
            }
        }
Exemple #4
0
        private void exec_btn_Click(object sender, RoutedEventArgs e)
        {
            if (mode.Equals("Add"))
            {
                // Если мы оставили все поля пустыми:
                if (title_txt.Text == "" && age_txt.Text == "" && author_txt.Text == "" && genre_txt.Text == "")
                {
                    MessageBox.Show("Вы оставили все поля пустыми", "Все поля пустые...", MessageBoxButton.OK, MessageBoxImage.Warning);
                }
                // Если мы оставили все поля пустыми:
                else if (title_txt.Text == "" || age_txt.Text == "" || author_txt.Text == "" || genre_txt.Text == "")
                {
                    MessageBox.Show("Какое то пеле оставили пустым", "Пустое к-ето поле...", MessageBoxButton.OK, MessageBoxImage.Warning);
                }
                else
                {
                    try
                    {
                        Book book = new Book
                        {
                            Id           = autoIncrement,
                            Title        = title_txt.Text,
                            Age_Release  = int.Parse(age_txt.Text),
                            Id_Author    = int.Parse(author_txt.Text),
                            Id_Genre     = int.Parse(genre_txt.Text),
                            Date_Updated = System.DateTime.Now
                        };

                        // Add to Books table of databese:
                        string msg = InsertToDatabase.InsertBook(book);
                        MessageBox.Show(msg, "Added");

                        (this.Owner as MainWindow).listBox1.Items.Add(book);
                        (this.Owner as MainWindow).listBox1.Items.Clear();

                        books = ReadFromDatabase.ReadAllBooks().ToArray();

                        // Ubdate listbox to curent datas from a table Books:
                        foreach (Book b in books)
                        {
                            (this.Owner as MainWindow).listBox1.Items.Add(b);
                        }
                        ClearFields();
                        this.Close();
                    }
                    catch { MessageBox.Show("Вы ввели символи, или строку вместо целого числа", "Не правильный формат", MessageBoxButton.OK, MessageBoxImage.Warning); }
                }
            }
            else if (mode.Equals("Update"))
            {
                // Если мы оставили все поля пустыми:
                if (title_txt.Text == "" && age_txt.Text == "" && author_txt.Text == "" && genre_txt.Text == "")
                {
                    MessageBox.Show("Вы оставили все поля пустыми", "Все поля пустые...", MessageBoxButton.OK, MessageBoxImage.Warning);
                }
                // Если мы оставили все поля пустыми:
                else if (title_txt.Text == "" || age_txt.Text == "" || author_txt.Text == "" || genre_txt.Text == "")
                {
                    MessageBox.Show("Какое то пеле оставили пустым", "Пустое к-ето поле...", MessageBoxButton.OK, MessageBoxImage.Warning);
                }
                else
                {
                    try
                    {
                        My_Context db    = new My_Context();
                        int        index = (this.Owner as MainWindow).listBox1.Items.IndexOf(this.book);

                        // Get Id entity from the Table:
                        int id = book.Id;

                        // Get entity from DbSet to Id and edit it:
                        List <Book> all     = ReadFromDatabase.ReadAllBooks();
                        Book        updated = all.Where(u => u.Id == id).FirstOrDefault();
                        //updated.Id = index + 1;
                        updated.Title        = title_txt.Text;
                        updated.Age_Release  = int.Parse(age_txt.Text);
                        updated.Id_Author    = int.Parse(author_txt.Text);
                        updated.Id_Genre     = int.Parse(genre_txt.Text);
                        updated.Date_Updated = System.DateTime.Now;

                        //string msg = UpdateFromDatrabase.EditBook(updated);

                        // Update to Database:
                        db.Entry(updated).State = System.Data.Entity.EntityState.Modified;
                        db.SaveChanges();
                        //MessageBox.Show(msg, "Updated");

                        (this.Owner as MainWindow).listBox1.Items.RemoveAt(index);
                        (this.Owner as MainWindow).listBox1.Items.Insert(index, updated);
                        (this.Owner as MainWindow).listBox1.Items.Clear();

                        books = ReadFromDatabase.ReadAllBooks().ToArray();
                        foreach (Book b in books)
                        {
                            (this.Owner as MainWindow).listBox1.Items.Add(b);
                        }
                        this.Close();
                    }
                    catch (System.Exception ex)
                    {
                        MessageBox.Show(ex.Message, "System Exception..Failed..", MessageBoxButton.OK, MessageBoxImage.Error);
                    }
                }
            }
            else
            {
                MessageBox.Show("Incorrect sting parameter 'mode'..", "Something went wrong...", MessageBoxButton.OK, MessageBoxImage.Warning);
            }
        }
Exemple #5
0
 private void ShowBook_btn_Click(object sender, EventArgs e)
 {
     listBox1.Items.Clear();
     Book[] books = ReadFromDatabase.ReadAllBooks().ToArray();
     listBox1.Items.AddRange(books);
 }