Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8642015
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T11:40:16+00:00 2026-06-12T11:40:16+00:00

I’m writing a music player application using WPF (C#). As part of its functionality,

  • 0

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:

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[i] = System.IO.Path.GetFileName(A[i]);

        cmd.Parameters.AddWithValue("@Title", fName[i]);
        cmd.Parameters.AddWithValue("@Path", A[i]);

        try
        {
            cmd.ExecuteNonQuery();
        }
        catch (Exception ex)
        {
            System.Windows.MessageBox.Show("Error: " + ex.Message);
        }
        finally
        {
            listBox1.Items.Add(A[i]);
            listBox2.Items.Add(fName[i]);
            cmd.Parameters.Clear();
        }
    }

    // Close the connection
    cmd.Dispose();
    conn.Close();
    conn.Dispose();
}

The code for the stored procedure is simple –

ALTER PROCEDURE dbo.InsertSongs
(
    @Title nvarchar(50),
    @Path nvarchar(50)
)
AS
    INSERT INTO Songs(Title, Path) VALUES(@Title, @Path)

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       int
Title    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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T11:40:17+00:00Added an answer on June 12, 2026 at 11:40 am

    The whole User Instance and AttachDbFileName= approach is flawed – at best! Visual Studio will be copying around the .mdf file and most likely, your INSERT works just fine – but you’re just looking at the wrong .mdf file in the end!

    If you want to stick with this approach, then try putting a breakpoint on the myConnection.Close() call – and then inspect the .mdf file with SQL Server Mgmt Studio Express – I’m almost certain your data is there.

    The real solution in my opinion would be to

    1. install SQL Server Express (and you’ve already done that anyway)

    2. install SQL Server Management Studio Express

    3. create your database in SSMS Express, give it a logical name (e.g. SongsDatabase)

    4. connect to it using its logical database name (given when you create it on the server) – and don’t mess around with physical database files and user instances. In that case, your connection string would be something like:

      Data Source=.\\SQLEXPRESS;Database=SongsDatabase;Integrated Security=True
      

      and everything else is exactly the same as before…

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am reading a book about Javascript and jQuery and using one of the
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
We're building an app, our first using Rails 3, and we're having to build
We are using XSLT to translate a RIXML file to XML. Our RIXML contains
I'm trying to select an H1 element which is the second-child in its group
I have an MVC Razor view @{ ViewBag.Title = Index; var c = (char)146;

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.