Ouch, silly mistake, thanks for pointing it out.
Finally I could solve the problem. I found out that the error 505 generates when there's an error in the operation and then in another iteration of the same operation a no-error overwrites the error. I used the shift registers in the for loop, and noticed that now another error showed, saying that I was writing a duplicate of a primary key. So I checked the array I was writing to the DB and in fact I had stored data from the previous operations that I already wrote in the DB. After fixing that the problem was solved.
Thanks!