datetime error on insert to db

Discussion in 'ASP .Net' started by Lerp, Jul 2, 2004.

  1. Lerp

    Lerp Guest

    Hi all,

    What do I have to do in order to get my value from my textbox that is
    formatted like 'mm/dd/yyyy' into a datetime database field ?

    Currently getting an error: Syntax error converting datetime from character
    string

    I have 2 validators on this form field as seen below along with a sub that
    is supposed to be called on ServerValidate (which it seems it is misfiring)

    'DATE VALIDATOR SUB
    Sub ValidateDate(Sender As Object, e As ServerValidateEventArgs)
    e.IsValid = isDate(e.Value)
    End Sub

    <!--- FROM FORM --->
    <tr valign="top">
    <td align="left" class="bodycopy">Date Of Birth:</td>
    <td align="left" class="bodycopy">
    <asp:TextBox id="dateofbirth" CssClass="textbox" columns="10"
    MaxLength="10" runat="server" />&nbsp;(mm/dd/yyyy format)
    <asp:CustomValidator ControlToValidate="dateofbirth"
    OnServerValidate="ValidateDate" ErrorMessage="Date is invalid! mm/dd/yyyy
    format." Display="Dynamic" RunAt=server ID="Customvalidator1"/>
    <asp:RequiredFieldValidator id="dobval" ControlToValidate="dateofbirth"
    CssClass="bodyreq" Display="dynamic" Width="100%" ErrorMessage="Please enter
    a date of birth." RUNAT="server"/>
    </td>
    </tr>





    Cheers, Lerp :)
    Lerp, Jul 2, 2004
    #1
    1. Advertising

  2. Lerp

    Peter Blum Guest

    Use the DateTime.ParseExcept() method. See the .net documenation for this
    class and method.

    Some suggestions:
    1. The CompareValidator already handles validating dates. Set the Operator
    property to DataTypeCheck and Type=Date. The format (mm/dd/yyyy) is
    determined by the current thread which has a DateTimeFormatInfo object on
    it. (Thread.CurrentCulture)

    2. There are numerous third party DateTextBoxes available with popup
    calendars and many other features. Some are free. You can find out about
    these controls and the rich selection of other types of controls at the
    Control Gallery of www.asp.net, www.123aspx.com and www.411asp.net. Mine is
    "Peter's Date Package" (http://www.peterblum.com/datecontrols/home.aspx).

    --- Peter Blum
    www.PeterBlum.com
    Email:
    Creator of "Professional Validation And More" at
    http://www.peterblum.com/vam/home.aspx

    "Lerp" <> wrote in message
    news:...
    > Hi all,
    >
    > What do I have to do in order to get my value from my textbox that is
    > formatted like 'mm/dd/yyyy' into a datetime database field ?
    >
    > Currently getting an error: Syntax error converting datetime from

    character
    > string
    >
    > I have 2 validators on this form field as seen below along with a sub that
    > is supposed to be called on ServerValidate (which it seems it is

    misfiring)
    >
    > 'DATE VALIDATOR SUB
    > Sub ValidateDate(Sender As Object, e As ServerValidateEventArgs)
    > e.IsValid = isDate(e.Value)
    > End Sub
    >
    > <!--- FROM FORM --->
    > <tr valign="top">
    > <td align="left" class="bodycopy">Date Of Birth:</td>
    > <td align="left" class="bodycopy">
    > <asp:TextBox id="dateofbirth" CssClass="textbox" columns="10"
    > MaxLength="10" runat="server" />&nbsp;(mm/dd/yyyy format)
    > <asp:CustomValidator ControlToValidate="dateofbirth"
    > OnServerValidate="ValidateDate" ErrorMessage="Date is invalid! mm/dd/yyyy
    > format." Display="Dynamic" RunAt=server ID="Customvalidator1"/>
    > <asp:RequiredFieldValidator id="dobval" ControlToValidate="dateofbirth"
    > CssClass="bodyreq" Display="dynamic" Width="100%" ErrorMessage="Please

    enter
    > a date of birth." RUNAT="server"/>
    > </td>
    > </tr>
    >
    >
    >
    >
    >
    > Cheers, Lerp :)
    >
    >
    Peter Blum, Jul 3, 2004
    #2
    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. Christos TZOTZIOY Georgiou
    Replies:
    3
    Views:
    704
    Christos TZOTZIOY Georgiou
    Sep 13, 2003
  2. Tim Peters
    Replies:
    0
    Views:
    532
    Tim Peters
    Sep 9, 2003
  3. mp
    Replies:
    1
    Views:
    386
    John Machin
    Jul 28, 2006
  4. Martin
    Replies:
    0
    Views:
    333
    Martin
    Dec 27, 2008
  5. Replies:
    2
    Views:
    754
    M.-A. Lemburg
    Jan 6, 2009
Loading...

Share This Page