Because the path is wrong:
Unable to open the physical file "C:\TestDB\MyProgram_Data.mdf".
Doesn't work, but:
str = "EXEC sp_attach_db @dbname = N'MyProgram' ,@filename1 = N'C:\\TestDB\\MyProgram\\MyProgram_Data.mdf'
Does. It has an extra folder name in there: "MyProgram"