Click here to Skip to main content
13,004,823 members (64,476 online)
Rate this:
Please Sign up or sign in to vote.
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
Updated 27-Jul-16 3:44am
faisal23 10-Jan-13 4:53am
Please provide more details ..
Rate this: bad
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.
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!
Marcus Kramer 10-Jan-13 9:22am
My 5.
Member 11012743 27-Jun-15 6:48am
i am running stored procedure from SQL SERVER than also and passing date as input still getting same error "Error converting data type nvarchar to datetime."
OriginalGriff 27-Jun-15 6:53am
Same same solution! :laugh:
Member 11012743 27-Jun-15 6:55am
what same solution?
OriginalGriff 27-Jun-15 7:10am
The one you are commenting on, that I posted here two years ago!
Member 11012743 27-Jun-15 7:13am
as i find problem than only i asked.
OriginalGriff 27-Jun-15 7:24am
The error message you get means that you are passing a string value to the stored procedure and converting it to a DATE or DATETIME value - but SQL can't understand the string value you passed.
The only solution for this is to go to where you call the SP in your application and fix it there, where you have the user locale info etc., by converting, error reporting, and passing only a DateTime value to your SP.

Exactly the same problem as the original OP, and exactly the same solution...
Member 11012743 27-Jun-15 7:29am
i define stored procedure parameter as DateTime and passing DateTimePicker value to it
OriginalGriff 27-Jun-15 7:42am
This is really a separate question - and probably better as one since code in these little text boxes doesn't work too well. :laugh:

So start a new question, and show the App code, the SP code, and the exact error message.
Post a link here and I'll have a look at it.
Member 11012743 27-Jun-15 9:18am
Rate this: bad
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.
Rate this: bad
Please Sign up or sign in to vote.

Solution 4


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);
Rate this: bad
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[ before doing so.
Rate this: bad
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

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

Advertise | Privacy | Mobile
Web02 | 2.8.170627.1 | Last Updated 30 Jul 2016
Copyright © CodeProject, 1999-2017
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