dataset nulls

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I have a DataSet with a DateTime field. I am using an EventRow r, to update
it with values from a TextBox in a DataGrid. Suppose a DataGrid in edit mode
had a field with value: 7/7/2005. Then the user deletes this value. Now, I
want to update the DataSet (I know how to update the database) with a
statement like this: r.MyDate = null. However, I can't seem to find the
syntax to do this. Thanks!
 
Strong-type your DataSet and the xsd generator will serialize a special method for setting nullable properties:

((DataRow) row).Set[column name]Null()
 
I'm confused about how to get started with this. I have used VS to generate
the DataSet. If I look at the DataSet properties, it has a field that says
"AllowDBNull" with the value "true" (however, it is grayed out). Then my code
attempts to: r.BTime = System.DBNull and the error says "class where variable
expected".

I have tried to modify the XSD file directly, as
http://www.adoguy.com/content.aspx?id=SampleChapter/Chapter5 suggests by
putting: codegen:nullValue="_empty" in XSD file, but then it doesn't validate
(doesn't like the codegen keyword) AND the DataSet disappears from the
component tray.

Also, when I preview the data in the DataSet, it shows the Null values, so
why won't it let me put them in myself. Thanks--Dave G

Dave said:
Strong-type your DataSet and the xsd generator will serialize a special method for setting nullable properties:

((DataRow) row).Set[column name]Null()

--
Dave Sexton
[email protected]
-----------------------------------------------------------------------
drg said:
I have a DataSet with a DateTime field. I am using an EventRow r, to update
it with values from a TextBox in a DataGrid. Suppose a DataGrid in edit mode
had a field with value: 7/7/2005. Then the user deletes this value. Now, I
want to update the DataSet (I know how to update the database) with a
statement like this: r.MyDate = null. However, I can't seem to find the
syntax to do this. Thanks!
 
I have also tried: r.Btime = System.DBNull.Value. The error I get is that it
can't convert it to System.DateTime.
 
drg said:
I have also tried: r.Btime = System.DBNull.Value. The error I get is that it
can't convert it to System.DateTime.

That was the obvious answer. Beyond that I'm afraid I've got no idea. I
never do updates via datasets.
 
If you have generated a strong-typed DataSet (i.e. used the designer to make a class that you can reference) then you can set your
property to null by calling this method:

r.SetBTimeNull();

This will only work if you have a field named "BTime" (or by using codegen to rename the field to "BTime") and the field is
nullable. The easiest way to make a field nullable is to select the field in the .xsd designer, view the properties grid for that
field and set minOccurs to "0" (This means that the field may be sepecified 0 times, which is null).

I see that in a previous post you tried using codegen, so here's an explaination of why that didn't work for you:

"codegen" is an xml namespace that must be imported into your xml file to be used. It provides certain configuration capabilities
of the dataset generator such as changing the names of fields and methods on the class that will be created from your .xsd schema.

I threw together an example DataSet that shows some of the use of codgen (see the inline comments):

<?xml version="1.0" encoding="utf-8"?>
<xs:schema id="Dataset1" targetNamespace="http://tempuri.org/Dataset1.xsd"
elementFormDefault="qualified" attributeFormDefault="qualified"
xmlns="http://tempuri.org/Dataset1.xsd" xmlns:mstns="http://tempuri.org/Dataset1.xsd"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:msdata="urn:schemas-microsoft-com:xml-msdata"
xmlns:codegen="urn:schemas-microsoft-com:xml-codegen"> <!-- THIS LINE IMPORTS THE codgen NAMESPACE -->
<xs:element name="Dataset1" msdata:IsDataSet="true">
<xs:complexType>
<xs:choice maxOccurs="unbounded">
<xs:element name="tbl_strings"
codegen:typedName="StringRow" <!-- This will name your element, in code, as "StringRow" -->
codgen:typedPlural="StringTable"> <!-- This will name your element collection, in code, as "StringTable" -->
<xs:complexType>
<xs:sequence>
<xs:element
codgen:typedName="StringField" <!-- This will name your field, in code, as "StringField" -->
name="vch_string_field" type="xs:string"
minOccurs="0" /> <!-- This will generate a "SetStringFieldNull" method -->
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:choice>
</xs:complexType>
</xs:element>
</xs:schema>

When you compile this into a DataSet, you can do something like the following to see how codegen generated the names I specified
instead of the xml element names:

DataSet1 ds = new DataSet1();

StringTable table = DataSet1.StringTable // "StringTable" instead of "Tables[0]"
table.AddStringRow("test field value"); // "AddStringRow" instead of the default "NewRow()" and code that goes with it

StringRow row = DataSet1.StringTable[0]; // Directly query for the "StringRow" instance at the first index (zero index).

Console.WriteLine(row.StringField); // "StringField" as the name instead of "vch_string_field"
// prints "test field value"

row.SetStringFieldNull(); // What your looking for !!!!!
Console.WriteLine(row.StringField);
// prints nothing

There are a few other options in codgen such as typedChildren and typedPlural which I use alot. They change the relational members
in code such as the parent property name of a child row, and the Get* method of the parent table for retrieving an array of
children.

Hope it helps :)

--
Dave Sexton
[email protected]
-----------------------------------------------------------------------
drg said:
I'm confused about how to get started with this. I have used VS to generate
the DataSet. If I look at the DataSet properties, it has a field that says
"AllowDBNull" with the value "true" (however, it is grayed out). Then my code
attempts to: r.BTime = System.DBNull and the error says "class where variable
expected".

I have tried to modify the XSD file directly, as
http://www.adoguy.com/content.aspx?id=SampleChapter/Chapter5 suggests by
putting: codegen:nullValue="_empty" in XSD file, but then it doesn't validate
(doesn't like the codegen keyword) AND the DataSet disappears from the
component tray.

Also, when I preview the data in the DataSet, it shows the Null values, so
why won't it let me put them in myself. Thanks--Dave G

Dave said:
Strong-type your DataSet and the xsd generator will serialize a special method for setting nullable properties:

((DataRow) row).Set[column name]Null()

--
Dave Sexton
[email protected]
-----------------------------------------------------------------------
drg said:
I have a DataSet with a DateTime field. I am using an EventRow r, to update
it with values from a TextBox in a DataGrid. Suppose a DataGrid in edit mode
had a field with value: 7/7/2005. Then the user deletes this value. Now, I
want to update the DataSet (I know how to update the database) with a
statement like this: r.MyDate = null. However, I can't seem to find the
syntax to do this. Thanks!
 
Dave, This is what I needed (and I had all along but didn't realize!):
r.SetBTimeNull();

Thanks for your XML example, I think I'm getting the idea with strongly
typed DataSets. My problem is now solved, but I wanted to follow up on the
"modifications" you can make to the DataSet via the XSD file. In VS, I opened
the XML and added the codegen namespace and a "typedName". The XML would
validate, but I don't believe it ever "regenerated" the DataSet.cs file. In
other words, the "typed name" (r.BegTime) is still not available in my code,
just the default (r.BTime). Question, do you have to run the command line
program (forget the name) when you modify the XML with a codegen? Seems like
VS would do this for you, but I can't find where. Thanks!--Dave
 
VS will run it for u.

I must appologize, but I gave you the wrong xmlns -- sorry ;)

It should be:

xmlns:codegen="urn:schemas-microsoft-com:xml-msprop"

(msprop, not codegen)

[...]
<xs:element codegen:typedName="BegTime"
 

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

Back
Top