Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: .NET3.0 C# DateTime
Conversion failed when converting date and/or time from character string.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.Data.SqlClient.SqlException: Conversion failed when converting date and/or time from character string.
Posted 4-Jan-12 7:07am
Edited 4-Jan-12 7:10am
v2
Comments
USABebopKid at 4-Jan-12 12:18pm
   
Great error message. You probably have a bug somewhere.
JawadHafiz at 4-Jan-12 12:21pm
   
how to fix it?
USABebopKid at 4-Jan-12 12:26pm
   
I'm not psychic. I can't see your code in my head.
 
But I can say that the string you're trying to convert isn't in the correct format.
SAKryukov at 4-Jan-12 12:27pm
   
Right :-)
--SA
devbtl at 4-Jan-12 12:26pm
   
put some piece of code here.
JawadHafiz at 4-Jan-12 12:34pm
   
SQL Query

SELECT ReferAFriend.*
FROM ReferAFriend where subscribe = @subscribe and @datetype between @SDate and @EDate

 
my codebehind

raf.SDate = Convert.ToDateTime(txtStart.Text).ToString("dd-MM-yyyy");
raf.EDate = Convert.ToDateTime(txtEnd.Text).ToString("dd-MM-yyyy");
s_magus at 4-Jan-12 16:20pm
   
What are you attempting to accomplish with @datetype? What datatype is @datetype?
Pantelis Chatzinikolis at 4-Jan-12 14:18pm
   
First, I would strongly recommend that you switch to DateTime.TryParse. That won't fix your issue but it's a better method to use. Can you post the string you're trying to convert? Furthermore make sure it doesn't equal DBNull.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 4

I would just start eliminating variables to what could be causing the problem:
 
1) Supply constant date times for the SQL parameters (SDate and EDate) (i.e. DateTime.Now.ToString("dd-MM-yyyy")) rather than the text boxes. If that doesn't fix it, continue.
2) Make sure the type of the SDate/EDate parameters match the datatype of the column they are compared to. If they are the same, continue.
3) Make sure the returning call to retrieve the data in .NET doesn't place the data into the wrong data type. If they are the same, you need to examine your system for further areas that can be eliminated for the source of the problem.
 

Happy hunting.
  Permalink  
Comments
Mika Wendelius at 4-Jan-12 13:08pm
   
Good answer, my 5.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

I think change
 
raf.EDate = Convert.ToDateTime(txtEnd.Text).ToString("dd-MM-yyyy");
 
to
 
raf.EDate = Convert.ToDateTime(txtEnd.Text);
  Permalink  
Comments
JawadHafiz at 4-Jan-12 12:44pm
   
still same and it seems error comes from sql query
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

Use DateTime.TryParse - in this case if your value is not DateTime you will not get an error.
Read more about this here[^].
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 5

To add to the solution given by Andrew Rissing, the code looks like you could have a varchar datatype in you table columns for dates. If that is true, I'd suggest using datetime2 and defining the parameter with that data type. This would ensure that you always handle dates correctly in the database. If you don't need/want the time portion, you can also use date.
  Permalink  
Comments
JawadHafiz at 4-Jan-12 13:15pm
   
i have datetime DATATYPE and i need both date & time
Mika Wendelius at 4-Jan-12 13:18pm
   
Ok, then there's no worry :) Did the original problem get solved?
JawadHafiz at 4-Jan-12 13:26pm
   
no, if anyone help
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 6

I am not sure where you are getting the error. Better you debug your code and find out where is the problem.
raf.SDate = Convert.ToDateTime(txtStart.Text).ToString("dd-MM-yyyy"); raf.EDate = Convert.ToDateTime(txtEnd.Text).ToString("dd-MM-yyyy");
 
If your above code doesn't trough any exception, then try the following:
 
SELECT ReferAFriend.* FROM ReferAFriend where subscribe = @subscribe and CONVERT(DATETIME,@datetype,103) between CONVERT(DATETIME,@SDate,103) and CONVERT(DATETIME,@EDate,103)
  Permalink  
Comments
JawadHafiz at 4-Jan-12 13:57pm
   
thanks, but same error
for
SELECT ReferAFriend.* FROM ReferAFriend where subscribe = @subscribe and CONVERT(DATETIME,@datetype,103) between CONVERT(DATETIME,@SDate,103) and CONVERT(DATETIME,@EDate,103)
Monjurul Habib at 4-Jan-12 14:16pm
   
did you debug??
JawadHafiz at 4-Jan-12 14:01pm
   
Thanks,
but same error
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 7

not yet solved, feeling shameful! on silly error, if anyone to help?
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 8

Coulumn name in Table was incorrect
  Permalink  

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

  Print Answers RSS
0 OriginalGriff 7,903
1 Sergey Alexandrovich Kryukov 7,127
2 DamithSL 5,604
3 Manas Bhardwaj 4,986
4 Maciej Los 4,820


Advertise | Privacy | Mobile
Web04 | 2.8.1411023.1 | Last Updated 7 Jan 2012
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