Search code examples
c#ado.netfirebirdfirebird-embedded

Why the ExecuteNonQuery catch exception {"validation error for column \"ORGTABLE\".\"FIKEYID\", value \"* null *\""}


Why the ExecuteNonQuery catch exception {"validation error for column \"ORGTABLE\".\"FIKEYID\", value \"* null *\""}

string stValuesPlaceHolder = "@p0";
for (int iii = 1; iii < liststFieldValuesNoKeyId.Count; iii++)
    stValuesPlaceHolder += ", @p" + (iii).ToString();

FbTransaction fbTransaction = fbConn.BeginTransaction();
FbCommand fbCmd = new FbCommand("INSERT INTO " + stTableName + "(" + stFieldNamesNoKeyId + ") VALUES ( " + stValuesPlaceHolder + " )", fbConn, fbTransaction);

for (int iii = 0; iii < liststFieldValuesNoKeyId.Count; iii++) {
    string stPlaceHolder = "@p" + (iii).ToString();
    string stValue = liststFieldValuesNoKeyId[iii];
    fbCmd.Parameters.AddWithValue(stPlaceHolder, stValue);
}

fbCmd.ExecuteNonQuery();
fbTransaction.Commit();

The stTableName is OrgTable.

The fields names are:

fstPriority, fstInfo, fstDateCreated, fstDateModified, fiKeyID.

The field definitions are:

fstPriority VARCHAR(30), fstInfo VARCHAR(100), fstDateCreated VARCHAR(30), fstDateModified VARCHAR(30), fiKeyID INTEGER PRIMARY KEY

In this section of the code:

stFieldNamesNoKeyId = "fstPriority, fstInfo, fstDateCreated, fstDateModified".

stValuesPlaceHolder = "@p0, @p1, @p2, @p3"

Four fbCmd.Parameters.AddWithValue:

stPlaceHolder = "@p0" ... stValue = "1st value";

stPlaceHolder = "@p1" ... stValue = "2nd value";

stPlaceHolder = "@p2" ... stValue = "3rd value";

stPlaceHolder = "@p3" ... stValue = "4th value";

I did not add a value for fiKeyID as it as the PRIMARY KEY.


Solution

  • Definitions:

    public const string stMAIN_TABLE_NAME = " OrgTable ";
    public const string stDELETED_TABLE_NAME = "  BackupTable ";
    
    public const string stFIELD_DEFINITIONS = " fstPriority VARCHAR(30)" + 
                                              ", fstInfo VARCHAR(100)" +
                                              ", fstDateCreated VARCHAR(30)" +
                                              ", fstDateModified VARCHAR(30)" +
                                              ", fiKeyID INTEGER GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY ";
    
    public const string stFIELD_NAMES_NO_KEY_ID = " fstPriority" + 
                                                  ", fstInfo" + 
                                                  ", fstDateCreated" + 
                                                  ", fstDateModified ";
    
    public const string stFIELD_NAMES_KEY_ID = " fiKeyID ";
    
    public const string stFIELD_NAMES = stFIELD_NAMES_NO_KEY_ID + ", " + stFIELD_NAMES_KEY_ID;
    

    Code:

    //------------------------------
    static private bool boCreateDatabaseTables(string stPathFilename, 
                                               string stUserID, 
                                               string stPassword, 
                                               List<string> liststTableNames, 
                                               List<string> liststFieldDefinitions) 
    {
      bool boErrorFlag = false;
      int iTablesCount = liststTableNames.Count();
      string stOpenConn = new FbConnectionStringBuilder {
        Database = stPathFilename,
        UserID = stUserID,
        Password = stPassword,
        ServerType = FbServerType.Embedded,
        ClientLibrary = stCLIENT_LIBRARY
        }.ToString();
      using (FbConnection fbConn = new FbConnection(stOpenConn)) {
        try {
          fbConn.Open();
    
          FbTransaction fbTransaction = fbConn.BeginTransaction();
          for (int ii = 0; ii < iTablesCount; ii++) {
            string stSql = "CREATE TABLE " + liststTableNames[ii] + "( " + liststFieldDefinitions[ii] + ")";
            FbCommand fbCmd = new FbCommand(stSql, fbConn, fbTransaction);
            fbCmd.ExecuteNonQuery();
          }
          fbTransaction.Commit();
        }
        catch (Exception ex) {
          boErrorFlag = true;
          MessageBox.Show("catch ... GlobalsFirebird ... boCreateDatabaseTables ... " + ex.Message);
        }
    }
    return boErrorFlag;
    }//boCreateDatabaseTables
    //------------------------------
    //------------------------------
    static public bool boAddRow(string stPathFilename,
                                string stUserID,
                                string stPassword,
                                string stTableName,
                                string stFieldNamesNoKeyId,
                                string stFieldNamesKeyId,
                                List<string> liststFieldValuesNoKeyId) 
    {
      bool boErrorFlag = false;
      string stOpenConn = new FbConnectionStringBuilder {
        Database = stPathFilename,
        UserID = stUserID,
        Password = stPassword,
        ServerType = FbServerType.Embedded,
        ClientLibrary = stCLIENT_LIBRARY
      }.ToString();
    
      using(FbConnection fbConn = new FbConnection(stOpenConn)) {
        fbConn.Open();
        try {
          string stValuesPlaceHolder = "@p0";
          for (int iii = 1; iii < liststFieldValuesNoKeyId.Count; iii++)
            stValuesPlaceHolder += ", @p" + (iii).ToString();
          FbTransaction fbTransaction = fbConn.BeginTransaction();
          string stCmd = "INSERT INTO " + stTableName + "(" + stFieldNamesNoKeyId + ") VALUES ( " + stValuesPlaceHolder + " ) RETURNING  " + stFieldNamesKeyId;
          FbCommand fbCmd = new FbCommand(stCmd, fbConn, fbTransaction);
    
          for (int iii = 0; iii < liststFieldValuesNoKeyId.Count; iii++) {
            string stPlaceHolder = "@p" + (iii).ToString();
            string stValue = liststFieldValuesNoKeyId[iii];
            fbCmd.Parameters.AddWithValue(stPlaceHolder, stValue);
          }
          fbCmd.Parameters.Add(new FbParameter() { Direction = System.Data.ParameterDirection.Output });
          fbCmd.ExecuteNonQuery();
          fbTransaction.Commit();
        }
        catch (Exception ex) {
          boErrorFlag = true;
          MessageBox.Show("catch ... GlobalsFirebird ... boAddRow ... " + ex.Message);
        }
      }
      return boErrorFlag;
    }//boAddRow
    //------------------------------