Bugzilla – Bug 316352
XSD creates buggy typed datasets
Last modified: 2007-09-15 21:24:23 UTC
---- Reported by juergen@cis-comsoft.de 2004-11-01 07:48:15 MST ---- Description of Problem: XSD creates strongly typed DataSets that throw exceptions when accessing the column-properties of datarows. Steps to reproduce the problem: step 1. Create Database on PostgreSQL Use this as input for psql: CREATE DATABASE test; \c test CREATE TABLE "simple" ( nr smallint, name character varying(10) ); COPY "simple" (nr, name) FROM stdin; 1 Peter 2 Paul 3 Mary \. --### end of psql-input step 2. //Create test_ds.xsd using System; using System.Data; using System.Data.Common; using Npgsql; class MainClass { public static void Main(string[] args) { string connStr="Server=localhost;User ID=juergen;Database=test"; NpgsqlConnection conn = new Npgsql.NpgsqlConnection(connStr); conn.Open(); DataSet ds = new DataSet("testDataSet"); NpgsqlDataAdapter da = new NpgsqlDataAdapter("SELECT * FROM simple WHERE 1=0;", conn); Console.WriteLine(da.Fill(ds, "simple")); ds.WriteXmlSchema("test_ds.xsd"); conn.Close(); } } ####### output is test_ds.xsd: <?xml version="1.0" standalone="yes"?> <xs:schema xmlns:msdata="urn:schemas-microsoft-com:xml-msdata" id="testDataSet" xmlns:xs="http://www.w3.org/2001/XMLSchema"> <xs:element name="testDataSet" msdata:IsDataSet="true" msdata:Locale="de-DE"> <xs:complexType> <xs:choice maxOccurs="unbounded"> <xs:element name="simple"> <xs:complexType> <xs:sequence> <xs:element minOccurs="0" name="nr" type="xs:short" /> <xs:element minOccurs="0" name="name" type="xs:string" /> </xs:sequence> </xs:complexType> </xs:element> </xs:choice> </xs:complexType> </xs:element> </xs:schema> step 3. Create source for test_ds.c xsd test_ds.xsd /dataset step 4. Use test_ds: using System; using System.Data; using Npgsql; class MainClass { public static void Main(string[] args) { string connStr="Server=localhost;User ID=juergen;Database=test"; NpgsqlConnection conn; Schemas.testDataSet tds = new Schemas.testDataSet(); conn = new NpgsqlConnection(connStr); NpgsqlDataAdapter da = new NpgsqlDataAdapter("SELECT * FROM simple;", (NpgsqlConnection)conn); Console.WriteLine(da.Fill(tds.simple)); Console.WriteLine(tds.simple[0].name); Console.WriteLine(tds.simple[0].nr); conn.Close(); } } Actual Results: Exceptions accessing tds.simple[0].name and tds.simple[0].nr Expected Results: No exceptions ;-) How often does this happen? every time Additional Information: There are 2 places in the get-accessor of the column properties of datarows that throw exceptions. Look at comments public virtual short nr { get { // next line (XSD-created) throws NullReferenceException because // InitializeFields for the table is never called and so the // column-variables aren't set. Calling it after InitializeClass // in the table-constructor makes this work (don't know if that's // the right place to call it...). object ret = this[this.table.nrColumn]; if ((ret == System.DBNull.Value)) { throw new System.Data.StrongTypingException("Cannot get strong typed value since it is DB null.", null); } else { // next line (XSD-created) throws InvalidCastException // the equivalent for strings seems to work return ((short)(ret)); // this works return Convert.ToInt16(ret); } } set { this[this.table.nrColumn] = value; } } ---- Additional Comments From atsushi@ximian.com 2004-11-02 09:40:02 MST ---- I haven't tried pgsql-dependent code, but that typed column problem should be fixed (both in HEAD and 1.0-branch). If other problems still happens, please reopen or create another bug report. Thanks. Unknown bug field "cf_op_sys_details" encountered while moving bug <cf_op_sys_details>Ubuntu 4.10</cf_op_sys_details>