Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win


DateTime, decimal type

Posted on 2010-11-27
Medium Priority
Last Modified: 2012-05-10
I'm working on grid Add/Edit/Delete. If user enter nothing for DateType or decimal data type fields, I'm trying to fill null in database.

I used (DateTime)SqlDateTime.Null, but 01/01/1900 is inserted. For decimal, except manually inserted 0, I cannot find a way to insert null value, as it complains null is not a decimal type.

Is there a way to insert null value for datetime and decimal data type?

Question by:minglelinch
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
LVL 23

Accepted Solution

Rajkumar Gs earned 1000 total points
ID: 34222247
In Stored Procedure, initialize parameter to NULL
... @date datetime = NULL

In C# cocde, you need to check whether it is default value for date and decimal. If not only pass the value to stored procedure.

So when the value of datetime variable is 01/01/1900 / decimal variable is 0, no value will be passed to Stored Procedure, and it will be inserted with the default value you set for the parameter inside Stored Procedure (NULL)

Hope it is clear
LVL 12

Expert Comment

ID: 34222256
At the time of sending values to the DB check below condition

For Date Time variable
if (DateOfBirth == DateTime.MinValue.Date)
                        cmd.Parameters.Add("DateOfBirth", SqlDbType.DateTime).Value = System.DBNull.Value;
                        cmd.Parameters.Add("DateOfBirth", SqlDbType.DateTime).Value = DateOfBirth;

do same for decimal also

   if (dbPrice == "0")
                        cmd.Parameters.Add("Price", SqlDbType.Decimal).Value = System.DBNull.Value;
                        cmd.Parameters.Add("Price", SqlDbType.Decimal).Value = dbPrice;

Author Comment

ID: 34227354
Thanks for the helpful comments.

I need to pass the format converted data to a C# function like

public void UpdateFin(int id, string accRec, decimal amt, DateTime payDate,...... )

The data are taken from front entry. If user does not enter any value in decimal or DateTime fields, I want to save null in DB.

I tried to pass the value of  (DateTime)System.DBNull.Value or System.DBNull.Value, but I got the error: CS0030: Cannot convert type 'System.DBNull' to 'System.DateTime'

If I pass  (decimal)System.DBNull.Value, I got the error: Cannot convert type 'System.DBNull' to 'decimal'

If I pass Convert.ToDecimal(System.DBNull.Value), I got error: Object cannot be cast from DBNull to other types.

Please help. Thanks.
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

LVL 12

Assisted Solution

jagssidurala earned 1000 total points
ID: 34227579
You can not send directly System.DBNull.Value in frontend directly to DB, this we can achive in DAL layer(if you have) or where your sqlcommand is present like my previous comments.  

If user does not enter any value in decimal or DateTime fields, set default value to these filelds and check the same cond in DAL or where your sqlcommand is present.

For ex: As per my comments

Default values are

PayDate = DateTime.MinValue,
amt = 0.00;

send these values to UpdateFin.
Thats it.


Author Comment

ID: 34239778
You mean insert DateTime.MinValue and 0..00 for dateTime and decimal type if frontend is empty.

I used "decimal amt  = 0.00M", it works fine.

I used DateTime.MinValue which is '1/1/0001 12:00:00 AM', I got the following error -
The conversion of a char data type to a datetime data type resulted in an out-of-range datetime value.

It complains '1/1/0001' is out of range. If I tested the inserting statement with '1/1/2010', it inserts. DateTime.MinValue is out of range - it should not performs this way.
LVL 12

Expert Comment

ID: 34250072
why char data type come into picture. are you dec;ared paydate as char or are you declared any variable declared as char. check it once.

my method will definatly work.  verify conversion in ur code. otherwise post ur code i will review and tell u.

Author Comment

ID: 34346888
Thank you for all the helps. I have made it work successfully.
Happy Holiday Season !

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

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

Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…
Are you ready to place your question in front of subject-matter experts for more timely responses? With the release of Priority Question, Premium Members, Team Accounts and Qualified Experts can now identify the emergent level of their issue, signal…

636 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