?
Solved

DateTime, decimal type

Posted on 2010-11-27
7
Medium Priority
?
483 Views
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?

Thanks.
0
Comment
Question by:minglelinch
[X]
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
7 Comments
 
LVL 23

Accepted Solution

by:
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
Raj
0
 
LVL 12

Expert Comment

by:jagssidurala
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;
                    }
                    else
                    {
                        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;
                    }
                    else
                    {
                        cmd.Parameters.Add("Price", SqlDbType.Decimal).Value = dbPrice;
                    }
0
 
LVL 1

Author Comment

by:minglelinch
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.
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 12

Assisted Solution

by:jagssidurala
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.




0
 
LVL 1

Author Comment

by:minglelinch
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.
0
 
LVL 12

Expert Comment

by:jagssidurala
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.
0
 
LVL 1

Author Comment

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

Featured Post

New benefit for Premium Members - Upgrade now!

Ready to get started with anonymous questions today? It's easy! Learn more.

Question has a verified solution.

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

Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
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
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …
Suggested Courses

777 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