Why are you generating the serial yourself, rather than letting the database do it automatically (by using an Identity/AutoNumber/Serial data type) ?

That would not only take less code, but would also avoid the error and other issues with serial numbers that Identity etc deal with.