ASP.Net Assign null value to decimal datatype

Posted on 2007-08-03
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

    Author Comment

    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
    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

    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

    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

    Gary Davis

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Training Course: Java/J2EE and SOA

    This course will cover both core and advanced Java concepts like Database connectivity, Threads, Exception Handling, Collections, JSP, Servlets, XMLHandling, and more. You'll also learn various Java frameworks like Hibernate and Spring.

    INTRODUCTION: While tying your database objects into builds and your enterprise source control system takes a third-party product (like Visual Studio Database Edition or Red-Gate's SQL Source Control), you can achieve some protection using a sing…
    For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK ( for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
    This video is in connection to the article "The case of a missing mobile phone (". It will help one to understand clearly the steps to track a lost android phone.
    Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

    737 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

    Need Help in Real-Time?

    Connect with top rated Experts

    23 Experts available now in Live!

    Get 1:1 Help Now