views:

57

answers:

1

i try to learn transaction scope. Everything looks good. i try to control transaction is running i add 500 char in NAME value (Name nvarchar(50) normally). While My formApplication not running (it is normal), i try to open a table table is empty. Stop running my app form . i can see values of Tables. WHY?

using (var ctx = new DataClassesCallCenterDataContext())
{
    using (var scope = new TransactionScope())
    {
        var test =
            from c in ctx.sp_CallCenterAnketEntity()
            select c;

        int? ID = test.ToList()[0].ID;

        var question = new QuestionsYesNo();
        question.Question = "Test3?";
        question.Date = DateTime.Now;

        ctx.QuestionsYesNos.InsertOnSubmit(question);
        ctx.SubmitChanges();

        Rehber rehber = (
            from r in ctx.Rehbers 
            where  r.ID == ID
            select r).First();

        rehber.Name =
            @"SQL Server 2005'i bir [many more characters] izleyin.";

        ctx.SubmitChanges();

        scope.Complete();
    }
}

i try to open QuestionsYesNo table. i can not in my application running?

A: 

Just a little rewrite to show you how you code could look like:

// use nested using block to decrease the indent
using (var ctx = new DataClassesCallCenterDataContext())
using (var scope = new TransactionScope())
{
    var test =
        from c in ctx.sp_CallCenterAnketEntity()
        select c;

    // if you're sure that query result will have at least one record.
    // else an exception will occur. use FirstOrDefault() if not sure.
    // and why int? - is it the type of ID. Isn't int the type?
    int? ID = test.First().ID; 

    // here you can use object initializer
    var question = new QuestionsYesNo
    {
        Question = "Test3?",
        Date = DateTime.Now
    };

    ctx.QuestionsYesNos.InsertOnSubmit(question);
    ctx.SubmitChanges();

    Rehber rehber = (
        from r in ctx.Rehbers
        where r.ID == ID
        select r).First(); // again about being sure and FirstOrDefault

    rehber.Name = @"SQL Server 2005'i bir [many more characters] izleyin.";

    ctx.SubmitChanges();    
    scope.Complete();
}
abatishchev