Date variables and nothing/null

Discussion in 'ASP .Net' started by wapsiii, Oct 20, 2005.

  1. wapsiii

    wapsiii Guest

    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
     
    wapsiii, Oct 20, 2005
    #1
    1. Advertising

  2. 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" <> wrote in message
    news:...
    > 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
     
    Kevin Spencer, Oct 20, 2005
    #2
    1. Advertising

  3. 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
    >
     
    =?Utf-8?B?RWx0b24gVw==?=, Oct 20, 2005
    #3
  4. wapsiii

    Andy Fish Guest

    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" <> wrote in message
    news:...
    > 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
     
    Andy Fish, Oct 20, 2005
    #4
  5. wapsiii

    wapsiii Guest

    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"
    <> 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" <> wrote in message
    >news:...
    >> 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

    >
     
    wapsiii, Oct 21, 2005
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?UmV6YQ==?=
    Replies:
    1
    Views:
    2,948
    Raterus
    Feb 27, 2004
  2. Peter Grison

    Date, date date date....

    Peter Grison, May 28, 2004, in forum: Java
    Replies:
    10
    Views:
    3,295
    Michael Borgwardt
    May 30, 2004
  3. Replies:
    5
    Views:
    26,875
    Mike Schilling
    Mar 29, 2006
  4. Torsten Bronger
    Replies:
    10
    Views:
    524
    [david]
    Sep 11, 2007
  5. Devin Jeanpierre
    Replies:
    2
    Views:
    492
    Devin Jeanpierre
    Feb 14, 2012
Loading...

Share This Page