DateTime.Now.ToShortDateString() and smalldatetime c#

Posted on 2004-11-20
Last Modified: 2012-05-05
I'm haveing a problem updateing the smalldatetime column in SQL server 2000.
i'm using C#

The command string looks like
string UpdateString= "INSERT INTO RISK_SCR (DateSCR, IMP, PROB) VALUES (" + DateTime.Now.ToShortDateString() + ",3,4)";

The output of the string looks like

All good right??
I'm catching the sqlexception and it pass and it updated the DB ...BUT in the smalldatetime column DateSCR it is defaulting to 1/1/1900, the default if nothing is entered.

kinda of strange that it updates with out a problem but doesn't update the smalldatetime column.
Question by:j_tipps
    LVL 8

    Accepted Solution

    Hi j_tipps


    string UpdateString= "INSERT INTO RISK_SCR (DateSCR, IMP, PROB) VALUES ('" + DateTime.Now.ToShortDateString() + "',3,4)";

    You need to include single quotes around the date otherwise it is evaluating it as an integer of 11 divided by 20 divided by 2004 which returns 0 which is the SQL Date of 1 Jan 1900.

    Better still convert the string to the format "d mmm yyyy" eg. 20 Nov 2004, this way SQL Server will not incorrectly convert the date.

    Cheers Sash

    Author Comment

    haha thanks, how funny

    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    Join & Write a Comment

    Suggested Solutions

    Title # Comments Views Activity
    Problem to setup 18 67
    wordsWithout 49 64
    find a node in VST 2 37
    countX 22 50
    Entering a date in Microsoft Access can be tricky. A typo can cause month and day to be shuffled, entering the day only causes an error, as does entering, say, day 31 in June. This article shows how an inputmask supported by code can help the user a…
    Whether you’re a college noob or a soon-to-be pro, these tips are sure to help you in your journey to becoming a programming ninja and stand out from the crowd.
    An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …
    In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

    734 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

    Need Help in Real-Time?

    Connect with top rated Experts

    18 Experts available now in Live!

    Get 1:1 Help Now