Bug 20418 - DataColumn throws exception when assigning "true" to a SqlBoolean type column
Summary: DataColumn throws exception when assigning "true" to a SqlBoolean type column
Status: RESOLVED FIXED
Alias: None
Product: Class Libraries
Classification: Mono
Component: System.Data (show other bugs)
Version: master
Hardware: All All
: --- normal
Target Milestone: Untriaged
Assignee: Bugzilla
URL:
Depends on:
Blocks:
 
Reported: 2014-06-06 06:27 UTC by Matthew Leibowitz
Modified: 2017-10-12 12:27 UTC (History)
2 users (show)

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


Attachments

Description Matthew Leibowitz 2014-06-06 06:27:31 UTC
When running these two lines on MS.NET and Mono.NET, there is a major difference: MS converts, Mono throws.

    DataColumn c = new DataColumn("myColumn", typeof(SqlBoolean));
    c.DefaultValue = true;

MS.NET converts true in to SqlBoolean with Value true. Mono throws this exception:

Unhandled Exception:
System.Data.DataException: Default Value of type 'System.Boolean' is not compatible with column type 'System.Data.SqlTypes.SqlBool
ean'
  at System.Data.DataColumn.SetDefaultValue (System.Object value, Boolean forcedTypeCheck) [0x0008a] in C:\cygwin\sources\mono\mcs
\class\System.Data\System.Data\DataColumn.cs:456
  at System.Data.DataColumn.set_DefaultValue (System.Object value) [0x00016] in C:\cygwin\sources\mono\mcs\class\System.Data\Syste
m.Data\DataColumn.cs:439
  at ConsoleApplication2.MainClass.Main (System.String[] args) [0x00000] in <filename unknown>:0
[ERROR] FATAL UNHANDLED EXCEPTION: System.Data.DataException: Default Value of type 'System.Boolean' is not compatible with column
 type 'System.Data.SqlTypes.SqlBoolean'
  at System.Data.DataColumn.SetDefaultValue (System.Object value, Boolean forcedTypeCheck) [0x0008a] in C:\cygwin\sources\mono\mcs
\class\System.Data\System.Data\DataColumn.cs:456
  at System.Data.DataColumn.set_DefaultValue (System.Object value) [0x00016] in C:\cygwin\sources\mono\mcs\class\System.Data\Syste
m.Data\DataColumn.cs:439
  at ConsoleApplication2.MainClass.Main (System.String[] args) [0x00000] in <filename unknown>:0
Comment 1 Matthew Leibowitz 2014-06-06 06:33:37 UTC
The Unit Test that should have picked this up is DataColumnTest.ChangeTypeAfterSettingDefaultValue.

However, this test, as a result of using the ExpectedExceptionAttribute, picked up the FIRST exception as the SECOND exception. Both had the same type, but the first is a bug and the second is a feature.
Comment 2 Matthew Leibowitz 2014-06-06 06:35:19 UTC
As an additional test case for record:

    DataColumn c = new DataColumn("myColumn", typeof(SqlBoolean));
    c.DefaultValue = true;

    Assert.IsInstanceOfType(c.DefaultValue, typeof(SqlBoolean));
    Assert.AreEqual(SqlBoolean.True, c.DefaultValue);
    Assert.IsTrue(((SqlBoolean)c.DefaultValue).Value);
Comment 3 Matthew Leibowitz 2014-06-06 06:45:27 UTC
Another test case is the read/write of XML:

    string xml =
        "<DocumentElement>" + Environment.NewLine +
        "  <myTable>" + Environment.NewLine +
        "    <myColumn>true</myColumn>" + Environment.NewLine +
        "  </myTable>" + Environment.NewLine +
        "</DocumentElement>";

    DataColumn c = new DataColumn ("myColumn", typeof(SqlBoolean));

    DataTable t = new DataTable ("myTable");
    t.Columns.Add (c);

    t.Rows.Clear ();
    // sql type
    DataRow r = t.NewRow ();
    r [0] = SqlBoolean.True;
    t.Rows.Add (r);

    StringWriter writer = new StringWriter ();
    t.WriteXml (writer);

    Assert.AreEqual (
        xml.Replace ("\r\n", "\n"), 
        writer.ToString ().Replace ("\r\n", "\n"),
        "#1 sql type failed to save correctly");

    t.Rows.Clear ();
    // value type
    r = t.NewRow ();
    r [0] = true;
    t.Rows.Add (r);

    writer = new StringWriter ();
    t.WriteXml (writer);

    Assert.AreEqual (
        xml.Replace ("\r\n", "\n"), 
        writer.ToString ().Replace ("\r\n", "\n"),
        "#2 value type failed to save correctly");
Comment 4 Matthew Leibowitz 2015-08-25 00:19:44 UTC
This was fixed when we switched to the MS reference source

Note You need to log in before you can comment on or make changes to this bug.