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
(unknown member)
Edited 4-Jan-12 7:10am
v2
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  
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  
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  
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  
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)



Advertise | Privacy | Mobile
Web01 | 2.8.150327.1 | Last Updated 7 Jan 2012
Copyright © CodeProject, 1999-2015
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