Wrong date when using SQL Server stored procedure with Dapper - c#

I am using a stored procedure with Dapper to retrieve data from a table. The stored procedure works fine when executed in SQL Server and returns the required information.
But when I use Dapper to run the stored procedure and retrieve a Date, the Date is returned as 01/01/0001.
Here is my stored procedure which works perfectly in SQL Server:
ALTER PROCEDURE [dbo].[spRankings_GetByEventAndGender]
#Event varchar(50),
#Gender varchar(10)
AS
BEGIN
DECLARE #event_factor INT = CASE
WHEN #Event IN ('Javelin', 'Discus', 'Shot Put', 'Hammer', 'Long Jump', 'High Jump', 'Triple Jump', 'Pole Vault')
THEN -1 /* reverse ranking = highest value is best */
ELSE 1 /* normal ranking = lowest value is best */
END;
SELECT
CASE
WHEN a.mark = ABS(b.mark)
THEN CAST(b.rank AS VARCHAR)
ELSE ''
END AS [Rank],
/*
,a.athleteid
,a.teamid
,a.eventid
*/
CASE
WHEN #event_factor = -1
THEN LTRIM(STR(a.mark, 10, 2))
ELSE FORMAT(DATEADD(SECOND, FLOOR(a.mark), 0),case when a.mark<60 then '' else 'm:' end+'ss')
+substring(ltrim((str(cast(a.mark as decimal(12,5))%1,10,2))),2,10)
end as Mark
,a.wind as Wind
,d.eventname as [Event]
,c.firstname+' '+c.lastname as Athlete
--,Convert(varchar(10),c.BirthDate,103) as [Birth Date]
,c.BirthDate as [BirthDate]
,e.teamname as [Team]
,a.venue as Venue
--, Convert(varchar(10),a.PerformanceDate,103) as [Performance Date]
,a.PerformanceDate as [Performance Date]
from dbo.Performances as a
inner join (select a.PersonId
,a.eventid
,min(a.mark*#event_factor) as mark
,rank() over(partition by a.eventid order by min(a.mark*#event_factor)) as [rank]
,avg(a.mark) as avg_mark
from dbo.Performances as a
inner join dbo.Persons as b
on b.PersonId=a.PersonId
inner join dbo.[Events] as c
on c.eventid=a.eventid
inner join dbo.Meets as d
on d.MeetId = a.MeetId
where b.gender=#Gender
and c.eventname=#Event
group by a.PersonId
,a.eventid
) as b
on b.eventid=a.eventid
and b.PersonId=a.PersonId
inner join dbo.Persons as c
on c.PersonId=a.PersonId
inner join dbo.events as d
on d.eventid=a.eventid
inner join dbo.teams as e
on e.teamid=a.teamid
inner join dbo.Meets as m
on m.MeetId = a.MeetId
order by a.eventid
,a.mark*#event_factor
,b.[rank]
/*
,b.avg_mark
,a.athleteid
*/
end
The results in SQL Server:
The method that uses Dapper to get the results:
public List<RankingsModel> GetRankingsByEventAndGender(string eventName, string gender) {
using (IDbConnection connection = new System.Data.SqlClient.SqlConnection(Helper.GetConString("GARanks"))) {
var output = connection.Query<RankingsModel>($"dbo.spRankings_GetByEventAndGender #Event, #Gender", new { Event=eventName, Gender=gender}).ToList();
return output;
}
}
The results in my application:

You are getting the default value in the application. Please find below links which can help:
Date is 01/01/0001 and not the date that is in the database?
DateTime shows the date as 01/01/0001
HTH
Thanks.

You can probably break the problem down into pieces. First make sure that Dapper is returning the right result set. You could
connection.Query<Object>
or
connection.Query<dynamic>
and inspect the result to see if it has the right DateTime value in it, in debug mode. If it does, then the problem isn't dapper retrieving the results from the database, it's that it can't map it to your POCO/class.
If the result dataset doesn't have the right value of Performance Date, then the problem is with the sql generation that Dapper is doing, and you could look into renaming the column to be without spaces, or pursue that avenue.
Good luck!

You probably have a property named 'PerformanceDate' in RankingsModel class which does not get mapped with 'Performance Date' returned from DB call.
Kindly update the SQL to return 'PerformanceDate' or make same name for both.
The other option is using Column Mapping

Related

'syntax error at end of input' - while trying to launch stored procedure from DB

I am writing in .net core and trying to launch stored procedure from database(Postgresql).
I am getting an error saying there is an "syntax error at end of input".
Connection string is right(found out during debugging). The problem is with .FromSql() method syntax.
here is the code:
List<ActivePackageForOpenBillingPeriod> activeUserPackagesForOpenBillingPeriod =null;
using(var conn = new NpgsqlConnection("Host=localhost;Port=xxx;Database=postgres;Username=xxx;Password=xxx;TrustServerCertificate=xxx;ApplicationName=xxx;"))
{
var s = "create function \"GetActiveUserPackagesForOpenBillingPeriod({0})\"";
activeUserPackagesForOpenBillingPeriod = context.ActivePackageForOpenBillingPeriods.FromSql(s,DateTime.Now).ToList();
}
This is the stored procedure I am trying to call
create function "GetActiveUserPackagesForOpenBillingPeriod"(date
timestamp without time zone) returns TABLE("Amount" numeric, "Package"
character varying, "User" text,"Account" int, "AcceptanceActID" int,
"HasChangedPackage" boolean)
language plpgsql
as
$$
DECLARE
BEGIN
RETURN QUERY
SELECT
a."ID" AS "AccountId",
u."ID" AS "UserId",
up."PackageID",
up."TotalAmount",
CASE
WHEN count(DISTINCT tl."PackageID") IN (0,1) THEN false
ELSE true
END AS "HasChangedPackage"
FROM public."Accounts" as a
LEFT JOIN billing."TransactionHeaders" AS th ON th."AccountID" = a."ID"
INNER JOIN security."Users" AS u ON u."AccountID"= a."ID"
INNER JOIN billing."UserPackages" AS up ON up."UserID"=u."ID" AND COALESCE(th."Date", date) BETWEEN up."StartDate" AND COALESCE(up."EndDate", date)
LEFT JOIN billing."TransactionLines" AS tl ON th."Id" = tl."TransactionHeaderID"
WHERE th."AcceptanceActID" IS NULL AND a."StatusID"!=4 AND a."StatusID"!=3 AND up."StatusID"=1
GROUP BY a."ID", u."ID", up."PackageID", up."TotalAmount";
END;
$$;
The query you are running is wrong. It is a mix between the function creation statement and a call to get its result.
Also only the function name should be doublequoted, not the parameter
Change the query to
var s = "SELECT * FROM \"GetActiveUserPackagesForOpenBillingPeriod\"({0})";
It looks like you are missing a RETURN; at the end of your function. It should be on the line above the END;

EF6 fails to import stored procedure

This is a simplified version of a stored procedure
ALTER PROCEDURE [dbo].[StoredProc1]
(
#PageIndex INT = 1,
#RecordCount INT = 20,
#Gender NVARCHAR(10) = NULL
)
AS
BEGIN
SET NOCOUNT ON ;
WITH tmp1 AS
(
SELECT u.UserId, MIN(cl.ResultField) AS BestResult
FROM [Users] u
INNER JOIN Table1 tbl1 ON tbl1.UserId = u.UserId
WHERE (#Gender IS NULL OR u.Gender = #Gender)
GROUP BY u.UserID
ORDER BY BestResult
OFFSET #PageIndex * #RecordCount ROWS
FETCH NEXT #RecordCount ROWS ONLY
)
SELECT t.UserId, t.BestResult, AVG(cl.ResultField) AS Average
INTO #TmpAverage
FROM tmp1 t
INNER JOIN Table1 tbl1 ON tbl1.UserId = t.UserId
GROUP BY t.UserID, t.BestResult
ORDER BY Average
SELECT u.UserId, u.Name, u.Gender, t.BestResult, t.Average
FROM #tmpAverage t
INNER JOIN Users u on u.UserId = t.UserId
DROP TABLE #TmpAverage
END
When I use EF6 to load the stored procedure, and then go to the "Edit Function Import" dialog, no columns are displayed there. Even after I ask to Retrieve the Columns, I get the message that the SP does not return columns. When I execute the SP from SMMS, I get the expected [UserId, Name, Gender, BestResult, Average] list of records.
Any idea how can I tweak the stored procedure or EF6 to make it work?
Thanks in advance
Thanks to the comments above, the answer is that unfortunately EF6 does not cope well with TMP tables on stored procedures.
One way around is the following:
1) Comment out all temp table calls inside the Stored Procedure.
2) Change the Stored Procedure to return a Fake a result with the same exact column's names that match the expected result
3) Import the Stored Procedure into EF6
4) Double Click on the Function Imports/Stored procedure name
5) On the Edit Function Import dialog, retrieve the Columns and the create a New Complex Type that will match the fake columns
6) CTRL+Save in order to generate all the C# code
7) Re-Update the Stored Procedure by removing the fake result set and un-comment the code with the Temp tables.
That should do the job.
P.S. Special thanks for the helpers that pointed me to the right place !!!
Sometimes it works to add the following statement to the stored proc:
set fmtonly off
But still, dont leave this statement in - only use it while generating the result set

