SqlDecimal.Null and DbNull.Value Problems

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Daniel Zelisko

    #1

    SqlDecimal.Null and DbNull.Value Problems

    Hello
    Could anyone tell me how to send a null decimal value to the sql
    server depending on its value?
    The following code gives me an error:
    System.Data.Sql Types.SqlNullVa lueException: Data is Null. This method
    or property cannot be called on Null values.

    I figured out SqlDecimal.Null is used only with db Money fields. But
    when I changed SqlDecimal.Null to DBNull.Value I got the error:
    CS0173 Type of conditional expression can't be determined because
    there is no implicit conversion between
    'System.Data.Sq lTypes.SqlDecim al' and 'System.DBNull'

    How should I rewrite this code to get it working?

    Any help appreciated.
    Daniel Zelisko
    dzelisko AT go2.pl

    (sql server field ProductPrice is decimal)
    ------------------- CODE STARTS HERE
    ------------------------------------

    //variable declaration
    protected Decimal _productPrice = new System.Decimal( );

    .....
    //(reading data)
    //if _productPrice equals zero it should send a null value
    myCommand.Param eters.Add("@Pro ductPrice", SqlDbType.Decim al).Value =
    (_productPrice != 0 ? new SqlDecimal(_pro ductPrice) :
    SqlDecimal.Null );

    try
    {
    SqlDataAdapter adapter = new SqlDataAdapter( );
    adapter.SelectC ommand = myCommand;
    adapter.Fill(my DataSet);
    }
    catch (Exception es)
    {
    ErrorText = es.ToString();
    }
    .....
Working...