Reputation: 4549
In my project i have used EntityFramework 5 and I came up to use an SP in my Context. Like this
public virtual object GetEvaluations(int AgentId, DateTime StartDate, DateTime EndDate, int FormId)
{
DataTable table = new DataTable();
using (DbDataAdapter adapter = new SqlDataAdapter())
{
adapter.SelectCommand = _context.Database.Connection.CreateCommand();
adapter.SelectCommand.Parameters.Clear();
adapter.SelectCommand.CommandType = CommandType.StoredProcedure;
adapter.SelectCommand.CommandText = "GetEvaluations";
SqlParameter aid = new SqlParameter("@AgentId", SqlDbType.Int);
aid.Value = AgentId;
SqlParameter sd = new SqlParameter("@StartDate", StartDate.Date.ToString("dd.MM.yyyy"));
SqlParameter ed = new SqlParameter("@EndDate", EndDate.Date.ToString("MM.dd.yyyy"));
SqlParameter fid = new SqlParameter("@FormId", SqlDbType.Int);
fid.Value = FormId;
adapter.SelectCommand.Parameters.Add(aid);
adapter.SelectCommand.Parameters.Add(sd);
adapter.SelectCommand.Parameters.Add(ed);
adapter.SelectCommand.Parameters.Add(fid);
adapter.Fill(table);
}
return table;
}
Here the DateTime formats different style but just working like this. Even this 2 parameters are looking to the same field in the table.
Please look at datetime formats (they are different) i think they must be same style.
My SP:
USE [EvaluationAssistt]
GO
/****** Object: StoredProcedure [dbo].[GetEvaluations] Script Date: 12.03.2015 11:25:08 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author: Hammit Yildirim
-- Create date: 11.03.2015
-- Description: Defined for Category Point Reports Performance
-- =============================================
ALTER PROCEDURE [dbo].[GetEvaluations]-- 258,'1.11.2014','11.24.2014',8
@AgentId int,
@StartDate Datetime,
@EndDate Datetime,
@FormId int
AS
BEGIN
SET NOCOUNT ON;
select fce.Id from FormsCallsEvaluated fce
left join FormsCalls fc on fce.CallId = fc.CallId
where fc.AgentId=@AgentId and fc.DateStarted > @StartDate and fc.DateStarted < @EndDate and fce.FormId=@FormId
END
What is my problem
I tryed below and returning me empty table
public virtual object GetEvaluations(int AgentId, DateTime StartDate, DateTime EndDate, int FormId)
{
DataTable table = new DataTable();
using (DbDataAdapter adapter = new SqlDataAdapter())
{
adapter.SelectCommand = _context.Database.Connection.CreateCommand();
adapter.SelectCommand.Parameters.Clear();
adapter.SelectCommand.CommandType = CommandType.StoredProcedure;
adapter.SelectCommand.CommandText = "GetEvaluations";
SqlParameter aid = new SqlParameter("@AgentId", SqlDbType.Int);
aid.SqlValue = AgentId;
SqlParameter sd = new SqlParameter("@StartDate", SqlDbType.DateTime);
sd.SqlValue = StartDate.Date;
SqlParameter ed = new SqlParameter("@EndDate", SqlDbType.DateTime);
ed.SqlValue = EndDate.Date;
SqlParameter fid = new SqlParameter("@FormId", SqlDbType.Int);
fid.SqlValue = FormId;
adapter.SelectCommand.Parameters.Add(aid);
adapter.SelectCommand.Parameters.Add(sd);
adapter.SelectCommand.Parameters.Add(ed);
adapter.SelectCommand.Parameters.Add(fid);
adapter.Fill(table);
}
return table;
}
It is working SQL side just like this
USE [EvaluationAssistt]
GO
DECLARE @return_value int
EXEC @return_value = [dbo].[GetEvaluations]
@AgentId = 258,
@StartDate = N'1.11.2014',
@EndDate = N'11.24.2014',
@FormId = 8
SELECT 'Return Value' = @return_value
GO
Upvotes: 1
Views: 383
Reputation: 98810
You defined your @StartDate
and @EndDate
columns as DateTime
but you try to pass them string values.
Just pass the right type and values to your parameter like;
SqlParameter sd = new SqlParameter("@StartDate", StartDate.Date);
SqlParameter ed = new SqlParameter("@EndDate", EndDate.Date);
Upvotes: 3