2

I have a transaction with multiple inserts. All of the inserts work fine, except one. I verified the parameters, the spelling, all of it, and it seems I'm not figuring it out. It gives me the error:

Timeout expired. The timeout period elapsed prior to completion of the operation 
or the server is not responding.

My transaction looks like this:

SqlConnection db = new SqlConnection(connString);
DataSet dataSet = new DataSet();
SqlDataAdapter da = new SqlDataAdapter();
using (db)
{
    db.Open();
    SqlTransaction trans = db.BeginTransaction();
    try
    {
        //insert into COMMSignalDefinition !!Problem HERE
        da.InsertCommand = 
            new SqlCommand("INSERT INTO COMMSignalDefinition(Name) " 
                           + "VALUES (@name)", db, trans);

        da.InsertCommand.Parameters.Add("@name", SqlDbType.NVarChar);
        foreach (DataRow row in ds.Tables["COMMTerminalSignal"].Select())
        {
            da.InsertCommand.Parameters[0].Value = row.ItemArray[1];
            da.InsertCommand.ExecuteNonQuery();
        }

        // insert into COMMSignalExceptionDefinition -- names
        da.InsertCommand = 
            new SqlCommand("INSERT INTO COMMSignalExceptionDefinition(Name) " 
                           + "VALUES (@name)", db, trans);
        da.InsertCommand.Parameters.Add("@name", SqlDbType.NVarChar);
        foreach (DataRow row in ds.Tables["COMMSignalExceptionDef"].Select())
        {
            da.InsertCommand.Parameters[0].Value = row.ItemArray[1];
            da.InsertCommand.ExecuteNonQuery();
        }

        trans.Commit();
        MessageBox.Show("You have successfully imported your Settings. " 
                        + "You can now exit the program.", 
                         "Success",
                         MessageBoxButtons.OK, 
                         MessageBoxIcon.Information);
    }
    catch (Exception e)
    {
        trans.Rollback();
        MessageBox.Show(e.Message,
                        "Error", 
                        MessageBoxButtons.OK, 
                        MessageBoxIcon.Error);
    }
}

I have more inserts which work fine (I deleted the rest of them) and I moved the one with problems at the beginning. My question is what could I possibly do wrong? I even verified if the "problematic" query is sent to the server, with SQL Server Profiler, and it does! And if I execute it in SQL Server Management studio, it works too.

Connection Timeout is set to 30

Can You please give me some leads ? SQL Server version is 2005 ! Thank You!

8
  • What's the number of records in the tables "COMMTerminalSignal" and "COMMSignalExceptionDef"? How long does it take to run this in SQL Server Management Studio? Commented Jan 18, 2013 at 9:42
  • COMMTerminalSignal has 2 records, COMMSignalExceptionDef (the insert works on this one) has 1 record Commented Jan 18, 2013 at 9:47
  • @Jacco It runs instantly Commented Jan 18, 2013 at 9:49
  • And running the profiler, so see 3 inserts appearing? Commented Jan 18, 2013 at 9:57
  • @Jacco yes they are appearing, since the other inserts work fine (and the rows are added in database) when i remove the one with problems ... Commented Jan 18, 2013 at 10:12

4 Answers 4

3

After hours of digging, it came out I did some tests in Management Studio, where I tested some transactions without committing them at all. So it was waiting for a commit, and I kept making, or trying to make inserts ... !

Sign up to request clarification or add additional context in comments.

1 Comment

I would just rewrite your own answer, so it has less smileys and mark it as "The answer". I guess we've all been there more than once!
1

If this is a pretty long running operation, you can try to change the Command Timeout.

Comments

1
  1. Check your connection string. Maybe you have some mistake in your connString var
  2. Check your SqlDataAdapter command timeouts. They may be the cause of the exception (see http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlcommand.commandtimeout.aspx) Usually such exception is the result of either long running tasks or uncommitted transactions.

Comments

0

Connection Timeout is set to 1

SqlConnection.ConnectionTimeout Property

The time (in seconds) to wait for a connection to open. The default value is 15 seconds.

So you have set the timout to 1 second. Does that answer your question?

Remarks: You can set the amount of time a connection waits to time out by using the ConnectTimeout or Connection Timeout keywords in the connection string. A value of 0 indicates no limit, and should be avoided in a ConnectionString because an attempt to connect waits indefinitely.

1 Comment

Thank You for the answer, @Tim Schmelter, but I have tried with the default Conn Time, even with 0, with the same results :(. The problem is that when I debug the code, it freezes at that line untill the exception is caught ...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.