473,322 Members | 1,778 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

Date variables and nothing/null

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
Nov 19 '05 #1
4 1923
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" <wa*****@otmail.com> wrote in message
news:cg********************************@4ax.com...
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

Nov 19 '05 #2
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

Nov 19 '05 #3
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" <wa*****@otmail.com> wrote in message
news:cg********************************@4ax.com...
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

Nov 19 '05 #4
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"
<aj****@blueyonder.co.uk> 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" <wa*****@otmail.com> wrote in message
news:cg********************************@4ax.com.. .
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

Nov 19 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Reza | last post by:
Hi I have a column in my datagrid that can have values of null at times. I am not assigning any value to it, if it is coming from Database empty. Now, the problem is I guess the datetime variables...
7
by: | last post by:
Source Error: Line 173: sData(rownumber - 1, lcnt) = WhatCol.Value Line 174: End IF Line 175: If (sData(rownumber, lcnt) = sData(rownumber - 1, lcnt)) AND...
4
by: Rethish | last post by:
Hi All, I am developing an application in VB.net. I am using .Net Datetime picker control to manage the date. But I am not able to assign null/Empty value to the control. I found the property...
12
by: Rob Meade | last post by:
Hi all, Ok - I've come from a 1.1 background - and previously I've never had any problem with doing this: Response.Write (Session("MyDate").ToString("dd/MM/yyyy")) So, I might get this for...
8
by: TGEAR | last post by:
I have an ItemSTDPriceHistory table as below and this is a child table of itemlookup table with one to many relationship. if exists (select * from dbo.sysobjects where id = object_id(N'.') and...
2
by: Oenone | last post by:
In our applications, we use the special value of DateTime.MinValue to represent "null dates" throughout all our code. We recently ran into an issue where we wanted an optional date parameter for a...
3
by: Darhl Thomason | last post by:
I'm building a database application that has a number of date fields in it. When I add the date/time picker control to my form and run it, if the field is null in my db, then it shows the current...
2
by: Brad Pears | last post by:
I am working on a vb.net 2005 project using sql server 2000 as the backend . I am having a bit of problems with date variables... Here is the scenario... I have a table that includes a couple...
3
by: janetopps | last post by:
I have a news website, with asp pages, which was on Access, and i upgraded to MySQL, i used Bullzip to transfer the data. It had about 1000 pages, which im now able to pull up on the public side. Im...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.