Checks the result from the stored porcedure

The stored procedure we use gives back a result. In this result we can see if there is a handled error and what error there is. Its is simple to extend its with error that are not handled in the stored procedure.

public class ExampleClass
{
  private static string errorMssg = string.Empty;
  // To use in the form
  public static string errorMsg { get { return errorMssg; } }
  // To use in the class only. It is not allowed to set the error from the form
  protected static string errorMsgSet { set { errorMssg = value; } }

  /*
   * Checks the result from the stored porcedure
   */
  
  /// If all is good then returns true, IF there are errors returns false and set a errormessages
  protected bool checkResult(int result)
  {
      switch (result)
      {
          case 999:
              return true;
          case 998:
              errorMsgSet = "error message";
              return false;
          case 997:
              errorMsgSet = "error message 2";
              return false;
          case 996: //update
              errorMsgSet = "error message 3";
              return false;
          case 995: //update
              errorMsgSet = "error message 4";
              return false;
          default:
              errorMsgSet = "error message 5";
              return false;
      }
  }
}

Howto use this function in a class

public bool exampleNew()
{
    dt = DatabaseClasses.Dac.MsSql.ExecuteStoredProcedure.ExecuteDataTable(Properties.Resources.I_Example, ref result
                                , DatabaseClasses.Dac.MsSql.ExecuteStoredProcedure.parameter(EXAMPLEID, a_ExampleID)
                                , DatabaseClasses.Dac.MsSql.ExecuteStoredProcedure.parameter(EXAMPLENAME, a_ExampleName)
                                , DatabaseClasses.Dac.MsSql.ExecuteStoredProcedure.parameter(USER, "None")
                                , DatabaseClasses.Dac.MsSql.ExecuteStoredProcedure.parameter(RETURNVALUE, 0)
        );
    bool error = checkResult(result);
    if (error && dt != null)
        return true;
    else
        return false;
}

Howto use it in a form

if (_clsAdres.exampleNew())
{
// your code
}
else
    MessageBox.Show(ExampleClass.errorMsg);
break;

Leave a Reply