Initialize read-only column in typed dataset to NULL?

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!
 
K

Kevin Yu [MSFT]

Hi Whitney,

First of all, I would like to confirm my understanding of your issue. From
your description, I understand that you need to set the default value of a
certain column to DBNull.Value in a typed DataSet. If there is any
misunderstanding, please feel free to let me know.

As far as I know, the AddMyTableRow method is generated by xsd.exe from the
typed DataSet schema. However, this method doesn't offer us an overload to
use the default value of that column. So we have to go another way.

First we set the default value of Entry1Column to DBNull.

ds.MyTable.Entry1Column.DefaultValue = DBNull.Value;

You can use ds.MyTable.NewMyTableRow() to get a new strong typed row.

MyDataset.MyTableRow row = ds.MyTable.NewMyTableRow();

Now new row hasn't been added to the table yet. The columns with default
value are initialized. Then we set values for other columns and use
ds.MyTable.Rows.Add() to add the row.

HTH.

Kevin Yu
=======
"This posting is provided "AS IS" with no warranties, and confers no
rights."
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top