G
Guest
Hi,
I'm using .NET 2003. Is there any way I can initialize a read-only column
in a typed dataset to Null while still utilizing the generated AddXXXXRow()
method?
Let's say I have a simple typed dataset with one table called MyTable that
has three columns, Entry1, Entry2, and Entry3, all of type int. Moreover,
Entry1 is a read-only column:
<?xml version="1.0" encoding="utf-8" ?>
<xs:schema id="MyDataset" targetNamespace="http://tempuri.org/MyDataset.xsd"
elementFormDefault="qualified"
attributeFormDefault="qualified" xmlns="http://tempuri.org/MyDataset.xsd"
xmlns:mstns="http://tempuri.org/MyDataset.xsd"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
<xs:element name="MyDataset" msdata:IsDataSet="true">
<xs:complexType>
<xs:choice maxOccurs="unbounded">
<xs:element name="MyTable">
<xs:complexType>
<xs:sequence>
<xs:element name="Entry1" type="xs:int" minOccurs="0"
msdata:ReadOnly="true" />
<xs:element name="Entry2" type="xs:int" minOccurs="0" />
<xs:element name="Entry3" type="xs:int" minOccurs="0" />
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:choice>
</xs:complexType>
</xs:element>
</xs:schema>
I would like to be able to initialize the read-only column to Null using the
generated AddXXXXRow() method when I add a new row, something like this:
MyDataset ds = new MyDataset();
MyDataset.MyTableRow row = ds.MyTable.AddMyTableRow(DBNull.Value, 456,
789);
Of course, that doesn't compile. So, I added a call to the generated
SetXXXXNull() method:
MyDataset ds = new MyDataset();
MyDataset.MyTableRow row = ds.MyTable.AddMyTableRow(123, 456, 789);
row.SetEntry1Null();
Not surprisingly, the SetEntry1Null() method throws a
System.Data.ReadOnlyException, saying that the column 'Entry1' is read only.
I know that I could temporarily set the ReadOnly flag of the column to false
before setting it to Null, as such:
MyDataset ds = new MyDataset();
MyDataset.MyTableRow row = ds.MyTable.AddMyTableRow(123, 456, 789);
ds.MyTable.Entry1Column.ReadOnly = false;
row.SetEntry1Null();
ds.MyTable.Entry1Column.ReadOnly = true;
But this seems like a hack. Is there a better way of doing this?
Thanks!
I'm using .NET 2003. Is there any way I can initialize a read-only column
in a typed dataset to Null while still utilizing the generated AddXXXXRow()
method?
Let's say I have a simple typed dataset with one table called MyTable that
has three columns, Entry1, Entry2, and Entry3, all of type int. Moreover,
Entry1 is a read-only column:
<?xml version="1.0" encoding="utf-8" ?>
<xs:schema id="MyDataset" targetNamespace="http://tempuri.org/MyDataset.xsd"
elementFormDefault="qualified"
attributeFormDefault="qualified" xmlns="http://tempuri.org/MyDataset.xsd"
xmlns:mstns="http://tempuri.org/MyDataset.xsd"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
<xs:element name="MyDataset" msdata:IsDataSet="true">
<xs:complexType>
<xs:choice maxOccurs="unbounded">
<xs:element name="MyTable">
<xs:complexType>
<xs:sequence>
<xs:element name="Entry1" type="xs:int" minOccurs="0"
msdata:ReadOnly="true" />
<xs:element name="Entry2" type="xs:int" minOccurs="0" />
<xs:element name="Entry3" type="xs:int" minOccurs="0" />
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:choice>
</xs:complexType>
</xs:element>
</xs:schema>
I would like to be able to initialize the read-only column to Null using the
generated AddXXXXRow() method when I add a new row, something like this:
MyDataset ds = new MyDataset();
MyDataset.MyTableRow row = ds.MyTable.AddMyTableRow(DBNull.Value, 456,
789);
Of course, that doesn't compile. So, I added a call to the generated
SetXXXXNull() method:
MyDataset ds = new MyDataset();
MyDataset.MyTableRow row = ds.MyTable.AddMyTableRow(123, 456, 789);
row.SetEntry1Null();
Not surprisingly, the SetEntry1Null() method throws a
System.Data.ReadOnlyException, saying that the column 'Entry1' is read only.
I know that I could temporarily set the ReadOnly flag of the column to false
before setting it to Null, as such:
MyDataset ds = new MyDataset();
MyDataset.MyTableRow row = ds.MyTable.AddMyTableRow(123, 456, 789);
ds.MyTable.Entry1Column.ReadOnly = false;
row.SetEntry1Null();
ds.MyTable.Entry1Column.ReadOnly = true;
But this seems like a hack. Is there a better way of doing this?
Thanks!