Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# SQL-Server-2008
i have a datetime string "01-10-2013 09:15 ص" when passing this value to a datetime parameter cause error
Error converting data type nvarchar to datetime.
plz help
Posted 9-Jan-13 22:47pm
rajin kp1.8K
Comments
faisal23 at 10-Jan-13 4:53am
   
Please provide more details ..
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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.
  Permalink  
Comments
rajin kp at 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 at 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 at 10-Jan-13 6:09am
   
thanks.When i use Convert.ToDateTime(stringdate) problem solved.
OriginalGriff at 10-Jan-13 6:13am
   
You're welcome!
Marcus Kramer at 10-Jan-13 9:22am
   
My 5.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

Try Converting the string to DateTime.
string datetime="01-10-2013 09:15 ص";
DateTime newdatetime = Convert.ToDateTime(datetime);
 
If you are still getting the error, Kindly share it.
  Permalink  
v2
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 4

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.
 
string str="01-10-2013 09:15 ص";
 
DateTime dt1=Convert.ToDateTime(str);
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

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.
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 5

You have non-alphanumeric character(ص) in the string due to which you are facing this error.
Try it with any alphanumeric, it will work
  Permalink  

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

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 398
1 CPallini 333
2 George Jonsson 270
3 OriginalGriff 159
4 Prasad Khandekar 135
0 OriginalGriff 4,613
1 CPallini 3,340
2 Sergey Alexandrovich Kryukov 2,771
3 George Jonsson 2,254
4 Gihan Liyanage 2,020


Advertise | Privacy | Mobile
Web03 | 2.8.140905.1 | Last Updated 10 Jan 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100