Hi,
the problem is that my scrip stops if a duplicate key is found and i would like to know how to handle duplicate inserts into database and if founded ignore and don't insert the data or handle the type of error.

try{
     cmd.CommandText = "Insert  into subscritor .....
     cmd.ExecuteNonQuery();
                                
   }
catch (SqlException exc)
      {
               //what do i say for duplicate values?                 
      }
 throw;

What data type is your key? The easiest way to solve this is to have an integer key type and have it auto_increment. Then you don't have to worry about duplicate keys.

Otherwise I guess the only way is to generate your key, perform a select on the database and check if anything is returned. If not, your key is 99% safe. However, this won't work if more than one application/process/thread is working with your database as you might check with two threads, the same key. Then it's a matter of, whoever gets there first wins.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.