I'm saving a DataTable(dt) to database table(tblCountry) using bulkcopy. There are constraints set for tblCountry , so exceptions throw up if there is a mistake in dt.
For eg: tblCountry has CountryId as the Primary Key. If dt has 2 records with same CountryId, that would throw an exception. I would like to get that CountryId which is a duplicate, when there is an exception. Is this possible with C# or maybe Stored Procedure SqlServer 2008
protected void btnSave_Click()
{
try
{
conn.Open();
SqlBulkCopy bulkcopy = new SqlBulkCopy(conn);
bulkcopy.DestinationTableName = "tblCountry";
bulkcopy.ColumnMappings.Add(0, 1);
bulkcopy.ColumnMappings.Add(1, 2);
bulkcopy.ColumnMappings.Add(2, 3);
bulkcopy.WriteToServer(dt);
conn.Close();
}
Catch(SqlException ex)
{
throw ex;
}
}