Click here to Skip to main content
15,887,434 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
this is my procedure
SQL
Create PROC[dbo].[EventLog_Search2]
(
    @min AS DATETIME = NULL,
    @max AS DATETIME = NULL
)
AS

SELECT 
		[idx],
		[eventName],
		[userIdx],
		[description],
		[ip],
		[status],
		creationDate,
		[visible]
	FROM eventLog cc 
	
WHERE  cc.Visible = 1 and
	cc.[creationDate] >= @min AND
	cc.[creationDate] <= @max 


and I am Executing it like

SQL
EXEC	[dbo].[EventLog_Search2]
	@min = '28/12/2013',
	@max = '10/03/2014'


and i am getting this error
"Msg 8114, Level 16, State 5, Procedure EventLog_Search2, Line 0
Error converting data type varchar to datetime."

My value is in "dd/mm/yyyy" format.
i Also tried this

SQL
cc.[creationDate] >= convert(datetime,@min,103) AND
cc.[creationDate] <= convert(datetime,@max,103)

But noting happening same error occurred

C# Code
C#
SqlCommand cmd = new SqlCommand("EventLog_Search2", DALHelper.GetConnection());
cmd.Parameters.AddWithValue("@min", cls.min);
cmd.Parameters.AddWithValue("@max", cls.max);
SqlConnection con = cmd.Connection;
cmd.CommandType = CommandType.StoredProcedure;
con.Open();
using (con)
{
   SqlDataReader dr = cmd.ExecuteReader();
Posted
Updated 11-Mar-14 23:23pm
v3
Comments
Kornfeld Eliyahu Peter 12-Mar-14 4:26am    
Probably you SQL do not know that the format should be dd/mm/yyyy - so interprets 28 as month and fails...
Try setting the format - http://technet.microsoft.com/en-us/library/ms189491.aspx
abdussalam143 12-Mar-14 5:27am    
how could i make this changes in shard hosting.
Kornfeld Eliyahu Peter 12-Mar-14 5:31am    
You must declare cls.min and cls.max as datetime so it will send to SQL as binary...
abdussalam143 12-Mar-14 5:36am    
both values declare as datetime
OPees 12-Mar-14 6:14am    
try this
convert(date,cc.[creationDate],103) >= convert(date,@min,103) AND
convert(date,cc.[creationDate],103) <= convert(date,@max,103)

Because cls.Idx and cls.TermIdx have the wrong Type - they must be DateTime instead of string!
 
Share this answer
 
Convert your datatype to datetime or try using CultureInfo parameter in your Code Behind page to control Datetime format isuues.
 
Share this answer
 
Comments
abdussalam143 12-Mar-14 5:39am    
protected void Application_BeginRequest(object sender, EventArgs e)
{
System.Globalization.CultureInfo newCulture = (System.Globalization.CultureInfo)System.Threading.Thread.CurrentThread.CurrentCulture.Clone();
newCulture.DateTimeFormat.ShortDatePattern = "dd-MMM-yyyy";
newCulture.DateTimeFormat.DateSeparator = "-";
System.Threading.Thread.CurrentThread.CurrentCulture = newCulture;
}

i use this code to set date format
CultureInfo culture= new CultureInfo("fr-FR");

DateTime dt1 = Convert.ToDateTime(txtDate1, culture);
 
Share this answer
 
Try This One:
VB
convert(varchar,cc.[creationDate],112) >= convert(varchar,@min,112) AND
    Convert(varchar,cc.[creationDate],112) <= convert(varchar,@max,112)
 
Share this answer
 
Comments
abdussalam143 12-Mar-14 7:26am    
if i convert it in varchar or nvarchar it reacts like string not date time.
I made these changes in proc and it start working

SQL
Create PROC[dbo].[EventLog_Search2]
(
    @min AS varchar(Max)= NULL,
    @max AS varchar(Max)= NULL
--make them varchar(max)

)
AS
 
SELECT 
		[idx],
		[eventName],
		[userIdx],
		[description],
		[ip],
		[status],
		creationDate,
		[visible]
	FROM eventLog cc 
	
WHERE  cc.Visible = 1 and
	cc.[creationDate] >= convert(datetime,@min,103) AND
	cc.[creationDate] <= convert(datetime,@max,103) 
--(convert them to date time here)
 
Share this answer
 
Comments
Bernhard Hiller 13-Mar-14 3:13am    
Whenever the date time format on the client is different, that will fail.

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900