Assigning a null value in a dataset

J

John

Hi,

I'm writing a database programme with C# using a dataset. I want to set a DateTime for a particular
field of a row to null (nulls are allowed for that column). When I use:

newRow.Date_Pub=null;

I get a compile error: "Cannot convert null to 'System.DateTime' because it is a value type".

How do I set the field to null?
 
S

Sericinus hunter

John said:
Hi,

I'm writing a database programme with C# using a dataset. I want to set a DateTime for a particular
field of a row to null (nulls are allowed for that column). When I use:

newRow.Date_Pub=null;

I get a compile error: "Cannot convert null to 'System.DateTime' because it is a value type".

How do I set the field to null?

There should be newRow.SetDate_PubNull() method generated for the typed dataset.
 
J

John

Thanks, that works.

IIs there a way to set a SetDate variable to a special value that could represent null? This would
make it easier for me since I'm returning a DateTime from a function, and sometimes need to return
something that represents null.

Best wishes,

John


John said:
Hi,

I'm writing a database programme with C# using a dataset. I want to set a DateTime for a
particular
field of a row to null (nulls are allowed for that column). When I use:

newRow.Date_Pub=null;

I get a compile error: "Cannot convert null to 'System.DateTime' because it is a value type".

How do I set the field to null?

There should be newRow.SetDate_PubNull() method generated for the typed dataset.
 
A

Alan Pretre

John said:
IIs there a way to set a SetDate variable to a special value that could
represent null? This would
make it easier for me since I'm returning a DateTime from a function, and
sometimes need to return
something that represents null.


You could use a DateTime as a nullable type:

private System.DateTime? GetDate(bool Condition) {
return Condition ? (System.DateTime.Now as System.DateTime?) :
null;
}


-- Alan
 
S

Sericinus hunter

John said:
Thanks, that works.

IIs there a way to set a SetDate variable to a special value that could represent null? This would
make it easier for me since I'm returning a DateTime from a function, and sometimes need to return
something that represents null.

If you use .Net 2.0 then you can probably use nullable type.
I work with 1.1 and use DateTime.MinValue for this purpose.
 

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