SqlDecimal.Null and DbNull.Value Problems

  • Thread starter Thread starter Daniel Zelisko
  • Start date Start date
D

Daniel Zelisko

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.SqlTypes.SqlNullValueException: 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.SqlTypes.SqlDecimal' 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.Parameters.Add("@ProductPrice", SqlDbType.Decimal).Value =
(_productPrice != 0 ? new SqlDecimal(_productPrice) :
SqlDecimal.Null);

try
{
SqlDataAdapter adapter = new SqlDataAdapter();
adapter.SelectCommand = myCommand;
adapter.Fill(myDataSet);
}
catch (Exception es)
{
ErrorText = es.ToString();
}
.....
 
Back
Top