ExecuteNonQuery() not saving any record

Ahmad picture Ahmad · Sep 24, 2012 · Viewed 9k times · Source

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 :)

Answer

Ahmad picture Ahmad · Sep 26, 2012

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 :)