Bende
Bende

Reputation: 101

How come this select statement returns 01/01/0001 and null, but only for a few columns?

I would like to read data from an SQL database, export it to XML and upload it to sharepoint. However, when using a SELECT statement via an IDbConnection to first read from the SQL database, I get unexpected returns. Can anyone explain why it returns 01/01/0001 00:00:00 and null for some columns?

Desired Result (as readable on the SQL server)

enter image description here

Undesired Result (as queried via IDbConnection)

[![date name check-in time of latest event latest event current status 2022-09-12   A   01/01/0001 00:00:00 01/01/0001 00:00:00 null        null
2022-09-12  B   01/01/0001 00:00:00 01/01/0001 00:00:00 null        null
2022-09-12  C   01/01/0001 00:00:00 01/01/0001 00:00:00 null        null][2]][2]

    public void SQL2SP()
    {
        using (IDbConnection connection = new System.Data.SqlClient.SqlConnection(Helper.CnnVal("")))
        {
            var presence = connection.Query<Presence>("SELECT * FROM ..."); # this is the statement that returns unexpected results
            Events events = new Events();                
            var newData =
                from element in presence
                group element.Name by new { element.Date, element.Name, element.CheckIn, element.TimeOfLatestEvent, element.LatestEvent, element.Status }
                into g
                select new XElement("DATA",
                    new XAttribute("date", XmlConvert.ToString(g.Key.Date, "yyyy-MM-dd")),
                    new XAttribute("name", g.Key.Name),
                    new XAttribute("checkIn", XmlConvert.ToString(g.Key.CheckIn, "yyyy-MM-dd HH:mm:ss")),
                    new XAttribute("timeOfLatestEvent", XmlConvert.ToString(g.Key.TimeOfLatestEvent, "yyyy-MM-dd  HH:mm:ss")),
                    new XAttribute("latestEvent", g.Key.LatestEvent),
                    new XAttribute("currentStatus", g.Key.Status));

    

    public class Presence
    {
        public DateTime Date { get; set; }
        public string Name { get; set; }
        public DateTime CheckIn { get; set; }
        public DateTime TimeOfLatestEvent { get; set; }
        public string LatestEvent { get; set; }
        public string Status { get; set; }       
    }

Upvotes: 0

Views: 573

Answers (0)

Related Questions