?
Solved

check for blank datetime in textbox

Posted on 2009-04-23
8
Medium Priority
?
785 Views
Last Modified: 2012-05-06
I am am using subsonic in an asp.net app with sql server 2005.  

I have a customer table with a nullable column called DOB which is of type smalldatetime.
In the webform the field is optional so what is the best way to handle this?  
Currently I am using ...

customer.DOB = DateTime.Parse(birthdate.Text);

But what is the best approach using nullable types to enter null in the dob field
0
Comment
Question by:mugsey
8 Comments
 
LVL 11

Expert Comment

by:vs1784
ID: 24213691
0
 

Author Comment

by:mugsey
ID: 24213706
Is there a better example please?
0
 
LVL 15

Assisted Solution

by:spprivate
spprivate earned 400 total points
ID: 24213731
You can use Nullable in C#2.0 for this.
Here is a good article

http://davidhayden.com/blog/dave/archive/2005/05/23/1047.aspx
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.

 

Author Comment

by:mugsey
ID: 24213745
OK I am using subsonic and the field is already nullable.  So from my webform code

customer.DOB = DateTime.Parse(birthdate.Text);

How could I handle it there?
0
 
LVL 11

Expert Comment

by:vs1784
ID: 24213762
System.Data.SqlTypes.SqlDateTime getDate;
getDate = SqlDateTime.Null;
customer.DOB = getDate;

Hope it helps.
0
 

Author Comment

by:mugsey
ID: 24213850
Thanks but I am getting invalid cast exceptions
0
 
LVL 11

Assisted Solution

by:vs1784
vs1784 earned 400 total points
ID: 24213894
customer.DOB is DateTime type?

If yes check this thread it will help you

http://forums.asp.net/t/383856.aspx?PageIndex=1
0
 
LVL 11

Accepted Solution

by:
Kelvin McDaniel earned 1200 total points
ID: 24214015
I assume that your table object was setup to allowing NULLs in the DOB column. If that is the case the code I would typically use is...
    // If you're using v3.5 of the .NET Framework...
    var dobCheck = default(DateTime);
    DateTime.TryParse(birthdate.Text, out dobCheck);
 
    if (dobCheck != default(DateTime))
    {
        customer.DOB = dobCheck;
    }
 
    // If you're using v2.0 of the .NET Framework...
    DateTime dobCheck = new DateTime();
    DateTime.TryParse(birthdate.Text, out dobCheck);
 
    if (dobCheck != new DateTime())
    {
        customer.DOB = dobCheck;
    }

Open in new window

0

Featured Post

Prep for the ITIL® Foundation Certification Exam

December’s Course of the Month is now available! Enroll to learn ITIL® Foundation best practices for delivering IT services effectively and efficiently.

Question has a verified solution.

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

ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
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.…
When cloud platforms entered the scene, users and companies jumped on board to take advantage of the many benefits, like the ability to work and connect with company information from various locations. What many didn't foresee was the increased risk…
Suggested Courses
Course of the Month16 days, 13 hours left to enroll

864 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