I am very confused with the error my SQL server is returning – what would be causing this?
The C# code I am running is :
List<CalendarEvent> events = new List<CalendarEvent>();
var db = Database.Open("myDatabase");
string username = HttpContext.Current.Request.Cookies.Get("username").Value;
var listOfGroups = db.Query("SELECT GroupID FROM Membership WHERE UserID = (SELECT UserID from Users WHERE Username = @0 )", username);
foreach(var groupID in listOfGroups)
{
var result = db.Query(
@"SELECT e.event_id, e.title, e.description, e.event_start, e.event_end, e.group_id, e.recurring
FROM event e
JOIN Membership m ON m.GroupID = e.group_id
WHERE e.recurring = 0
AND m.GroupID = @0
AND e.event_start >= @1
AND e.event_end <= @2
UNION ALL
SELECT e.event_id, e.title, e.description, DATEADD(week, w.weeks, e.event_start), DATEADD(week, w.weeks, e.event_end), e.group_id, e.recurring
FROM event e
JOIN Membership m ON m.GroupID = e.group_id
CROSS JOIN
( SELECT row_number() OVER (ORDER BY Object_ID) AS weeks
FROM SYS.OBJECTS
) AS w
WHERE e.recurring = 1
AND m.GroupID = $3
AND e.event_start >= @4
AND e.event_end <= @5", groupID, start, end, groupID, start, end
);
foreach(var record in result)
{
CalendarEvent cevent = new CalendarEvent();
cevent.id = record.event_id;
cevent.title = record.title;
cevent.description = record.description;
cevent.start = record.event_start;
cevent.end = record.event_end;
cevent.recurring = record.recurring;
events.Add(cevent);
}
}
return events;
}
And the error:
Exception Details: System.ArgumentException: No mapping exists from
object type WebMatrix.Data.DynamicRecord to a known managed provider
native type.
What could be causing this and how can I fix it ?
What I wish to do is return all events where recurring = 0, and any events where recurring =1 I want to be returned for their week and the 52 weeks thereafter.
Looks like your issue is with your
groupIDinput parameter.Your
listOfGroupsis coming back as a collection of theWebMatrix.Data.DynamicRecordobjects. You seem to want just theintvalue, as you getgroupIDin theforeach(var groupID in listOfGroups)statement.Try instead replacing the last line:
with:
This should pull the value from the first (and only) column of each dynamic query result, that value being the int group ID that you are after.
EDIT: Corrected value call syntax of DynamicRecord query result.