SqlDecimal.Null and DbNull.Value Problems

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();
}
.....
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top