Example #1
0
        private void AddCDBtn_Click(object sender, EventArgs e)
        {
            //TODO Add in a confirmation button

            bool   canAdd   = true;
            string sqlQuery = @"INSERT INTO CDdtb (`Album`,`Artist`,`Genre`,`DateStr`) values (?,?,?,?)";
            string addSongs = @"INSERT INTO SongListDTB ( `Album`, `Artist`,`Song_Name`, `AlbumID`) values (?,?,?,?)";
            string newAlbum = addAlbumBx.Text.Trim();

            StandardizeText(ref newAlbum);
            string newArtist = addArtistBx.Text.Trim();

            StandardizeText(ref newArtist);
            string newGenre = addGenreBx.Text.Trim();

            StandardizeText(ref newGenre);
            using (OleDbConnection conn = new OleDbConnection(pathStr))
            {
                using (OleDbCommand cmd = new OleDbCommand(sqlQuery, conn))
                {
                    foreach (Cd x in CdList)
                    {
                        if (newAlbum == x.getAlbum() &&
                            newArtist == x.getArtist())
                        {
                            MessageBox.Show("CD already exists.");
                            canAdd = false;
                            break;
                        }
                    }
                    if (canAdd)
                    {
                        try
                        {
                            conn.Open();

                            cmd.Parameters.AddWithValue("@Album", newAlbum);
                            cmd.Parameters.AddWithValue("@Artist", newArtist);
                            cmd.Parameters.AddWithValue("@Genre", newGenre);
                            cmd.Parameters.AddWithValue("@DateStr", this.dateTimePicker1.Value.Date);
                            cmd.ExecuteNonQuery();
                            cmd.Dispose();

                            conn.Close();
                            Cd x = new Cd();
                            x.setAlbum(newAlbum);
                            x.setArtist(newArtist);
                            x.setDate(this.dateTimePicker1.Value.Date);
                            x.setGenre(newGenre);

                            CdList.AddLast(x);
                            if (newGenre != "")
                            {
                                if (!GenreComboBx.Items.Contains(newGenre))
                                {
                                    GenreComboBx.Items.Add(newGenre);
                                }
                            }

                            if (newArtist != "")
                            {
                                if (!ArtistComboBx.Items.Contains(newArtist))
                                {
                                    ArtistComboBx.Items.Add(newArtist);
                                }
                            }

                            using (OleDbConnection songConn = new OleDbConnection(songPathStr))
                            {
                                using (OleDbCommand songCmd = new OleDbCommand(addSongs, songConn))
                                {
                                    try
                                    {
                                        songConn.Open();
                                        songCmd.Parameters.AddWithValue("@Album", x.getAlbum());
                                        songCmd.Parameters.AddWithValue("@Artist", x.getArtist());
                                        List <string> tmpSngLst = new List <string>();
                                        string        z         = "";
                                        songCmd.Parameters.AddWithValue("@SongName", z);
                                        foreach (string y in AddSongBx.Lines)
                                        {
                                            z = y.Trim();
                                            StandardizeText(ref z);
                                            songCmd.Parameters["@SongName"].Value = z;
                                            songCmd.Parameters.AddWithValue("@AlbumID", nextID);
                                            songCmd.ExecuteNonQuery();
                                            tmpSngLst.Add(y);
                                        }
                                        nextID++;

                                        x.setSongName(tmpSngLst);

                                        //foreach(string x in AddSongBx.Text)
                                    }
                                    catch (Exception ex)
                                    {
                                        Debug.WriteLine(ex.StackTrace);
                                    }
                                    finally
                                    {
                                        songConn.Close();
                                    }
                                }
                            }
                        }

                        catch (Exception ex)
                        {
                            Debug.WriteLine("Error opening file" + ex.StackTrace);
                        }
                        finally
                        {
                            conn.Close();
                        }
                    }
                }
            }
        }