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

  • Home
  • SEARCH
  • 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 8589243
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T23:00:32+00:00 2026-06-11T23:00:32+00:00

I’m working a WinForms based C# tool which has an attached MDF file based

  • 0

I’m working a WinForms based C# tool which has an attached MDF file based database. I’m trying to use the SqlCommand.ExecuteNonQuery() method to save a record to this attached MDF database, but the record is not saved. No error or exception occurs; only problem is that the record is not actually saved.

I have a Console.WriteLine at the top which shows the query I’m trying to run. Its correct syntax-wise, and if I copy-paste it from the output windows and run it separately, it works.

I have correctly defined the connection string as the following, and it works fine for fetching records:

public static String connectionString = @"Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\TestBuildDB.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True";

Here the function I’m using to save records:

public static void PerformDBWriteTransaction(string inputSQLStatement)
{
    Console.WriteLine(inputSQLStatement);
    DataTable returnDataTable = new DataTable();
    SqlConnection sqlConnection = new SqlConnection();
    sqlConnection.ConnectionString = connectionString;
    SqlCommand cmd = new SqlCommand();
    cmd.Connection = sqlConnection;
    cmd.CommandType = CommandType.Text;
    cmd.CommandText = inputSQLStatement;
    cmd.Connection.Open();
    try
    {
        cmd.ExecuteNonQuery();
    }
    catch (SqlException ex)
    {
        errorMessages.Clear();
        errorMessages.Append("The following errors were found in the SQL statement:\n\n");

        for (int i = 0; i < ex.Errors.Count; i++)
        {
            errorMessages.Append("Index #" + i + "\n" +
                "Message: " + ex.Errors[i].Message + "\n" +
                "LineNumber: " + ex.Errors[i].LineNumber + "\n" +
                "Source: " + ex.Errors[i].Source + "\n" +
                "Procedure: " + ex.Errors[i].Procedure + "\n");
        }
        MessageBox.Show(errorMessages.ToString());
    }
    finally
    {
        cmd.Connection.Close();
    }
}

Can someone tell me what might be the problem ? Do I need to perform a ‘commit’ somehow ?

EDIT:

I have found the problem, and have written up a solution below .. Thanks to all who helped me out though ๐Ÿ™‚

  • 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-11T23:00:33+00:00Added an answer on June 11, 2026 at 11:00 pm

    I found the problem ! It was very simple, and it was stupid really ๐Ÿ™‚ .. The code above is all correct .. Yes, people pointed out optimizations, etc, but still the code above was correct.

    The problem was that when I imported the TestDB.MDF file into my Visual 2010 project, a copy of that was made inside the project’s folder. When you run/debug the program, another copy of the this file is made and is put in the \bin\Debug\ folder. In the connection string I was using, I had mentioned: AttachDbFilename=|DataDirectory|\TestBuildDB.mdf .. This meant that all reads/writes were done to the copy in the bin\Debug folder. However, the TestDB.MDF file I was looking into to verify if records were inserted or not, was in the project’s folder ! So basically, there were two MDF files, and I was writing the records into one file, but was trying to find them in the other ๐Ÿ™‚

    When you add an MDF file into your VS2010 Project, VS2010 by default makes a connection to that MDF file, from where you can browse the stuff in that MDF file .. The MDF file used for this purpose was the one placed in the project’s folder, NOT the one in bin\Debug\ folder. And like I said earlier, my code used the one in the bin\Debug folder ๐Ÿ™‚

    So what I’ve done now is that I’ve removed the Test.MDF file reference from my project, which removes the copy present in the project’s folder. However, I DO have a copy of TestDB.MDF file in the bin\Debug\ folder, which I connect to from within my application. And if I want to browse the MDf file outside my project, I use SQL Management Studio. The only problem here is that an MDF file can only be used by one program at a given time. So if I have to use it with my application, I have to take it offline from SQL Management studio, and vica versa !

    I hope this explanation helps someone out there ๐Ÿ™‚

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

Sidebar

Related Questions

I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
I want use html5's new tag to play a wav file (currently only supported
In my XML file chapters tag has more chapter tag.i need to display chapters
I am trying to render a haml file in a javascript response like so:
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have an array which has BIG numbers and small numbers in it. I
I'm trying to select an H1 element which is the second-child in its group
I'm trying to use string.replace('โ€™','') to replace the dreaded weird single-quote character: โ€™ (aka
I have a reasonable size flat file database of text documents mostly saved in

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.