Darren Hoehna
Darren Hoehna

Reputation: 379

Single insert statment is inserting more than one value

Hello Stack Overflow Community,

I have a wild problem on my hands. Here is my problem:
1). Before ExecuteNonQuery() is executed there are no records in my database
2). After ExecuteNonQuery() is executed there are two records in my database, although I am using one one insert statement.

My program is multithreaded. I have restricted my program so only one thread is running but I still get the error.

I would look in other areas but I don't know where to search or any reason why this would happen.

Here is the basic outline of my program.
Start one thread per day
Look for a file
Extract information from file
Upload information

Here is the code that I use to upload the data (Excluding error handling)

      string CommandString = INSERT INTO Production_Test.dbo.Transactions
      (BoxId, ProcessDate, Batch, BillingElementID, 
      TransactionDescription, Quantity)

      VALUES (@BoxId, @ProcessDate, @Batch, 
      @BillingElementID, @TransactionDescription, @Quantity)";

      SqlCommand Command = new SqlCommand(CommandString, Database);

            Command.Parameters.AddWithValue("ProcessDate", CheckPrimaryKeyForNull(this.ProcessDate));
            Command.Parameters.AddWithValue("Batch", CheckPrimaryKeyForNull(this.Batch));
            Command.Parameters.AddWithValue("BoxId", CheckPrimaryKeyForNull(this.BoxId));
            Command.Parameters.AddWithValue("BillingElementId", CheckPrimaryKeyForNull(this.BillingElementId));
            Command.Parameters.AddWithValue("TransactionDescription", CheckPrimaryKeyForNull(this.TransactionDescription));
            Command.Parameters.AddWithValue("Quantity", CheckIfNullAddZero(this.Quantity));

Command.ExecuteNonQuery();

I am using only one insert statement but it inserts two records. WOuld anyone know why this would be happening?

Thanks,

Darren.

EDIT: Here is the multi-threaded part of my code.

for (DateTime CurrentDate = BeginningDate; CurrentDate <= EndingDate; CurrentDate =     CurrentDate.AddDays(1))
{
    DateTime TempCurrentDate = CurrentDate;
    FileThreads[ThreadCounter] = new Thread(() => RipInformationFromFiles.Process(Parameters, TempCurrentDate, ArgsIndex));
    FileThreads[ThreadCounter].Start();
    ThreadCounter++;
 }

 foreach (Thread ThisThread in FileThreads)
 {
     if (ThisThread != null)
     {
         ThisThread.Join();
     }
 }

Each thread then looks through a certain path and updates information.
The only time I do have a lock is when I call the upload method of a class.

        lock (Locker)
        {
                ProductionDatabase.Upload();
        }

I use this static readonly object Locker = new object(); for my lock.

Upvotes: 0

Views: 106

Answers (3)

Richard
Richard

Reputation: 662

Threading is really hard. Really hard. So don't use it unless you have to and then think again. I think in your case it gives you nothing, since you lock around the part that is probably taking the most of the time, thereby essentially serializing your calls.

Use threads if your problem is CPU-bound (like calculations), otherwise use asynchronous calls, especially when you're on .NET 4.5.

Upvotes: 0

Darren Hoehna
Darren Hoehna

Reputation: 379

Found my answer. I just have to put a locker around ExecuteNonQuery(). :)

Upvotes: 0

Rajeev Bera
Rajeev Bera

Reputation: 2019

Here is the solution what I think

  • try to debug your application with Breakpoint
  • Make sure there is no triggers are there
  • if you are using this method make sure its not inserting again a new record on page refresh
  • Make sure you have not copied this code anywhere else or this function is not called anywhere

i am sure debug your application with Breakpoint will solve this issue

Hope it will help

Upvotes: 3

Related Questions