Error clearing Date fields

K

Kevin Jackson

I'm using an Access Database, and VB.Net 2003.

One of my tables has, amongst others, 2 fields that are defined as
Date/Time fields. with a format of Medium Date.

In the VB Solution explorer I added a new form using the Data Form Wizard
over this table. This creates the appropriate Bound fields on the form
linked to the fields in the database.

The problem I've run into is, when I have put dates into these fields and
then add the record to the database, I sometimes have a need to clear those
dates and update the record with no date . but When I do, After pressing
the Update button, the date re-appears in the field. It hasn't been removed
from the database record..

Here is the code thath is responsible for doing the update. This code is all
generated by the form wizard.
I have also tried creating a simple test application in a completely
seperate directory with its own Db, that has one field in the table defined
as a date/time field.

In the Db the properties of the date field are: Format - Medium Date,
Required = No, and Indexed = No. Those are the only propertiies set.

When I say I'm blanking out the field on the VB Form I have tried multiple
things. Setting it to Zero, Setting it to Nothing, I'm not sure how to set
it to Null, maybe that's my problem ?

--------------------------------------------------------------------------
' Update Button

Private Sub btnUpdate_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles btnUpdate.Click

Try
'Attempt to update the datasource.

Me.UpdateDataSet()
Catch eUpdate As System.Exception
'Add your error handling code here.

'Display error message, if any.

System.Windows.Forms.MessageBox.Show(eUpdate.Message)
End Try

Me.objtrial2ds_PositionChanged()
End Sub

----------------------------------------------------------------------------------------------

Public Sub UpdateDataSet()
'Create a new dataset to hold the changes that have been made to the
main dataset.

Dim objDataSetChanges As trial2.trial2ds = New trial2.trial2ds
'Stop any current edits.

Me.BindingContext(objtrial2ds, "Table1").EndCurrentEdit()
'Get the changes that have been made to the main dataset.

objDataSetChanges = CType(objtrial2ds.GetChanges, trial2.trial2ds)
'Check to see if any changes have been made.

If (Not (objDataSetChanges) Is Nothing) Then

Try

'There are changes that need to be made, so attempt to update the
datasource by

'calling the update method and passing the dataset and any
parameters.

Me.UpdateDataSource(objDataSetChanges)
objtrial2ds.Merge(objDataSetChanges)
objtrial2ds.AcceptChanges()
Catch eUpdate As System.Exception
'Add your error handling code here.

Throw eUpdate
End Try

'Add your code to check the returned dataset for any errors that may
have been

'pushed into the row object's error.

End If

End Sub


----------------------------------------------------------------------------------------

Public Sub UpdateDataSet()
'Create a new dataset to hold the changes that have been made to the main
dataset.

Dim objDataSetChanges As trial2.trial2ds = New trial2.trial2ds
'Stop any current edits.

Me.BindingContext(objtrial2ds, "Table1").EndCurrentEdit()
'Get the changes that have been made to the main dataset.

objDataSetChanges = CType(objtrial2ds.GetChanges, trial2.trial2ds)
'Check to see if any changes have been made.

If (Not (objDataSetChanges) Is Nothing) Then

Try

'There are changes that need to be made, so attempt to update the
datasource by

'calling the update method and passing the dataset and any
parameters.

Me.UpdateDataSource(objDataSetChanges)
objtrial2ds.Merge(objDataSetChanges)
objtrial2ds.AcceptChanges()
Catch eUpdate As System.Exception
'Add your error handling code here.

Throw eUpdate
End Try

'Add your code to check the returned dataset for any errors that may
have been

'pushed into the row object's error.

End If

End Sub

---------------------------------------------------------------------------------------


Public Sub UpdateDataSource(ByVal ChangedRows As trial2.trial2ds)

Try

'The data source only needs to be updated if there are changes pending.

If (Not (ChangedRows) Is Nothing) Then

'Open the connection.

Me.OleDbConnection1.Open()
'Attempt to update the data source.

OleDbDataAdapter1.Update(ChangedRows)
End If

Catch updateException As System.Exception
'Add your error handling code here.

Throw updateException
Finally

'Close the connection whether or not the exception was thrown.

Me.OleDbConnection1.Close()
End Try

End Sub

Thanks in advance for any help you can give......

tattoo
 
C

Cowboy \(Gregory A. Beamer\)

I cannot help you completely, but I can aim you in the correct direction.
You will have to explicitly set the values in the DataSet to a DBNull value
when the text fields are clear out. This will null out the values in the
database. The other option, which is kludgy, is to set the date to a very
low value (1/1/1900) and have that represent a null. It is not the best
option, but it is sometimes easier to deal with until you are comfortable
with null values.

--
Gregory A. Beamer
MVP; MCP: +I, SE, SD, DBA

*************************************************
Think outside of the box!
*************************************************
 
C

Cor Ligthert [MVP]

Kevin,

I would not use this wizard, the wizard in 2005 is very fine, this 2003 code
(in VB.Net) looks awful (it looks as a kind of not well done converted C#
code). This wizard is as well not good extendable while the 2005 version is
extendable in everyway and has than as extra the partial class.

However, are you dates typed well confirm your system. As well are they not
blanks, those are not valid as a date, therefore you can use AFAIK a kind of
valid date as 01-01-00

I hope this helps,

Cor
 

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