Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > Date variables and nothing/null

Reply
Thread Tools

Date variables and nothing/null

 
 
wapsiii
Guest
Posts: n/a
 
      10-20-2005
I'm unsure how best to handle dates from my webforms to my database.

Often I have a textbox and a datepicker on a webform. Upon postback
dates are received in string format (like DD-MM-YYYY). Ultimately the
dates need to be stored in a datetime/smalldatetime field in the sql
server. However, first I pass then dates to the db via a business
layer where the date properties are of type datetime.

I've explored several ways to handle this scenario, but am unsure
which of the ways are the "best".

Should I first:
1) enter the posted textbox.text into a string variable,
2) try catch type cast the string variable into a datetime variable
(if the type cast fails should I set the datetime property of the
business layer to null/nothing? / or how should optional dates be
handled).

Morten
 
Reply With Quote
 
 
 
 
Kevin Spencer
Guest
Posts: n/a
 
      10-20-2005
First you need to determine the business rules for your app. Is the DateTime
filed required? Are there any constraints on the range of dates?

Second, you need to use Form validation to enforce those rules. Do not allow
the form to be posted if any of them are broken. Do not allow the form to be
posted if the value in it is not a valid DateTime string. In fact, the best
strategy in an ASP.Net app for getting DateTime data from the user is to use
a Calendar Control. It builds the DateTime value for you, and does not
require the user to type (and therefore mistype) the date.

--
HTH,

Kevin Spencer
Microsoft MVP
..Net Developer
Ambiguity has a certain quality to it.

"wapsiii" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> I'm unsure how best to handle dates from my webforms to my database.
>
> Often I have a textbox and a datepicker on a webform. Upon postback
> dates are received in string format (like DD-MM-YYYY). Ultimately the
> dates need to be stored in a datetime/smalldatetime field in the sql
> server. However, first I pass then dates to the db via a business
> layer where the date properties are of type datetime.
>
> I've explored several ways to handle this scenario, but am unsure
> which of the ways are the "best".
>
> Should I first:
> 1) enter the posted textbox.text into a string variable,
> 2) try catch type cast the string variable into a datetime variable
> (if the type cast fails should I set the datetime property of the
> business layer to null/nothing? / or how should optional dates be
> handled).
>
> Morten



 
Reply With Quote
 
 
 
 
=?Utf-8?B?RWx0b24gVw==?=
Guest
Posts: n/a
 
      10-20-2005
Hi Morten,

I agree Kevin’s suggestion. Calendar control definitely gives you a valid
date value. But sometimes, end-users don’t like use Calendar control to
pickup date. In that case, you can use textbox and RegularExpressionValidator
control. Following is a very simple Date Regular Expression:

([1-9]|1[012])[-/]([1-9]|[12][0-9]|3[01])[-/](19|20)\d\d

Validator control can validate data on client-side.

HTH

Elton Wang

"wapsiii" wrote:

> I'm unsure how best to handle dates from my webforms to my database.
>
> Often I have a textbox and a datepicker on a webform. Upon postback
> dates are received in string format (like DD-MM-YYYY). Ultimately the
> dates need to be stored in a datetime/smalldatetime field in the sql
> server. However, first I pass then dates to the db via a business
> layer where the date properties are of type datetime.
>
> I've explored several ways to handle this scenario, but am unsure
> which of the ways are the "best".
>
> Should I first:
> 1) enter the posted textbox.text into a string variable,
> 2) try catch type cast the string variable into a datetime variable
> (if the type cast fails should I set the datetime property of the
> business layer to null/nothing? / or how should optional dates be
> handled).
>
> Morten
>

 
Reply With Quote
 
Andy Fish
Guest
Posts: n/a
 
      10-20-2005
in addition to the other two suggestions, I use a convention inside the
business layer that a value of DateTime.MinValue in the code corresponds to
NULL on the database. That way you can parse date strings at the earliest
opportunity and safely handle blank dates inside the code.

Andy



"wapsiii" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> I'm unsure how best to handle dates from my webforms to my database.
>
> Often I have a textbox and a datepicker on a webform. Upon postback
> dates are received in string format (like DD-MM-YYYY). Ultimately the
> dates need to be stored in a datetime/smalldatetime field in the sql
> server. However, first I pass then dates to the db via a business
> layer where the date properties are of type datetime.
>
> I've explored several ways to handle this scenario, but am unsure
> which of the ways are the "best".
>
> Should I first:
> 1) enter the posted textbox.text into a string variable,
> 2) try catch type cast the string variable into a datetime variable
> (if the type cast fails should I set the datetime property of the
> business layer to null/nothing? / or how should optional dates be
> handled).
>
> Morten



 
Reply With Quote
 
wapsiii
Guest
Posts: n/a
 
      10-20-2005
Thanks for your input... I was struggling with passing dates through
my business layer. Eventually, I ended up using datetime.minvalue to
handle null/nothing.

On the client side I'm using a datepicker, but users still want to
enter text, compare and regex validators and custom js.


On Thu, 20 Oct 2005 17:36:39 +0100, "Andy Fish"
<(E-Mail Removed)> wrote:

>in addition to the other two suggestions, I use a convention inside the
>business layer that a value of DateTime.MinValue in the code corresponds to
>NULL on the database. That way you can parse date strings at the earliest
>opportunity and safely handle blank dates inside the code.
>
>Andy
>
>
>
>"wapsiii" <(E-Mail Removed)> wrote in message
>news:(E-Mail Removed).. .
>> I'm unsure how best to handle dates from my webforms to my database.
>>
>> Often I have a textbox and a datepicker on a webform. Upon postback
>> dates are received in string format (like DD-MM-YYYY). Ultimately the
>> dates need to be stored in a datetime/smalldatetime field in the sql
>> server. However, first I pass then dates to the db via a business
>> layer where the date properties are of type datetime.
>>
>> I've explored several ways to handle this scenario, but am unsure
>> which of the ways are the "best".
>>
>> Should I first:
>> 1) enter the posted textbox.text into a string variable,
>> 2) try catch type cast the string variable into a datetime variable
>> (if the type cast fails should I set the datetime property of the
>> business layer to null/nothing? / or how should optional dates be
>> handled).
>>
>> Morten

>

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Date, date date date.... Peter Grison Java 10 05-30-2004 01:20 PM
Given a date, how to find the beginning date and ending date of that week Matt ASP General 11 11-08-2003 11:24 PM
Given a date, how to find the beginning date and ending date of that week Matt ASP .Net 1 11-08-2003 09:14 PM
Given a date, how to find the beginning date and ending date of that week Matt C Programming 3 11-08-2003 09:07 PM
Given a date, how to find the beginning date and ending date of that week Matt C++ 2 11-08-2003 08:30 PM



Advertisments