G
Guest
Hello,
I'm using SqlDataAdapter to insert data from one table into another with
SqlCommand after I start a transaction.
If I have convertion error for the insertion of the data, The transaction is
no longer valid, I'ts rolled back.
This is my sample code:
try
{
SqlConnection conn = new SqlConnection("Application
Name=UpgradeDLL;workstation id=Sharon"
+ ";packet size=4096;user id="
+ "sa"
+ ";Password="
+ ""
+ ";data source=" + '"'
+ "192.168.110.30"
+ '"' + ";persist security info=False;initial catalog="
+ "aa");
conn.Open();
SqlTransaction tran = conn.BeginTransaction();
try
{
SqlCommand cmd = new SqlCommand("alter table table1 add a3 int", conn,
tran);
cmd.ExecuteNonQuery();
cmd = new SqlCommand("insert into table1 (a3) values('txt')", conn, tran);
cmd.ExecuteNonQuery();
}
catch(Exception exp)
{
System.Diagnostics.Debug.WriteLine(exp.Message);
}
finally
{
tran.Commit();
conn.Close();
}
}
catch(Exception exp1)
{
System.Diagnostics.Debug.WriteLine(exp1.Message);
}
I want to commit the transaction even if there was an error.
But I get this exception at tran.Commit()
exp1:
"The COMMIT TRANSACTION request has no corresponding BEGIN TRANSACTION"
I want to be able to commit the transaction after the error.
Thanks,
Sharon
I'm using SqlDataAdapter to insert data from one table into another with
SqlCommand after I start a transaction.
If I have convertion error for the insertion of the data, The transaction is
no longer valid, I'ts rolled back.
This is my sample code:
try
{
SqlConnection conn = new SqlConnection("Application
Name=UpgradeDLL;workstation id=Sharon"
+ ";packet size=4096;user id="
+ "sa"
+ ";Password="
+ ""
+ ";data source=" + '"'
+ "192.168.110.30"
+ '"' + ";persist security info=False;initial catalog="
+ "aa");
conn.Open();
SqlTransaction tran = conn.BeginTransaction();
try
{
SqlCommand cmd = new SqlCommand("alter table table1 add a3 int", conn,
tran);
cmd.ExecuteNonQuery();
cmd = new SqlCommand("insert into table1 (a3) values('txt')", conn, tran);
cmd.ExecuteNonQuery();
}
catch(Exception exp)
{
System.Diagnostics.Debug.WriteLine(exp.Message);
}
finally
{
tran.Commit();
conn.Close();
}
}
catch(Exception exp1)
{
System.Diagnostics.Debug.WriteLine(exp1.Message);
}
I want to commit the transaction even if there was an error.
But I get this exception at tran.Commit()
exp1:
"The COMMIT TRANSACTION request has no corresponding BEGIN TRANSACTION"
I want to be able to commit the transaction after the error.
Thanks,
Sharon