Click here to Skip to main content
15,891,607 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I'm making a transaction in my code and I get unreacheable code detected.Can someone help me to correct it?trans.Commit can't be reached,finding it hard to find solution.
after
C#
<pre>return await _dbContext.SaveChangesAsync() > 0;

I can't reach
trans.Commit()


What I have tried:

public async Task<bool> Save(string company, int number,string registrationNumber)
        {

            using (var trans = _dbContext.Database.BeginTransaction())
            {
                var db = new SibaCiidDbContext();
                var dbSet = _dbContext.Set<IntermediaryAssignment>();
            
                // set the database
                var check =await  (from s in db.StickerDistributions
                                    join i in db.IntermediaryAssignment
                                    on s.CompanyCode equals i.CompanyCode
                                    where s.Dispatched == false && s.CompanyCode == 
                                     company <pre>&& s.StickerCode != i.StickerCode
                             
                             select s).ToListAsync();

                var datas = await (from s in db.StickerDistributions
                                  where s.Dispatched == false && s.CompanyCode == 
                                   company && s.IntermediaryDispatched == false select s)
                                   .ToListAsync();
                                   

                var data = await (from s in db.StickerDistributions
                                  where s.Dispatched == false && s.CompanyCode == company &&
                                  s.IntermediaryDispatched == false
                                  select s).Take(number).ToListAsync();

                var intermediary = (await _repo.FindBy(s => s.RegistrationNumber == 
                                    registrationNumber && s.Status == EntityStatus.Active)).FirstOrDefault();
                
            
             
               foreach (var sticker in data)
                {
                    if (dbSet.Any(s => s.StickerCode != sticker.StickerCode))
                    {
                        var entity = new IntermediaryAssignment();

                        entity.CompanyCode = sticker.CompanyCode;
                        entity.StickerCode = sticker.StickerCode;
                        entity.RegistrationNumber = intermediary.RegistrationNumber;
                        entity.Status = EntityStatus.Active;
                        entity.CreatedDate = DateTime.Now;
                        entity.Dispatched = false;
                        entity.IntermediaryType = intermediary.IntermediaryType;
                        // entity.Sticker.Id = sticker.Sticker.Id;
                        sticker.IntermediaryDispatched = true;


                        dbSet.Add(entity);
                    }
                    

                }
                return await _dbContext.SaveChangesAsync() > 0;
                trans.Commit();

                

            }

        }
Posted
Updated 22-Aug-18 18:14pm

That's because you use the keyword return before trans.Commit(). That line will never be called.

What you can do is something like this:

C#
var result = await _dbContext.SaveChangesAsync();
trans.Commit();
if(result > 0)
      return true;
return false;
 
Share this answer
 
v4
Comments
Member 13053943 23-Aug-18 0:06am    
it didn't work,i keep getting "cannot implicitly convert type int to System.Threading.Task.Task" and "> cannot be used on type int and void"
Vincent Maverick Durano 23-Aug-18 0:13am    
I didn't see that your method is expecting a Task<bool>. What you can do is either:

change your method to Task<int>
or do something like this:

if(result > 0)
return true;
else
return false;
Member 13053943 23-Aug-18 0:14am    
i fixed it bro,thank you.since its a bool,it has to return true or false
Vincent Maverick Durano 23-Aug-18 0:15am    
I've updated the code based on my suggestion to fix that.
Vincent Maverick Durano 23-Aug-18 0:16am    
Oh cool. Glad it works for you now. Please don't forget to close this thread if your issue is resolved for the sake of future readers. Thanks!
Task results =  _dbContext.SaveChangesAsync() ;
                 trans.Commit();
                return  true ;
 
Share this answer
 
Comments
Vincent Maverick Durano 23-Aug-18 0:18am    
this will always return true. If you don't need the result then you can simply do:

await _dbContext.SaveChangesAsync() ;
trans.Commit();
return true ;

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900