By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,267 Members | 1,861 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,267 IT Pros & Developers. It's quick & easy.

DBNull to database date-field

P: n/a
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

Jul 11 '07 #1
Share this Question
Share on Google+
3 Replies


P: n/a

"Finn Stampe Mikkelsen" <st****@city.dkwrote in message
news:Os**************@TK2MSFTNGP05.phx.gbl...
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.

Jul 11 '07 #2

P: n/a

"Mr. Arnold" <MR. Ar****@Arnold.comskrev i meddelelsen
news:ex**************@TK2MSFTNGP03.phx.gbl...
>
"Finn Stampe Mikkelsen" <st****@city.dkwrote in message
news:Os**************@TK2MSFTNGP05.phx.gbl...
>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??
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(Arbejds NummerTextBox.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

Jul 11 '07 #3

P: n/a

"Finn Stampe Mikkelsen" <st****@city.dkwrote in message
news:%2***************@TK2MSFTNGP06.phx.gbl...
>
<snipped>
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
<snipped>
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

Jul 11 '07 #4

This discussion thread is closed

Replies have been disabled for this discussion.