It works fine for me.
Try using transactionscope instead:
using System.Transactions;
...
public static void button1_Click()
{
var options = new TransactionOptions
{
Timeout = TimeSpan.FromSeconds(120)
};
try
{
using (var scope = new TransactionScope(TransactionScopeOption.Required, options))
{
using (SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["TRY"].ConnectionString))
{
using (SqlCommand query = con.CreateCommand())
{
query.CommandText = ("insert into wfe.Student_Data values(@param1,@param2,@param3)");
query.CommandType = CommandType.Text;
query.Parameters.Add("@param1", SqlDbType.VarChar).Value = "a";
query.Parameters.Add("@param2", SqlDbType.VarChar).Value = "b";
query.Parameters.Add("@param3", SqlDbType.VarChar).Value = "c";
con.Open();
var rowsadded = query.ExecuteNonQuery();
con.Close();
scope.Complete();
Console.WriteLine(@"{0} rows added",rowsadded);
}
}
}
}
catch (Exception ec)
{
Console.WriteLine(ec);
}
}
Note that you only need to open the connection at the last minute. The
Using
wrappers will help cleanly dispose of the connection even if an error does occur.
Note that I tested this in a console app so you will have to add your messageboxes back in