Thursday, December 3, 2015

SqlParameter does not accept 'null' value

In .NET, if you want to call DbContext.Database.ExecuteSqlCommand and use SqlParameter to pass parameters to the SQL command, and if one of your parameters contains value 'null', you cannot simply pass the 'null' as the value of the parameter. Instead, you need to pass DBNull.Value as a null parameter within SQLCommand.

Also, you cannot use ?: operator to do something like this:

(Assume yourParameter is the variable that contains some value, and yourSqlParameter is the variable you want to pass into SqlCommand as the parameter.

SqlParameter yourSqlParameter = new SqlParameter("@FieldName", SqlDbType.CertainDbType);
yourSqlParameter.Value = (yourParameter == null) ? DBNull.Value : yourParameter;

because yourParameter type is not compatible with DBNull.Value. In the MSDN documentation for the ?: operator states the following:

Either the type of first_expression and second_expression must be the same, or an implicit conversion must exist from one type to the other.


Therefore, we need to do something like:


if (yourParameter == null) 
{
    yourSqlParameter.Value = DBNull.Value;
}
else
{
    yourSqlParameter.Value = yourParameter;
}

Wednesday, December 2, 2015

How to show ellipsis if the button label (text inside the button) is too long

Add the following style to the button:

.btn-long-label {
text-overflow: ellipsis;
overflow: hidden;
white-space: nowrap;
}