Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 16331
  • Last Modified:

DateTime.Now.ToShortDateString() and smalldatetime c#

I'm haveing a problem updateing the smalldatetime column in SQL server 2000.
i'm using C# asp.net

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
INSERT INTO RISK_SCR (DateSCR, IMP, PROB) VALUES (11/20/2004,3,4)

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.
0
j_tipps
Asked:
j_tipps
1 Solution
 
SashPCommented:
Hi j_tipps

Try

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
0
 
j_tippsAuthor Commented:
haha thanks, how funny
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now