X
xlar54
I am writing a parametrized query, where Im building an UPDATE
statement through concatenation, and then adding the parameters such
as:
UPDATE myTable SET [columna] = @newcolumna, [columnb] = @newcolumnb
WHERE [columna] = @origcolumna
AND [columnb] = @origcolumnb
This works fine, unless one of the original database values are a
NULL. Im creating the parameters as:
SqlParameter sqlOrgParam = new SqlParameter();
sqlOrgParam.SqlDbType = this.GetColumnType(tableName,
rowUpdate.ColumnName); // my own method for type
sqlOrgParam.ParameterName = "@org_" + rowUpdate.ColumnName;
if (rowUpdate.OriginalValue == "") // By now, an empty
string would represent a null
sqlOrgParam.Value = DBNull.Value; //This line executes as it
should
else
sqlOrgParam.Value = rowUpdate.OriginalValue;
sqlCmd.Parameters.Add(sqlOrgParam);
The query is called without failure, its just that it doesn't find the
null in the original record, and as such, doesn't perform any
updates. If I remove the null in the table, everything works fine.
It seems like parameter substitution doesnt like DBNull.Value for some
reason. Any thoughts?
statement through concatenation, and then adding the parameters such
as:
UPDATE myTable SET [columna] = @newcolumna, [columnb] = @newcolumnb
WHERE [columna] = @origcolumna
AND [columnb] = @origcolumnb
This works fine, unless one of the original database values are a
NULL. Im creating the parameters as:
SqlParameter sqlOrgParam = new SqlParameter();
sqlOrgParam.SqlDbType = this.GetColumnType(tableName,
rowUpdate.ColumnName); // my own method for type
sqlOrgParam.ParameterName = "@org_" + rowUpdate.ColumnName;
if (rowUpdate.OriginalValue == "") // By now, an empty
string would represent a null
sqlOrgParam.Value = DBNull.Value; //This line executes as it
should
else
sqlOrgParam.Value = rowUpdate.OriginalValue;
sqlCmd.Parameters.Add(sqlOrgParam);
The query is called without failure, its just that it doesn't find the
null in the original record, and as such, doesn't perform any
updates. If I remove the null in the table, everything works fine.
It seems like parameter substitution doesnt like DBNull.Value for some
reason. Any thoughts?