C# .NET - conversion failed when converting datetime from character string error

Asked By ahmed salah on 01-Sep-14 02:42 PM
Hi guy when i run this Stored procedure it give me message error

conversion faild when converting date time from charachter sting

stored procedure as following

Create proc searchData

    @StartDate datetime,
    @EndDate datetime

as
Declare @SQLQuery as nvarchar(2000)
SET @SQLQuery ='SELECT * from Employee Where (1=1)'
  If (@StartDate is not NULL) AND (@EndDate is not NULL)
   Set @SQLQuery = @SQLQuery + ' And (JoinDate
   BETWEEN '+ @StartDate +' AND '+@EndDate+')'
Exec (@SQLQuery)

JoinDate found in table Employee as datetime

but when i make stored procedure as following

it work in formate dd/mm/yyyy and this is what i need 

ALTER  proc [dbo].[searchData]

@StartDate datetime 

@EndDate  datetime, 

as


select * from dbo.Employee e where JoinDate between @StartDate and @EndDate
Now what is the proplem in first stored procedure
Please help me if possible

Robbe Morris replied to ahmed salah on 01-Sep-14 10:02 AM
More with this dynamic sql crap.

You are passing in datetime variables and expecting them to concatenate like strings.  You'll have to convert/cast them to varchar prior to concatenating them.