Problem Adding new record to a database

G

Guest

I'm using VB .Net 2005 with Access 2003 for the database. The database has
two tables: tblContact (ContactNum (k), lastname, firstname, phone, email,
compNum) and tblCompany (CompNun (k), CompName, address, city, state, zip,
compNum)

There are a unumber of contact at each company. I have created a Windows
form where I can select a contact, all of the information about the contact
(tblContact), and the information about the (tblCompany) appears and can be
edited.

If I try to add a new contact, at a new company; the contact information is
saved to tblContact, but the company is not saved. Edit can be made to any
field for either table, but new records cannot be added to the company table.

The code is:

Private Sub TblContactBindingNavigatorSaveItem_Click(ByVal sender _
As System.Object, ByVal e As System.EventArgs) _
Handles TblContactsBindingNavigatorSaveItem.Click
'save, update records
Me.Validate()
Me.TblCompanyBindingSource.EndEdit()
Me.TblCompanyTableAdapter.Update(Me.DsContactData.tblCoAddress)

Me.TblContactBindingSource.EndEdit()
Me.TblContactTableAdapter.Update(Me.DsContactData.tblContact)
End Sub

When I run the program no errors appear, but nothing is saved to the
tblCompany table. Any help will be appreciated.
 
I

Izzy

Honestly I would not use Adapter.Update() to write your update, insert
and delete statements for you. In my opinion that's kind of a lazy way
to write programs.

My solution to this is to use the Oledb.Command object and write insert
and update statements yourself. When the user adds a new company, run
the insert, then let them add new contacts for that company once the
database has returned the Primary Key for the Company.

Izzy
 
G

Guest

Thanks Izzy, I was looking for the lazy way out, but if it's not going to
work I'll have to do things the right way.
 
C

Cor Ligthert [MVP]

Tony,

You say it is not added, is there not any error?
(Because than there are most probably changes done in the row)

Cor
 
G

gene kelley

I'm using VB .Net 2005 with Access 2003 for the database. The database has
two tables: tblContact (ContactNum (k), lastname, firstname, phone, email,
compNum) and tblCompany (CompNun (k), CompName, address, city, state, zip,
compNum)

There are a unumber of contact at each company. I have created a Windows
form where I can select a contact, all of the information about the contact
(tblContact), and the information about the (tblCompany) appears and can be
edited.

If I try to add a new contact, at a new company; the contact information is
saved to tblContact, but the company is not saved. Edit can be made to any
field for either table, but new records cannot be added to the company table.

The code is:

Private Sub TblContactBindingNavigatorSaveItem_Click(ByVal sender _
As System.Object, ByVal e As System.EventArgs) _
Handles TblContactsBindingNavigatorSaveItem.Click
'save, update records
Me.Validate()
Me.TblCompanyBindingSource.EndEdit()
Me.TblCompanyTableAdapter.Update(Me.DsContactData.tblCoAddress)

Me.TblContactBindingSource.EndEdit()
Me.TblContactTableAdapter.Update(Me.DsContactData.tblContact)
End Sub

When I run the program no errors appear, but nothing is saved to the
tblCompany table. Any help will be appreciated.

Build a release. Run the release executable, make some changes and then exit. Rerun
the release executable and see if the changes are shown.

Gene
 
G

Guest

You are correct, there are no error messages. When I open the database the
contact table has the new record added, but the company does not. I have
switched between having the company table updated first then the contact
table, and vise versa.
--
Tony


Cor Ligthert said:
Tony,

You say it is not added, is there not any error?
(Because than there are most probably changes done in the row)

Cor
 
K

kgerritsen

Tony,

Two ideas: First, is there any referential integrity defined in the
Access database on the foreign key CompNum? If so, I'd actually expect
the reverse (no new contact insert until the company was inserted), but
I have experienced Access get the integrity relationship backwards once
before.

When using table adapter update, I've been more successful with
extracting a changed dataset as in the following approach:
Dim dschg As DataSet
dschg = ds.GetChanges()

If Not dschg Is Nothing Then
da.Update(dschg, SqlTable)
End If

Actually, a third idea: make sure there are no nulls or type-illegal
data in the tblCompany rows.

Regards,
Keith
 
C

Cor Ligthert [MVP]

Than you have to check if the rows have really changes before the update.

If dt.haschanges then

or whatever like that.

Cor

Tony A. said:
You are correct, there are no error messages. When I open the database
the
contact table has the new record added, but the company does not. I have
switched between having the company table updated first then the contact
table, and vise versa.
 
C

Cor Ligthert [MVP]

K,

Be aware that in this approach the acceptchanges should be used on the
original table. The getchanges gives a copy. I tell that because it was a
mistake that I made the first time I was using this.

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