[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now


ASP.Net Assign null value to decimal datatype

Posted on 2007-08-03
Medium Priority
Last Modified: 2013-11-26
i am trying to call a stored procedure from My C# code. It has three parameters but the third one(elementChange) is null sometimes and has a value at other times. The data type for the elementChange field is decimal. In cases where there are no value to pass i set the variable elementChange= null
but gives me the error message ..cannot convert null to decimal  how do i handle this ?
Any suggestions is welcome..
Question by:saswati_m
  • 2
  • 2

Author Comment

ID: 19628412
cmd.AddInParameter("@seriesChange", DbType.Decimal, System.Convert.DBNull)  this passes a null value for this parameter and it works.
But I am not sure why this does not work
cmd.AddInParameter("@seriesChange", DbType.Decimal, System.DBNull)
LVL 18

Expert Comment

by:Gary Davis
ID: 19629345
In the SP, define a default for the parameter in case it is not supplied. In the code, if null, don't bother passing the parameter (skip the cmd.AddInParameter). That's one way to do it.

Author Comment

ID: 19629846
I have done it that way, ie by specifying a default value for the parameter. But my question is why am I not being able to pass a null value for the decimal data type. Is it becaus e decimal is a value type and null a referece type . May be the convert does the boxing and that is why it works.
LVL 18

Accepted Solution

Gary Davis earned 200 total points
ID: 19631466
A decimal column in SQL can have a null but a decimal variable in C# can not unless you use the new (2.0) "nullable types". Use the question mark to declare a variable that may contain a null.

decimal? amount = 1.50;
amount = null;
if (amount.HasValue) ... /* Test if amount is null or not */

For more info, see http://www.codeguru.com/csharp/.net/net_data/datagrid/article.php/c10393/

Gary Davis

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Data architecture is an important aspect in Software as a Service (SaaS) delivery model. This article is a study on the database of a single-tenant application that could be extended to support multiple tenants. The application is web-based develope…
In this article I will describe the Copy Database Wizard method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…
Despite its rising prevalence in the business world, "the cloud" is still misunderstood. Some companies still believe common misconceptions about lack of security in cloud solutions and many misuses of cloud storage options still occur every day. …

868 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question