Beispiel #1
0
        private void btn_Add_Click(object sender, EventArgs e)
        {
            errorProvider.Clear();
            if (String.IsNullOrEmpty(txt_AuthorName.Text))
            {
                errorProvider.SetError(txt_AuthorName, "Enter author name");
            }
            else
            {
                using (var context = new ChinookEntities())
                {
                    var newArtist = new Artist()
                    {
                        Name = txt_AuthorName.Text
                    };

                    if (!context.Artists.Any(a => a.Name == newArtist.Name))
                    {
                        context.Artists.Add(newArtist);
                    }
                    context.SaveChanges();
                }


                this.Hide();
                var form = new AddItemForm();
                form.ShowDialog();
                this.Close();
            }
        }
Beispiel #2
0
        private void btn_AddItem_Click(object sender, EventArgs e)
        {
            this.Hide();
            var addItemForm = new AddItemForm();

            addItemForm.ShowDialog();
        }
Beispiel #3
0
        private void btn_Cancel_Click(object sender, EventArgs e)
        {
            this.Hide();
            var form = new AddItemForm();

            form.ShowDialog();
            this.Close();
        }
Beispiel #4
0
        private void btn_Add_Click(object sender, EventArgs e)
        {
            errorProvider.Clear();
            if (comboBox_Author.SelectedIndex < 0)
            {
                errorProvider.SetError(comboBox_Author, "Choose author");
            }
            else if (String.IsNullOrEmpty(txt_Title.Text))
            {
                errorProvider.SetError(txt_Title, "Enter album title");
            }
            else
            {
                using (var context = new ChinookEntities())
                {
                    string selectedArtist = comboBox_Author.SelectedItem.ToString();

                    var artist = context.Artists
                                 .Where(a => a.Name == selectedArtist)
                                 .FirstOrDefault();

                    var newAlbum = new Album()
                    {
                        Artist   = artist,
                        ArtistId = artist.ArtistId,
                        Title    = txt_Title.Text
                    };

                    context.Albums.Add(newAlbum);
                    context.SaveChanges();
                }
                this.Hide();
                var form = new AddItemForm();
                form.ShowDialog();
                this.Close();
            }
        }
        private async void btn_Add_Click(object sender, EventArgs e)
        {
            errorProvider.Clear();
            if (ValidateFields())
            {
                using (var context = new ChinookEntities())
                {
                    Artist = await context.Artists
                             .Where(a => a.Name == comboBox_Author.SelectedItem.ToString())
                             .FirstOrDefaultAsync();

                    Album = await context.Albums
                            .Where(a => a.Title == comboBox_Album.SelectedItem.ToString())
                            .FirstOrDefaultAsync();

                    Genre = await context.Genres
                            .Where(g => g.Name == comboBox_Genre.SelectedItem.ToString())
                            .FirstOrDefaultAsync();

                    MediaType = await context.MediaTypes
                                .Where(m => m.Name == comboBox_MediaType.SelectedItem.ToString())
                                .FirstOrDefaultAsync();

                    if (double.TryParse(txt_Size.Text, out double size))
                    {
                    }
                    if (double.TryParse(txt_Minutes.Text, out double minutes))
                    {
                    }
                    if (double.TryParse(txt_Seconds.Text, out double seconds))
                    {
                    }
                    if (decimal.TryParse(txt_Price.Text, out decimal price))
                    {
                    }

                    var newTrack = new Track()
                    {
                        Name         = txt_Title.Text,
                        Album        = this.Album,
                        AlbumId      = this.Album.AlbumId,
                        Bytes        = (int)ConvertMegabytesToBytes(size),
                        Composer     = txt_Composer.Text,
                        Genre        = this.Genre,
                        GenreId      = this.Genre.GenreId,
                        MediaType    = this.MediaType,
                        MediaTypeId  = this.MediaType.MediaTypeId,
                        Milliseconds = (int)(ConvertMinutesToMilliseconds(minutes) + ConvertSecondsToMilliseconds(seconds)),
                        UnitPrice    = price
                    };

                    double ConvertMinutesToMilliseconds(double time)
                    {
                        return(TimeSpan.FromMinutes(time).TotalMilliseconds);
                    }

                    double ConvertSecondsToMilliseconds(double time)
                    {
                        return(TimeSpan.FromSeconds(time).TotalMilliseconds);
                    }

                    double ConvertMegabytesToBytes(double mb)
                    {
                        return(mb * 1048576);
                    }

                    context.Tracks.Add(newTrack);
                    context.SaveChanges();

                    MessageBox.Show("Track added", "Success", MessageBoxButtons.OK, MessageBoxIcon.Information);

                    this.Hide();
                    var form = new AddItemForm();
                    form.ShowDialog();
                    this.Close();
                }
            }
        }