Click here to Skip to main content
15,888,251 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
i have a datetime string "01-10-2013 09:15 ص" when passing this value to a datetime parameter cause error
SQL
Error converting data type nvarchar to datetime.

plz help
Posted
Updated 27-Jul-16 3:44am
Comments
faisal23 10-Jan-13 4:53am    
Please provide more details ..

Firstly, don't pass it as a string - pass it as a DateTime via a Parametrized Query and you won't get a problem.

The problem is that SQL expects dates in yyyy-MM-dd format, and the non-English characters confuse it.

Passing dates as strings is not a good idea - convert them to DateTime values as soon as possible to get rid of any local Culture information that the user may have entered.
 
Share this answer
 
Comments
rajin kp 10-Jan-13 5:14am    
DateTime d= DateTime.Parse(MeetingTime,CultureInfo.InvariantCulture);
cmdobj.Parameters.AddWithValue("@MeetingTime",d );
Now new error "The string was not recognized as a valid DateTime. There is a unknown word starting at index 17."
OriginalGriff 10-Jan-13 5:19am    
Yes - the Arabic characters start at index 17.
You can't use InvariantCulture, you have to use the culture for the machine on which the date information was entered, it's the only way to be reasonably sure that the user input format matches what you are trying to translate it with.
rajin kp 10-Jan-13 6:09am    
thanks.When i use Convert.ToDateTime(stringdate) problem solved.
OriginalGriff 10-Jan-13 6:13am    
You're welcome!
fjdiewornncalwe 10-Jan-13 9:22am    
My 5.
Try Converting the string to DateTime.
C#
string datetime="01-10-2013 09:15 ص";
DateTime newdatetime = Convert.ToDateTime(datetime);


If you are still getting the error, Kindly share it.
 
Share this answer
 
v2
Hi,


what you try to do is you directly pass string value with datetime parameter.
That is not a good way whatever your using datatypes in sql you must and should to pass the same values otherwise you face some convertion problem.
Please convert your string value to datetime using convert.

C#
string str="01-10-2013 09:15 ص";

DateTime dt1=Convert.ToDateTime(str);
 
Share this answer
 
It might be that you are trying to concatenate an NVARCHAR with a DATETIME field. You need to use CONVERT http://msdn.microsoft.com/en-us/library/ms187928.aspx[ before doing so.
 
Share this answer
 
You have non-alphanumeric character(ص) in the string due to which you are facing this error.
Try it with any alphanumeric, it will work
 
Share this answer
 

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



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