PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Tuesday, July 12, 2022

[FIXED] How to prompt a message to a C# Program from a firebird database Insert Procedure

 July 12, 2022     c#, firebird, insert, message, messagebox     No comments   

Issue

I have here an insert procedure in firebird, what it does is to verify if the data I am trying to save is existing in the table. If the data I am trying to save exist in the table, then the data will not be saved. If this is the case, How will I prompt the user that data is not saved because the data exist?

here is my insert procedure

    CREATE PROCEDURE TRAINTRN_INSERT(
  EMP_PK INTEGER,
  TRAINTRN_EMP_TYPE VARCHAR(50) CHARACTER SET ISO8859_1 COLLATE ISO8859_1,
  TRAINTRN_BATCH VARCHAR(50) CHARACTER SET ISO8859_1 COLLATE ISO8859_1,
  TRAINTRN_AREA VARCHAR(100) CHARACTER SET ISO8859_1 COLLATE ISO8859_1,
  TRAINTRN_SRCVCRDT DECIMAL(12, 2),
  TRAINING_PK INTEGER,
  USER_PK SMALLINT,
  TRAINTRN_UPDATETIME TIMESTAMP)
AS
DECLARE VARIABLE EXSTING_EMP_PK INTEGER;
DECLARE VARIABLE EXSTING_TRAINING_PK INTEGER;
BEGIN
 FOR
SELECT  
       COUNT(A.EMP_PK)
FROM TRAINTRN_TABLE A WHERE A.TRAINING_PK =: TRAINING_PK AND  A.EMP_PK =: EMP_PK
INTO
:EXSTING_EMP_PK
DO

IF (EXSTING_EMP_PK = 0) THEN
BEGIN
  INSERT INTO TRAINTRN_TABLE (
    EMP_PK,
    TRAINTRN_EMP_TYPE,
    TRAINTRN_BATCH,
    TRAINTRN_AREA,
    TRAINTRN_SRCVCRDT,
    TRAINING_PK,
    USER_PK,
    TRAINTRN_UPDATETIME)
  VALUES (
    :EMP_PK,
    :TRAINTRN_EMP_TYPE,
    :TRAINTRN_BATCH,
    :TRAINTRN_AREA,
    :TRAINTRN_SRCVCRDT,    
    :TRAINING_PK,
    :USER_PK,
    CURRENT_TIMESTAMP);
    END
END;

and here is my code for c#

var adder = new ClsHR();
adder.SaveParticipants();

XtraMessageBox.Show("Successfully Added.", "Save ",
MessageBoxButtons.OK, MessageBoxIcon.Asterisk);

public void SaveParticipants()
        {
            try
            {
                var adder = new FbConnection(ClsConnection.FirebirdSQL);
                var fbcmd = new FbCommand("TRAINTRN_INSERT", adder)
                {
                    CommandType = CommandType.StoredProcedure
                };
                fbcmd.Parameters.Add("@EMP_PK", FbDbType.VarChar).Value = UpdtHndlrEmp;
                fbcmd.Parameters.Add("@TRAINTRN_EMP_TYPE", FbDbType.VarChar).Value = TRAINTRN_EMP_TYPE;
                fbcmd.Parameters.Add("@TRAINTRN_BATCH", FbDbType.VarChar).Value = TRAINTRN_BATCH;
                fbcmd.Parameters.Add("@TRAINTRN_AREA", FbDbType.VarChar).Value = TRAINTRN_AREA;
                fbcmd.Parameters.Add("@TRAINTRN_SRCVCRDT", FbDbType.VarChar).Value = TRAINTRN_SRCVCRDT;
                fbcmd.Parameters.Add("@TRAINING_PK", FbDbType.VarChar).Value = UpdateHandler;
                fbcmd.Parameters.Add("@USER_PK", FbDbType.SmallInt).Value = ClsEmployee.USER_PK;
                fbcmd.Parameters.Add("@TRAINTRN_UPDATETIME", FbDbType.VarChar).Value = EventTimestamp;
                fbcmd.Connection.Open();
                fbcmd.ExecuteNonQuery();
                fbcmd.Connection.Close();
            }
            catch (Exception errorcode)
            {
                XtraMessageBox.Show(String.Format("Error in connection: {0}. Saving failed.", errorcode.Message), @"Server Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }
        }

Solution

You may create an exception.

CREATE EXCEPTION MY_EXCEPTION 'Exception text';

Then in stored procedure call the exception if the data is there:

IF (EXSTING_EMP_PK = 0) THEN
BEGIN
  INSERT INTO TRAINTRN_TABLE (
    EMP_PK,
    .....
END
ELSE
  EXCEPTION MY_EXCEPTION;

Then in C code you may get the exception as usual.

Another way:

Just return a result from stored procedure for example :

IF (EXSTING_EMP_PK = 0) THEN
BEGIN
  INSERT INTO TRAINTRN_TABLE (
    EMP_PK,
    .....
  OUTPUT_RESULT = 1;
END
ELSE
  OUTPUT_RESULT = 0;

SUSPEND;


Answered By - Val Marinov
Answer Checked By - Timothy Miller (PHPFixing Admin)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing