RP said:
I have a DateTimePicker with format dd-MM-yyyy. While attempting to
insert this date in SQL Server Date column, following exception is
thrown:
The conversion of a char data type to a datetime data type resulted in
an out-of-range datetime value.
Are you passing the parameter in as string or as DateTime (referring to
the other thread you had started on this).
In the other thread you had code that was attempting to parse the string
into a DateTime object. I assumed that you were then passing that into
your SQL call. But you are saying the error is still in around
conversion, which indicates to me that you are passing a string into SQL
server and hoping it can parse that into the correct type.
So my first suggestion would be to change your call to SQL to pass in
the DateTime object rather than a string.
If that is not an option, then the following code should work.
string dateToPass = DateTime.ParseExact(txtDOB.text, "dd-MM-yyyy",
System.Globalization.DateTimeFormatInfo.CurrentInfo).ToString("MM/dd/yyyy");
You could surely also do the conversion using regular expression,
possibly more efficiently, but that just isn't my forte.