Bug 43144 - SqlDataReader can't handle fetch of DateTime2(7) columns
Summary: SqlDataReader can't handle fetch of DateTime2(7) columns
Status: RESOLVED FIXED
Alias: None
Product: Class Libraries
Classification: Mono
Component: System.Data (show other bugs)
Version: 4.5.X
Hardware: PC Windows
: --- normal
Target Milestone: Untriaged
Assignee: Bugzilla
URL:
Depends on:
Blocks:
 
Reported: 2016-08-06 15:36 UTC by Tiago Freitas Leal
Modified: 2018-02-22 22:22 UTC (History)
3 users (show)

Tags:
Is this bug a regression?: ---
Last known good build:


Attachments
Vendor class (1.46 KB, patch)
2016-08-06 15:36 UTC, Tiago Freitas Leal
Details


Notice (2018-05-24): bugzilla.xamarin.com is now in read-only mode.

Please join us on Visual Studio Developer Community and in the Xamarin and Mono organizations on GitHub to continue tracking issues. Bugzilla will remain available for reference in read-only mode. We will continue to work on open Bugzilla bugs, copy them to the new locations as needed for follow-up, and add the new items under Related Links.

Our sincere thanks to everyone who has contributed on this bug tracker over the years. Thanks also for your understanding as we make these adjustments and improvements for the future.


Please create a new report on GitHub or Developer Community with your current version information, steps to reproduce, and relevant error messages or log files if you are hitting an issue that looks similar to this resolved bug and you do not yet see a matching new report.

Related Links:
Status:
RESOLVED FIXED

Description Tiago Freitas Leal 2016-08-06 15:36:54 UTC
Created attachment 16928 [details]
Vendor class

I have a table Vendors with columns
Id int,
Name nchar(50),
CreateDate datetime2(7),
ChangeDate datetime2(7),

I have a POCO object with properties
public int Id { get; set; }
public string Name { get; set; }
public DateTime CreateDate { get; set; }
public DateTime ChangeDate { get; set; }

When I fetch a row from the DB, everything runs ok under .NET but on Mono it throws

Unhandled Exception:
System.InvalidCastException: Type is System.String
  at System.Data.SqlClient.SqlDataReader.GetDateTime (Int32 i) <0x3c5c548 + 0x0012b> in <filename unknown>:0
  at TestMono.Console.PocoTest.FetchVendor (Int32 id) <0x3bed190 + 0x001d6> in <filename unknown>:0
  at TestMono.Console.Program.Main () <0x3bb0f58 + 0x000d3> in <filename unknown>:0
[ERROR] FATAL UNHANDLED EXCEPTION: System.InvalidCastException: Type is System.String
  at System.Data.SqlClient.SqlDataReader.GetDateTime (Int32 i) <0x3c5c548 + 0x0012b> in <filename unknown>:0
  at TestMono.Console.PocoTest.FetchVendor (Int32 id) <0x3bed190 + 0x001d6> in <filename unknown>:0
  at TestMono.Console.Program.Main () <0x3bb0f58 + 0x000d3> in <filename unknown>:0

If I change the table schema and use datetime instead of datetime2(7), everything runs ok on Mono.
Comment 1 Tiago Freitas Leal 2016-08-06 15:48:13 UTC
Tested on Mono 4.4.2.11 (Aug 02 2016)
Comment 2 Tiago Freitas Leal 2016-08-06 15:54:28 UTC
Comment on attachment 16928 [details]
Vendor class

using System;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;

namespace TestMono.Console
{
    public class Vendor
    {
        public int Id { get; set; }
        public string Name { get; set; }
        public DateTime CreateDate { get; set; }
        public DateTime ChangeDate { get; set; }

        public Vendor FetchVendor(int id)
        {
            using (var cnx = new SqlConnection(ConfigurationManager.ConnectionStrings["TestMono"].ConnectionString))
            {
                using (var cmd = new SqlCommand(string.Empty, cnx))
                {
                    cmd.CommandType = CommandType.Text;
                    cmd.CommandText =
                        "SELECT Id, Name, CreateDate, ChangeDate FROM Vendors WHERE Id = @Id";
                    cmd.Parameters.AddWithValue("@Id", id).DbType = DbType.Int32;

                    cnx.Open();
                    using (var dr = cmd.ExecuteReader())
                    {
                        if (dr.Read())
                        {
                            Id = dr.GetInt32(0);
                            Name = dr.GetString(1);
                            CreateDate = dr.GetDateTime(2);
                            ChangeDate = dr.GetDateTime(3);
                        }
                    }
                    cnx.Close();
                }
            }

            return this;
        }
    }
}
Comment 3 Marek Safar 2018-02-22 22:22:33 UTC
Mono 5.10 has significantly improved System.Data implementation which should resolve this issue. If you can still reproduce it please reopen the issue.