Entity Framework doesn't support stored procedures which build result sets from Dynamic queries or Temporary tables

I've written the following stored proc, which works fine. What I want to do with it though is use it an entity data model. However using it in the entity data model maps to a return type of integer, and a value of zero.
How do I get the SP to return the actual data instead of an integer using the DataContext ?
IF EXISTS (SELECT * FROM SYS.OBJECTS WHERE TYPE = 'P' AND NAME = 'myProc') DROP PROCEDURE myProc;
GO
CREATE PROCEDURE [dbo].myProc
#START DateTime, #STOP DateTime
AS
BEGIN TRY
CREATE TABLE #Temp (download_Pk int);
INSERT INTO #Temp
SELECT download_pk FROM t1
UNION ALL
SELECT download_pk FROM t2;
WITH
x as
(
SELECT ID as Caps_Pk,
rootID as [Caps_RootId],
Case400Series as [Case],
SUBSTRING(c1, CHARINDEX('_', c1,1)+1, LEN(c1)) as [Customer],
run as Run,
SUBSTRING(c2, 1, CHARINDEX('_', c2, 1) -1) as [Sample],
SUBSTRING(c2, CHARINDEX('_', c2,1)+1, len(c2)) as [Amplification],
projectTitle,
DateAdded as [UploadTime],
UserId as [User]
FROM t3
WHERE DateAdded >= #START AND DateAdded <= #STOP AND
[User] in (SELECT name FROM ViewUsers WHERE Site = 'abc' AND Role = 'def')
)
SELECT *
FROM x
WHERE Caps_Pk NOT IN (Select download_Pk from #Temp)
DROP TABLE #Temp;
END TRY
BEGIN CATCH
DROP TABLE #Temp;
END CATCH
GO
Thanks in Advance.
Have you looked at this Code First Stored Procudure, this works great for me, it also has a NuGet Pckage that you can install.
found the answer here: EF4 - The selected stored procedure returns no columns
"EF doesn't support importing stored procedures which build result set from:
Dynamic queries
Temporary tables
The reason is that to import the procedure EF must execute it."

Last record of orders for specific customer - SQL

i am trying to show the last order for the a specific customer on a grid view , what i did is showing all orders for the customer but i need the last order
here is my SQL code
SELECT orders.order_id, orders.order_date,
orders.payment_type, orders.cardnumber, packages.Package_name,
orders.package_id, packages.package_price
FROM orders INNER JOIN packages ON orders.package_id = packages.Package_ID
WHERE (orders.username = #username )
#username get its value from a cookie , now how can i choose the last order only for a cookie value " Tony " for example ?
To generalize (and fix a little bit) Mitch's answer, you need to use SELECT clause embellished with TOP(#N) and ORDER BY ... DESC. Note that I use TOP(#N), not TOP N, which means you can pass it as an argument to the stored procedure and return, say, not 1 but N last orders:
CREATE STORED PROCEDURE ...
#N int
...
SELECT TOP(#N) ...
ORDER BY ... DESC
SELECT top 1
orders.order_id,
orders.order_date,
orders.payment_type,
orders.cardnumber,
packages.Package_name,
orders.package_id,
packages.package_price
FROM orders
INNER JOIN packages ON orders.package_id = packages.Package_ID
WHERE (orders.username = #username )
ORDER BY orders.order_date DESC
In fact assuming orders.order_id is an Identity column:
SELECT top 1
orders.order_id,
orders.order_date,
orders.payment_type,
orders.cardnumber,
packages.Package_name,
orders.package_id,
packages.package_price
FROM orders
INNER JOIN packages ON orders.package_id = packages.Package_ID
WHERE (orders.username = #username )
ORDER BY orders.order_id DESC

Need help with some stored procedure

The stored procedure:
ALTER PROC [Admin].[sp_Ques]
(
#QuesID bigint
)
AS
BEGIN
IF #QuesID = 0
SET #QuesID =NULL
SELECT FQ.QuesID, FQ.Ques,QuesAns
FROM Admin.Ques FQ
WHERE FQ.QuesID = Coalesce(#QuesID,QuesID)
SELECT Language FROM Admin.Language WHERE LanguageID=FQ.LanguageID
END
In the second Select statement:
SELECT Language FROM Admin.Language WHERE LanguageID=FQ.LanguageID
In this statement, I want the value of "FQ.LanguageID" from 1st select statement, so I wrote this:-
LanguageID=FQ.LanguageID
Apparently didn't work. It says "The multi-part identifier "FQ.LanguageID" could not be bound."
Do I need to pass this LanguageID to the stored procedure as a parameter and then use it as:-
SELECT Language FROM Admin.Language WHERE LanguageID=#LanguageID
How can I make this LanguageID=FQ.LanguageID work if I don't want to pass LanguageID as the second argument to the stored procedure? Is there a way?
Perhaps create a local variable to hold the LanguageID that's being retrieved. Assign a value to it during the previous SELECT. The addition of TOP 1 simply ensures that if/when you ever have multiple matches in the first query (indeed you will when #Ques is zero or null!), only one value is returned in that query, thereby allowing a single value into your variable.
DECLARE #Lang int --whatever datatype your QuesID is.
SELECT TOP 1
FQ.QuesID, FQ.Ques,QuesAns as QuesAns,
FQ.QuesAns[Answers], FQT.QuesType ,
FQ.QuesTypeID, FQ.QuesParentID, FQ.Active, FQ.AdminLanguageID
,#Lang = FQ.AdminLanguageID
FROM Admin.Ques FQ
LEFT OUTER JOIN Admin.QuesTypes FQT ON FQT.QuesTypeID=FQ.QuesTypeID
WHERE FQ.QuesID = Coalesce(#QuesID,QuesID)
SELECT TelerikLanguage FROM Admin.Language
WHERE AdminLanguageID=#Lang
The scope of FQ is limited to the first select statement.
Your options include:
Passing AdminLanguageID as a parameter as you have suggested
Retrieving AdminLanguageID in a prior statement (select #AdminLanguageID = AdminLanguageID from...)
Joining Admin.Language with Admin.Ques
Using a subquery (select ... from Admin.Language where AdminLanguageID in (select AdminLanguageID from Admin.Ques where ...)
Why not just join them into 1 select?
ALTER PROC [Admin].[sp_Ques]
(
#QuesID bigint
)
AS
BEGIN
IF #QuesID = 0
SET #QuesID =NULL
SELECT FQ.QuesID, FQ.Ques,QuesAns as QuesAns,FQ.QuesAns[Answers], FQT.QuesType ,FQ.QuesTypeID, FQ.QuesParentID, FQ.Active,FQ.AdminLanguageID, AL.TelerikLanguage
FROM Admin.Ques FQ
LEFT OUTER JOIN Admin.QuesTypes FQT ON FQT.QuesTypeID=FQ.QuesTypeID
LEFT JOIN Admin.Language AL ON AL.AdminLanguageID=FQ.AdminLanguageID
WHERE FQ.QuesID = QuesID OR #QuesID IS NULL
END

Categories

Resources