I am using TableAdapter to insert records in table within a loop.
foreach(....)
{
....
....
teamsTableAdapter.Insert(_teamid, _teamname);
....
}
Where TeamID is the primary key in the table and _teamID inserts it. Actually i am extracting data from XML file which contains unique teamId
After first run of this loop, Insert throws Duplicate Primary Key found Exception. To handle this, i have done this
foreach(....)
{
....
....
try
{
_teamsTableAdapter.Insert(_teamid, _teamname);
}
catch (System.Data.SqlClient.SqlException e)
{
if (e.Number != 2627)
MessageBox.Show(e.Message);
}
....
....
}
But using try catch statement is costly, how to avoid this exception. I am working in VS2010 and INSERT ... ON DUPLICATE KEY UPDATE
does not work.
I want to avoid try catch statements and handle it without the use of try catch statements.