Any Idea why this code SqlCommand ExecuteNonQuery Throws StackOverFlowException

Any Idea why this code Throws StackOverFlowException

Thread Status:

I have no idea why this code always throws StackOverflowExeption, please take a look, this is the source script

1DROP TABLEtrantest1

2DROP TABLEtrantest2

3CREATE TABLEtrantest1 (idint identity(1000,1), messagenvarchar(1000))

4CREATE TABLEtrantest2 (idint identity(1000,1), messagenvarchar(1000))

5 GO

6DROP PROCusp_insert_message1;

7 GO

8CREATE PROCusp_insert_message1 @messagenvarchar(200)AS

9INSERT INTOtrantest1VALUES(@message);

10 GO

11DROP PROCusp_insert_message2;

12 GO

13CREATE PROCusp_insert_message2 @messagenvarchar(200)AS

14INSERT INTOtrantest2VALUES(@message);

15 GO

16SELECT*FROMtrantest1

17SELECT*FROMtrantest2

This is the test codes:

1publicclassTestBLL {

2

3publicvoid InsertMessages() {

4IDataAccessAdapter adapter =newSqlDataAdapter();

5

6try {

7 adapter.KeepConnectionAlive =true;

8 adapter.StartTransaction(IsolationLevel.ReadCommitted,"InsertMessage");

9

10TestMapper mapper1 =newTestMapper(adapter);

11 mapper1.InsertMessage1("first message");

12

13TestMapper mapper2 =newTestMapper(adapter);

14 mapper2.InsertMessage2("second message");

15

16 adapter.Commit();

17 }catch(Exception){

18 adapter.Rollback();

19 }finally {

20 adapter.Dispose();

21 }

22 }

23 }

1publicclassTestMapper :MapperBase {

2

3 #region constructors

4public TestMapper() :base() { }

5

6public TestMapper(string connectionString) :base(connectionString) { }

7

8public TestMapper(IDataAccessAdapter adapterToUse) :base(adapterToUse) { }

9 #endregion

10

11publicint InsertMessage1(string message) {

12int affectedRecords = 0;

13

14using (SqlConnection connection = Adapter.GetActiveConnection<SqlConnection>()) {

15SqlCommand command =newSqlCommand("usp_insert_message1", connection);

16 command.CommandType =CommandType.StoredProcedure;

17 command.Parameters.AddWithValue("@message", message);

18

19 Adapter.OpenConnection();

20 command.ExecuteNonQuery();//<<-- stackoverflow exception thrown here

21 }return affectedRecords;

22 }

23

24

25publicint InsertMessage2(string message) {

26int affectedRecords = 0;

27

28using (SqlConnection connection = Adapter.GetActiveConnection<SqlConnection>()) {

29SqlCommand command =newSqlCommand("usp_insert_message2", connection);

30 command.CommandType =CommandType.StoredProcedure;

31 command.Parameters.AddWithValue("@message", message);

32

33 Adapter.OpenConnection();

34 command.ExecuteNonQuery();//<<-- never called because of exception

35 }return affectedRecords;

36 }

37

38 }

I tried executing the script on QA and it works fine but when using SqlCommand, it throws that exception on ExecuteNonQuery

TIA!

[10913 byte] By [RodelE.Dagumampan] at [2007-12-26]
# 1
he'res something interesting also, here I cannot trace because its

the ExecuteNonQuery method tht throws the exception. trying other

options, i discovered that when you create an sql command and assign an

open connection which is currently in a transaction, the transaction

object is not yet assigned on the command object. So i change the code

and the xcerption was fixed. The code now looks like this,

1 public int InsertMessage1(string message) {

2 int affectedRecords = 0;

3

4 using (SqlConnection connection = Adapter.GetActiveConnection<SqlConnection>()) {

5 SqlCommand command = new SqlCommand("usp_insert_message1", connection, Adapter.GetActiveTransaction<SqlTransaction>());

6 command.CommandType = CommandType.StoredProcedure;

7 command.Parameters.AddWithValue("@message", message);

8

9 Adapter.OpenConnection();

10 affectedRecords = command.ExecuteNonQuery(); //<<-- affectedRecord is 1 but no record committed on database

11 } return affectedRecords;

12 }

13

14

15 public int InsertMessage2(string message) {

16 int affectedRecords = 0;

17

18 using (SqlConnection connection = Adapter.GetActiveConnection<SqlConnection>()) {

19 SqlCommand command = new SqlCommand("usp_insert_message2", connection, Adapter.GetActiveTransaction<SqlTransaction>());

20 command.CommandType = CommandType.StoredProcedure;

21 command.Parameters.AddWithValue("@message", message);

22

23 Adapter.OpenConnection();

24 affectedRecords = command.ExecuteNonQuery(); //<<-- affectedRecord is 1 but a NEW record committed on database

25 } return affectedRecords;

26 }

I

have one more issue here, please see the comments. The first method

InsertMessage1 is executed succesfully, it returns 1 record is affected

but no record on database. I asume its because I am no yet calling

Adater.Commit on my BLL. But weird, the second method InsertMessage2

insert the record and is already viewable on QA even If im not called

Adapter.Commit yet.

Even if I End the debgugger before calling

Adapter.Commit on BLL, still the "second message" is committed. Really

really strange. I am sure that command has the ActiveTransaction.

This is how a start the transaction:
//adapter.StartTransaction(IsolationLevel.ReadCommitted, "InsertMessage");

1 //create a physical transaction

2 public IDbTransaction CreatePhysicalTransaction() {

3 return (this.ActiveConnection as SqlConnection).BeginTransaction(this.IsolationLevel, this.TransactionName);

4 }

5

6 public void StartTransaction(IsolationLevel iso, string transactionName) {

7 //cannot start transaction while there pending

8 if (IsTransactionInProgress) {

9 throw new InvalidOperationException("Pending transaction not yet committed or rollback!");

10 }

11

12 this.IsolationLevel = iso;

13 this.TransactionName = transactionName;

14 this.OpenConnection();

15

16 this.ActiveTransaction = this.CreatePhysicalTransaction();

17 }


rodel e. dagumampan
software engineer/independent consultant
makati city, philippines, 1200
RodelE.Dagumampan at 2007-9-4 > top of Msdn Tech,.NET Development,.NET Framework Data Access and Storage...

.NET Development

Site Classified