There is lots of code so try to narrow your problem by using the debugger and checking how the problem occurs.
One possibility is that if the check for existing rows isn't working, yuo could have a mismatch in the comparison against the database. One thing is that you should use
SqlParameter[
^] in your queries instead of concatenated literals in the statement. Also if the problem is that the row isn't found, check your collation settings and if upper and lower case characters are handled as different characters.