I have added unique constraint to datatable like this
DataTable dtemp
private void TempTable()
{
dtemp = new DataTable("Temp");
dtemp.Columns.Add(new DataColumn("Table", typeof(int)));
dtemp.Columns.Add(new DataColumn("Capacity", typeof(int)));
UniqueConstraint TableUnique = new UniqueConstraint(new DataColumn[] { dtemp.Columns["Table"] });
dtemp.Constraints.Add(TableUnique);
}
When I try to add same record, instead of going to catch block, it simply exit the program abruptly and gives unique constraint unhandled exception in the visual studio.
private void GetTable()
{
try
{
dtemp.Rows.Add(mTable.TableID,mTable.Capacity);
}
catch(SqlException ee)
{
if (ee.Number == 2627)
{
MessageBox.Show("Sorry, This Table Is Already Reserved!");
if (Con.State == ConnectionState.Open) { Con.Close(); }
return;
}
MessageBox.Show("Exception: " + ee.Message);
}
Can anyone please tell me how to handle this exception?