BaBY-DruStR
New Member
I'm writing a music player application using WPF (C#). As part of its functionality, I'm populating a music library, where I'm storing the Title and Path to an mp3 file. The user gets to select a root folder for his music library and then the contents are populated in a "Songs" table. This is the code that I've written:\[code\]private void Populate_Click(object sender, RoutedEventArgs e){ // Folder browser FolderBrowserDialog dlg = new FolderBrowserDialog(); dlg.ShowDialog(); string DirectoryPath = System.IO.Path.GetDirectoryName(dlg.SelectedPath); // Get the data directory string[] A = Directory.GetFiles(DirectoryPath, "*.mp3", SearchOption.AllDirectories); string[] fName = new string[A.Count()]; // Initialize connection string connstr = "Data Source=.\\SQLEXPRESS;AttachDbFilename=|DataDirectory|\\Database.mdf;Integrated Security=True;User Instance=True"; SqlConnection conn = new SqlConnection(connstr); conn.Open(); // Create the SqlCommand SqlCommand cmd = new SqlCommand(); cmd.Connection = conn; cmd.CommandType = CommandType.StoredProcedure; cmd.CommandText = "InsertSongs"; // Create the parameters and execute the command for (int i = 0; i < A.Count(); i++) { fName = System.IO.Path.GetFileName(A); cmd.Parameters.AddWithValue("@Title", fName); cmd.Parameters.AddWithValue("@Path", A); try { cmd.ExecuteNonQuery(); } catch (Exception ex) { System.Windows.MessageBox.Show("Error: " + ex.Message); } finally { listBox1.Items.Add(A); listBox2.Items.Add(fName); cmd.Parameters.Clear(); } } // Close the connection cmd.Dispose(); conn.Close(); conn.Dispose();}\[/code\]The code for the stored procedure is simple -\[code\]ALTER PROCEDURE dbo.InsertSongs( @Title nvarchar(50), @Path nvarchar(50))AS INSERT INTO Songs(Title, Path) VALUES(@Title, @Path)\[/code\]Now, when I execute the program, there is no error message thrown (the file names and directory names have size less than 50). However, at the end of execution, no value is inserted in the Songs table.The Songs table is described as below:ID intTitle nvarchar(50)Path nvarchar(50)I'm not sure where I went wrong: I have also tried using SqlParameter and then defining the type of parameter as NVARCHAR with size 50, but to no avail. May I kindly request you to assist me here? Many thanks in advance.