Search code examples
c#sqlsql-servermodeexecutenonquery

How to debug null object field value on ExecuteNonQuery?


I've passed an typed object containing multiple field values into an SQL command which calls an Insert stored procedure. On debug I can see that each of the fields have a value and none are null.

But when I debug further into the db.ExecuteNonQuery call, I get an SQL exception stating that the Application field is null. Although it isn't null and has a string value prior to executing this command.

I checked the usual debug steps, the model field type matches the type in the DbCommand. Also I checked the escalation.Application value prior to executing the query which is populated with a string value as expected.

Question:

Does anyone know why the field value is evaluated as null on ExecuteNonQuery?

Code:

  • DAL class -

    public bool InsertWebReq(Escalation escalation)
    {
        Database db = null;
        string sqlCommand = "";
    
        try
        {
            DatabaseProviderFactory factory = new DatabaseProviderFactory();
            db = factory.Create("NOTIFICATION");
    
            sqlCommand = "CREATE_ESCALATION";
    
            using (DbCommand dbCommand = db.GetStoredProcCommand(sqlCommand))
            {
                db.AddInParameter(dbCommand, "Application", DbType.String, escalation.Application);
                db.AddInParameter(dbCommand, "EM", DbType.String, escalation.EM);
                db.AddInParameter(dbCommand, "EscalationActions", DbType.String, escalation.EscalationActions);
                db.AddInParameter(dbCommand, "ProblemStatement", DbType.String, escalation.ProblemStatement);
                db.AddInParameter(dbCommand, "status", DbType.String, escalation.status);
                db.AddInParameter(dbCommand, "UpdatedBy", DbType.String, escalation.UpdatedBy);
                db.AddInParameter(dbCommand, "UpdatedTime", DbType.DateTime, escalation.UpdatedTime);
                db.AddInParameter(dbCommand, "Impact", DbType.String, escalation.Impact);
    
                db.ExecuteNonQuery(dbCommand);
                return true;
            }
        }
        catch (SqlException ex)
        {
            //Log the SQL specific errors
            for (int i = 0; i < ex.Errors.Count; i++)
            {
                StringBuilder errorMessages = new StringBuilder();
    
                errorMessages.Append("Index #" + i + "\n" +
                    "Message: " + ex.Errors[i].Message + "\n" +
                    "LineNumber: " + ex.Errors[i].LineNumber + "\n" +
                    "Source: " + ex.Errors[i].Source + "\n" +
                    "Procedure: " + ex.Errors[i].Procedure + "\n");
            }
    
            MyLogger.FileLogger.ErrorFormat("{0} - {1}", sqlCommand, ex.Message);
            return false;
        }
    }
    
  • Model -

    public partial class Escalation
    {
        public int ID { get; set; }
        public string Application { get; set; }
        public string EM { get; set; }
        public string status { get; set; }
        public string ProblemStatement { get; set; }
        public string Impact { get; set; }
        public string EscalationActions { get; set; }
        public System.DateTime UpdatedTime { get; set; }
        public string UpdatedBy { get; set; }
    }
    
  • Html element of Application list -

                            <div class="form-adjacent">
                                <label class="col-md-3 control-label" for="App">App</label>
                                <div class="col-md-8">
                                    <select id="App" name="Application" onchange="" class="form-control">
                                        <option value="SAP">SAP</option>
                                        <option value="EME">EME</option>
                                    </select>
                                </div>
                            </div>
    

Solution

  • Usually, database parameters must start with a @. So if you change your code for this:

    db.AddInParameter(dbCommand, "@Application", DbType.String, escalation.Application);
    db.AddInParameter(dbCommand, "@EM", DbType.String, escalation.EM);
    db.AddInParameter(dbCommand, "@EscalationActions", DbType.String, escalation.EscalationActions);
    db.AddInParameter(dbCommand, "@ProblemStatement", DbType.String, escalation.ProblemStatement);
    db.AddInParameter(dbCommand, "@status", DbType.String, escalation.status);
    db.AddInParameter(dbCommand, "@UpdatedBy", DbType.String, escalation.UpdatedBy);
    db.AddInParameter(dbCommand, "@UpdatedTime", DbType.DateTime, escalation.UpdatedTime);
    db.AddInParameter(dbCommand, "@Impact", DbType.String, escalation.Impact);
    

    It should work.