Click here to Skip to main content
15,884,176 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
hi to all,
i'm using web application.when i run the code in local system it should work fine but if i host in server i got error "
String was not recognized as a valid DateTime.
" for datetime error.here my project consists date and time also.whe he login and when he logout.so thing how to resolve it.here i used culture info also but still i got error.can anyone help me for this. here i used "windows server 2008 R2".

Another thing here some of my date fields datatype as nvarchar.is it correct or want to change as datetime
Posted
Updated 17-Sep-15 20:27pm
v2
Comments
Suvendu Shekhar Giri 18-Sep-15 2:31am    
Share the relevant code block here.

Very often the root cause for this kind of situation is that the application does not use parameters. Instead values from client are concatenated directly to the SQL statement. Something like
C#
... MyDateColumn = '" + datetimepicker123.value + "'...

If you have such code on the client side you should modify it to use parameters correctly.

Another scenario is that you use paramters but the value is a string and an implicit conversion is done. Again the best solution would be to use correct type of parameter. If the target column is datetime then the type of the parameter should be datetime and so on

And the last thing is, as you wrote, you have stored date information in a character column. However you try to handle the data as date values. The only feasible solution for this is to convert the character columns to date/time columns. There is no reason to store data in another format if a native data type is present. It just causes unnecessary troubles...
 
Share this answer
 
Comments
DamithSL 18-Sep-15 2:53am    
5wd!
Wendelius 19-Sep-15 4:45am    
Thank you.
Check the server's collation.
Different collations can have different effects on date and time.

Example - http://www.sqlservercentral.com/Forums/Topic835597-146-1.aspx[^].

Some sites suggest using yyyymmdd hh:mm:ss to store dates and times.
 
Share this answer
 
v2
don't use nvarchar datatype for date/datetime columns. use date or datetime data types when you need to store dates in sql server. when you working with date or datetimes you better use sql parameters and set the parameter values using .net datetime object and also you can handover the inserting current datetime to Database itself. for example if you need to insert userid with current datetime, then you only need to insert userid from as parameter from the code, sql server will insert database current datetime for you.
C#
INSERT INTO UserLog(UserId, EventTime)
     VALUES (@userID, GETDATE())
 
Share this answer
 
v2
Hi,

I guess the problem is format of Date & Time in your machine, I request you to please recheck the format and change it to accordingly and recheck it once.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900