Click here to Skip to main content
16,020,315 members
Home / Discussions / C#
   

C#

 
GeneralRe: Importing environment variables Pin
W. Kleinschmit24-Feb-09 13:11
W. Kleinschmit24-Feb-09 13:11 
GeneralRe: Importing environment variables Pin
PIEBALDconsult24-Feb-09 13:12
mvePIEBALDconsult24-Feb-09 13:12 
JokeRe: Importing environment variables Pin
W. Kleinschmit24-Feb-09 13:18
W. Kleinschmit24-Feb-09 13:18 
GeneralRe: Importing environment variables Pin
W. Kleinschmit24-Feb-09 13:27
W. Kleinschmit24-Feb-09 13:27 
GeneralRe: Importing environment variables Pin
PIEBALDconsult24-Feb-09 15:03
mvePIEBALDconsult24-Feb-09 15:03 
QuestionDisassembly cannot be displayed. The expression has not yet been translated to native machine code. Pin
bemahesh24-Feb-09 11:32
bemahesh24-Feb-09 11:32 
AnswerRe: Disassembly cannot be displayed. The expression has not yet been translated to native machine code. Pin
Mark Churchill24-Feb-09 14:07
Mark Churchill24-Feb-09 14:07 
QuestionProblem with multiple insert via TransactionScope class. Pin
hdv21224-Feb-09 10:58
hdv21224-Feb-09 10:58 
Hi i want to insert multiple rows to database in a single transaction via TransactionScope class, for this, i iterate through datatable that binds to datagridview for get list of rows to be inserted. here is my code :
using (SqlConnection con = new SqlConnection("data source=.;initial catalog=test;Trusted_Connection=Yes"))
            {
                SqlCommand cmd = con.CreateCommand();
                cmd.CommandText = "insert Person(titleId,fname,lname) values(@titleId,@fname,@lname)";
                con.Open();

                System.Transactions.TransactionOptions trOptions = new System.Transactions.TransactionOptions();
                trOptions.IsolationLevel = System.Transactions.IsolationLevel.ReadCommitted;
                trOptions.Timeout = new TimeSpan(0, 0, 15);
                using (System.Transactions.TransactionScope ts = new System.Transactions.TransactionScope(System.Transactions.TransactionScopeOption.Required, trOptions))
                {
                    try
                    {
                        foreach (DataRow row in this.dt1.Rows)
                        {
                            int titleId = int.Parse(row["titleId"].ToString());
                            string fname = row["fname"].ToString();
                            string lname = row["lname"].ToString();

                            if (cmd.Parameters.Count > 0)
                                cmd.Parameters.Clear();

                            cmd.Parameters.AddWithValue("@titleId", titleId);
                            cmd.Parameters.AddWithValue("@fname", fname);
                            cmd.Parameters.AddWithValue("@lname", lname);
                            cmd.ExecuteNonQuery();

                        }
                        con.Close();
                        ts.Complete();
                    }
                    catch (Exception ex)
                    {

                    }                    
                }
            }


but when any error occur during foreach loop (insert loop), those rows that inserted to database, could not rollback.
where does my problem and how to solve it ?

Note : i know this can implement by SqlTransaction, but i want to do with TransactionScope class.
Thanks
AnswerRe: Problem with multiple insert via TransactionScope class. Pin
Jon Rista24-Feb-09 12:35
Jon Rista24-Feb-09 12:35 
QuestionC# alias Pin
Bill Stumps24-Feb-09 10:05
Bill Stumps24-Feb-09 10:05 
AnswerRe: C# alias Pin
Pete O'Hanlon24-Feb-09 10:10
mvePete O'Hanlon24-Feb-09 10:10 
GeneralRe: C# alias Pin
PIEBALDconsult24-Feb-09 10:31
mvePIEBALDconsult24-Feb-09 10:31 
GeneralRe: C# alias Pin
Pete O'Hanlon24-Feb-09 10:49
mvePete O'Hanlon24-Feb-09 10:49 
GeneralRe: C# alias Pin
PIEBALDconsult24-Feb-09 10:56
mvePIEBALDconsult24-Feb-09 10:56 
GeneralRe: C# alias Pin
Pete O'Hanlon24-Feb-09 11:07
mvePete O'Hanlon24-Feb-09 11:07 
GeneralRe: C# alias Pin
PIEBALDconsult24-Feb-09 12:55
mvePIEBALDconsult24-Feb-09 12:55 
GeneralRe: C# alias Pin
_Maxxx_24-Feb-09 16:00
professional_Maxxx_24-Feb-09 16:00 
GeneralRe: C# alias Pin
PIEBALDconsult24-Feb-09 16:42
mvePIEBALDconsult24-Feb-09 16:42 
GeneralRe: C# alias Pin
_Maxxx_24-Feb-09 21:23
professional_Maxxx_24-Feb-09 21:23 
GeneralRe: C# alias Pin
Pete O'Hanlon24-Feb-09 21:46
mvePete O'Hanlon24-Feb-09 21:46 
GeneralRe: C# alias Pin
_Maxxx_24-Feb-09 22:54
professional_Maxxx_24-Feb-09 22:54 
GeneralRe: C# alias Pin
Jon Rista24-Feb-09 12:41
Jon Rista24-Feb-09 12:41 
GeneralRe: C# alias Pin
PIEBALDconsult24-Feb-09 12:53
mvePIEBALDconsult24-Feb-09 12:53 
GeneralRe: C# alias Pin
Jon Rista24-Feb-09 17:06
Jon Rista24-Feb-09 17:06 
GeneralRe: C# alias Pin
PIEBALDconsult24-Feb-09 17:13
mvePIEBALDconsult24-Feb-09 17:13 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.