sql - Strange issue with Date.Parse / ParseExact not altering date (ASP.Net) -


i have bit of head scratcher date.parse /parseexact functionality in vb. surmise, have asp.net 4.0 app, on 1 of pages there calendar control user chooses date , time, these fed string (strreqdeadline) takes following european / uk date time format: dd/mm/yyyy hh:mm:ss.fff

so example contents of strreqdeadline be: 29/03/2013 16:30:00.000

i need insert sql datetime column, needs converted uk us/datetime format. i've been attempting date.parse , date.parseexact no success. following should work according research i've done:

strreqdeadline = "29/03/2013 16:30:00.000" dim usdate = date = date.parseexact(strreqdeadline, "dd/mm/yyyy hh:mm:ss.fff",  system.globalization.cultureinfo.invariantculture) 

however, happens @ runtime bizzare, date.parseexact function trims off fractal seconds time (as far can see shouldn't doing because filter specifies .fff), , otherwise leaves entire string unchanged. so, if value of usdate output, appears follows: 29/03/2013 16:30:00

what should contain datetime: 3/29/2013 4:30pm

the strange thing if put watch on usdate , start app, in development environment value shows #3/29/2013 4:30pm#, both in watch list , when hovered on in source window, form of output displays original string, minus fractions of second, , doesn't convert datetime.

from read 'invariantculture' specification should negate locale specific issues output, in case issue tried specifying explicit local culture system.globalization.cultureinfo.createspecificculture("en-gb") (tried fr-fr too), makes no difference. windows regional settings on both client , server set uk if bears relevance.

maybe i'm missing obvious can't see why i'm getting output, date.parseexact doesn't throw exceptions or complain string not being recognised, i'm struggling understand why removes fraction seconds , nothing else, since input spring matches specified mask exactly.

i'd interested hear if else has experienced odd issue , did it!

thanks :)

edit: full code sql section follows:

strreqdeadline = "29/03/2013 16:30:00.000"     dim usdate date = date.parseexact(strreqdeadline, "dd/mm/yyyy hh:mm:ss.fff", system.globalization.cultureinfo.invariantculture)  'sql dim con new data.sqlclient.sqlconnection("data source=xxxxx;initial catalog=yyyyy;integrated security=true") dim cmd new data.sqlclient.sqlcommand()  cmd.connection = con  cmd.commandtext = "insert requests (reqownerid, reqdeadline, reqstatus)" _ & "values ('" & userid & "', '" & usdate & "', '1')"  con.open() dim newreqid = cmd.executescalar()  con.close() 

'

why thin not working? these all same underlying date/time:

29/03/2013 16:30:00.000  29/03/2013 16:30:00  3/29/2013 4:00pm 

you cannot rely on hovering on non-string variable shows determine inner value. seeing evaluation of tostring(). if want string show fractions seconds, need call tostring() , specify format "dd/mm/yyyy hh:mm:ss.fff". default datetime type if not going show fractions seconds when convert string.


if not using parameters (and should be) final sql statement after injecting datetime this:

insert mytablewithdate    (column1    ,column2    ,mydatecol) values     ('a'    ,'b'    ,'20130329 16:30:00.557') 

as mentioned before, date datatype not string. it's object (or rather datetime structure, digress). must call correct tostring() meth0d.

try using withing sql string:

& "values ('" & userid & "', '" & usdate.tostring("yyyymmdd hh:mm:ss.fff") & "', '1')" 

of course, there little point converting string date object convert string again, code should work.


Comments