DBNull to database date-field

  • Thread starter Finn Stampe Mikkelsen
  • Start date
F

Finn Stampe Mikkelsen

Hi

I have defined a table in my database, with 2 date-fields. I have set a
default value to DBNull. I have integrated a nullable datetimepicker control
to my project and set the apropriate NullValue in my datagridview...

Everything works great, with a table-row added manually to the database,
without setting any value to the second date-field. Both the datagridview
and the datetimepicker displays correctly the DBNull value..

My problem however is, how do i add a new row in my program, setting one
date to an actual date, but leaving the other un-set and thereby leaving it
to the DBNull i have setup in the table-design??

When using the addrow method of the dataset i have tried with the field
represented as 'Nothing' but this does not leave the field set to DBNull in
the DB but to a date of 30. december 1899...

Using DBNull.value does not work, but generates an error... How do i add a
new row to my table, leaving one of my date fields un-set (left to
DBNull)???

/Finn
 
M

Mr. Arnold

Finn Stampe Mikkelsen said:
Hi

I have defined a table in my database, with 2 date-fields. I have set a
default value to DBNull. I have integrated a nullable datetimepicker
control to my project and set the apropriate NullValue in my
datagridview...

Everything works great, with a table-row added manually to the database,
without setting any value to the second date-field. Both the datagridview
and the datetimepicker displays correctly the DBNull value..

My problem however is, how do i add a new row in my program, setting one
date to an actual date, but leaving the other un-set and thereby leaving
it to the DBNull i have setup in the table-design??

When using the addrow method of the dataset i have tried with the field
represented as 'Nothing' but this does not leave the field set to DBNull
in the DB but to a date of 30. december 1899...

Using DBNull.value does not work, but generates an error... How do i add a
new row to my table, leaving one of my date fields un-set (left to
DBNull)???

/Finn

Well, I would suspect that you wouldn't address the field in question and
not put anything in the field, which the database itself would insert the
record with Null data in the record field.

You may have to go to the database itself for the table's field and set the
field to use Null data as the default value when the record is inserted into
the table.
 
F

Finn Stampe Mikkelsen

Mr. Arnold said:
Well, I would suspect that you wouldn't address the field in question and
not put anything in the field, which the database itself would insert the
record with Null data in the record field.

You may have to go to the database itself for the table's field and set
the field to use Null data as the default value when the record is
inserted into the table.
Hi

I have been to the table-design and set the default value to be Null. I
can't seem to find any way to pass the null value to the DB.

The field is a Date field. I can't pass DBNull.value, i have tried VB's
Nothing reference but it passes a value leaving the datefield set to dec.
30., 1899... i can't pass "" and i can't ommit the field in the addrow
method...

So i can't seem to find a way that i can do that...

Here's my code

Kunder.Arbejdsopgaver.AddArbejdsopgaverRow(ArbejdsNummerTextBox.Text,
StolTypeTextBox.Text, _ Kunder._Kunder.Rows(KunderBindingSource.Position) ,
DatoStartDateTimePicker.Value, DatoSlutDateTimePicker.Value)

It's the last field (DatoSlutDateTimePicker.Value) i need to pass to the DB
as a null value, since it depick's a date marking the end of a project. This
date is not set, when creating a project and logically should be set to
null..

I have worked around the DBNull problem with DateTimePicker and created a
control handling this and my column in my DataGridView is setup to show the
NullValue with the correct text..

Now i can't seem to create the project by adding a new row, passing this
DBNull value in the field...

/Finn
 
M

Mr. Arnold

Hi

I have been to the table-design and set the default value to be Null. I
can't seem to find any way to pass the null value to the DB.
The field is a Date field. I can't pass DBNull.value, i have tried VB's
Nothing reference but it passes a value leaving the datefield set to dec.
30., 1899... i can't pass "" and i can't ommit the field in the addrow
method...

You don't pass anything in the field from your program. You don't address
the field period on the table record. You just leave the field alone. And
when the record is inserted into the table, the database will put the NULL
value in the table's field for that record by itself.

So you're saying that you can't determine if that datepicker value is less
than 01/01/1900 and say that's an indication that it's NULL, just omit the
field, don't put that data into the table record's date filed, and insert
the record?
So i can't seem to find a way that i can do that...

Here's my code
It's the last field (DatoSlutDateTimePicker.Value) i need to pass to the
DB as a null value, since it depick's a date marking the end of a project.
This date is not set, when creating a project and logically should be set
to null..

I have worked around the DBNull problem with DateTimePicker and created a
control handling this and my column in my DataGridView is setup to show
the NullValue with the correct text..

Most would just correct the value by putting vbNullstring as the text value
to be shown (blank), if the date is less than 01/01/1900 as that's an
indication that the date value was NULL.

It's an old problem that was even there in VB 6 concerning the display of
data from a date field on a record and it's a NULL value, which is being
shown as 12/31/1899.

work around it
 

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