Unique Constraint Exception handling in c# .NET

3k views Asked by At

I am a beginner in c# .NET and I am struggling to find an efficient way to catch an unique constraint exception in c# (windows forms).

I am dealing with a combination of three database table columns that are supposed to be unique but I am unable to handle the arising exception on my form.

The code that I am trying on the OnBeforeSaving Event of my database manager is attached below. Thanks in advance for the help. I am looking for an efficient way to catch the exception(Oracle).

private void dbInstCharges_OnBeforeSaving(object sender, DataManagerSaveArgs e)
{
    try
    {
        this.dbInstCharges.Table.PrimaryKey = new DataColumn[] { dbInstCharges.Table.Columns["EQ17_GC09_ID"], dbInstCharges.Table.Columns["EQ17_CH01_ID"], dbInstCharges.Table.Columns["EQ17_GC05_ID"] };
    }
    catch (ConstraintException ex)
    {
        MessageBox.Show(ex.Message);
    }

The updated block of code is

        private void dbInstCharges_OnBeforeSaving(object sender, DataManagerBeforeSavingArgs e)
       {
        //e.Record["EQ17_SS01_ID"] = Config.MemberID;
        //e.Record["EQ17_EQ16_ID"] = dbManager.CurrentDataRecord["EQ16_ID"];
        try
        {
            UniqueConstraint TableUnique = new UniqueConstraint (new DataColumn[] { dbInstCharges.Table.Columns["EQ17_GC09_ID"], dbInstCharges.Table.Columns["EQ17_CH01_ID"], dbInstCharges.Table.Columns["EQ17_GC05_ID"] });
            dbInstCharges.Table.Constraints.Add(TableUnique);
            //this.dbInstCharges.Table.PrimaryKey = new DataColumn[] { dbInstCharges.Table.Columns["EQ17_GC09_ID"], dbInstCharges.Table.Columns["EQ17_CH01_ID"], dbInstCharges.Table.Columns["EQ17_GC05_ID"] };
        }
        catch (Exception ex)
        {
            MessageBox.Show("Please enter a unique record");

        }
      }

Code after handling the ArgumentException.

    private void dbInstCharges_OnBeforeSaving(object sender, DataManagerBeforeSavingArgs e)
    {
        //e.Record["EQ17_SS01_ID"] = Config.MemberID;
        //e.Record["EQ17_EQ16_ID"] = dbManager.CurrentDataRecord["EQ16_ID"];
        try
        {

            UniqueConstraint TableUnique = new UniqueConstraint(new DataColumn[] { dbInstCharges.Table.Columns["EQ17_GC09_ID"], dbInstCharges.Table.Columns["EQ17_CH01_ID"], dbInstCharges.Table.Columns["EQ17_GC05_ID"], dbInstCharges.Table.Columns["EQ17_SS01_ID"], dbInstCharges.Table.Columns["EQ17_EQ16_ID"] });
            dbInstCharges.Table.Constraints.Add(TableUnique);
            //this.dbInstCharges.Table.PrimaryKey = new DataColumn[] { dbInstCharges.Table.Columns["EQ17_GC09_ID"], dbInstCharges.Table.Columns["EQ17_CH01_ID"], dbInstCharges.Table.Columns["EQ17_GC05_ID"], dbInstCharges.Table.Columns["EQ17_SS01_ID"], dbInstCharges.Table.Columns["EQ17_EQ16_ID"] };
        }
        catch (ArgumentException ex)
        {
            MessageBox.Show("Please enter a unique record");
        }

    }
1

There are 1 answers

2
Sebi On

I think you don't really understand Exception-Handling. I will try to explain your fault/missunderstanding as answer. If you execute a block of code this code runs up to the point where an exception occurs or it completes. If an exception occurs your code break and throw the exception or executes the catch-block if it exists one. If you use a try{} catch{} structure and an exception occurs, the code will following in catch{}. But there is no goto or jump back to your code in your try-block. What does this mean for your codeblock:

try
{
  UniqueConstraint TableUnique = new UniqueConstraint(new DataColumn[{//Your data});
  dbInstCharges.Table.Constraints.Add(TableUnique);
  //this.dbInstCharges.Table.PrimaryKey = new DataColumn[] {//Your data};
}
catch (ArgumentException ex)
{
  MessageBox.Show("Please enter a unique record");
}

The try-block is executed up to the point where the Argument-Exception occurs. Because you catch the ArgumentException (which is the right way), the code jumps to the catch-block and execute it. The Message is shown and your code is done. Make sure that after handling the exception the code doesn't jump back and execute further lines of your try-block. This won't and can't work because there is an wrong Argument which brokes your code. So it is absolutly nessesary that you fix your Argument-Exception. After fixing this issue you can handle your Unique-Constraint Exception as follows:

try
{
    UniqueConstraint TableUnique = new UniqueConstraint(new DataColumn[{//Your data});
  dbInstCharges.Table.Constraints.Add(TableUnique);
  //this.dbInstCharges.Table.PrimaryKey = new DataColumn[] {//Your data};
}
catch (OracleException ex)
{
  MessageBox.Show("Please enter a unique record.");
}

The ArgumentException always break your code before the code with your Unique-Constrain Exception is executed. That's why this exception is never catched.