How do I write a "null" into dateTime and decimal columns?

  • Thread starter Thread starter Tom
  • Start date Start date
T

Tom

I am attempting to parse data from a flat file into a DataSet table. Some of
the columns are amount fields (decimal) or dateTime.

The flat file can contain spaces or low-values in cases where there is no
valide money or date. These should be treated as "null".

How do I initialize these columsn to null in this case. (I'm thinking of
something like DBNull)
 
Tom said:
I am attempting to parse data from a flat file into a DataSet table. Some of
the columns are amount fields (decimal) or dateTime.

The flat file can contain spaces or low-values in cases where there is no
valide money or date. These should be treated as "null".

How do I initialize these columsn to null in this case. (I'm thinking of
something like DBNull)

Well, what happens when you just put DBNull.Value in the datatable?
 
Jon Skeet said:
Well, what happens when you just put DBNull.Value in the datatable?

It generates compile errors.

Here is a short example of what I am attempting (twsContractHdr is a table
in a strongly typed DataSet)

public void ConvertFTPtoDataset()
{
[...]

TwsDS.twsContractHdrRow h =
twsds.twsContractHdr.NewtwsContractHdrRow();

h.RedemAmt = DBNull.Value;
h.RedemThru = DBNull.Value;

[...]
}

Compile errors are:
Cannot implicity convert type 'System.DBNull' to 'decimal'
Cannot implicity convert type 'System.DBNull' to 'System.DateTime'
 
Tom said:
It generates compile errors.

Here is a short example of what I am attempting (twsContractHdr is a table
in a strongly typed DataSet)

Ah - I didn't know you were using a strongly typed dataset. That
changes things, I'm afraid - and I don't have any experience with them
myself (mainly having worked on the Compact Framework which doesn't
have them).

Are there any other properties which indicate whether or not the value
is null?
 
Tom said:
Jon Skeet said:
Well, what happens when you just put DBNull.Value in the datatable?

It generates compile errors.

Here is a short example of what I am attempting (twsContractHdr is a table
in a strongly typed DataSet)

public void ConvertFTPtoDataset()
{
[...]

TwsDS.twsContractHdrRow h =
twsds.twsContractHdr.NewtwsContractHdrRow();

h.RedemAmt = DBNull.Value;
h.RedemThru = DBNull.Value;

You work around this by setting

h[TwsDS.twsds.twsContractHdr.RedemAmtColumn] = DBNull.Value;

Or by just not setting it at all.

David
 
Have you tried this

public void ConvertFTPtoDataset(

[...

TwsDS.twsContractHdrRow h
twsds.twsContractHdr.NewtwsContractHdrRow()

h.SetRedemAmtNull()
h.SetRedemThruNull()

[...


----- Tom wrote: ----


Jon Skeet said:
Well, what happens when you just put DBNull.Value in the datatable

It generates compile errors

Here is a short example of what I am attempting (twsContractHdr is a tabl
in a strongly typed DataSet

public void ConvertFTPtoDataset(

[...

TwsDS.twsContractHdrRow h
twsds.twsContractHdr.NewtwsContractHdrRow()

h.RedemAmt = DBNull.Value
h.RedemThru = DBNull.Value

[...


Compile errors are
Cannot implicity convert type 'System.DBNull' to 'decimal
Cannot implicity convert type 'System.DBNull' to 'System.DateTime
 
This issue slowed me down for days. This should work:

1. Go to the XSD for your typed dataset.
2. Select the field in the dataset that needs to accept null values.
3. If you're using VS.NET change "nillable" from "Default" to "True" in the Properties window. If you're working directly in the XML schema, add a 'nillable="true"' attribute to the field element.
4. Save the DataSet and rebuild the project.
5. Go to your client code. You should have a new method on your DataRow:
myDataRow.Set<fieldname>Null();


Hope this helps
 
Back
Top