Visual Basic .NET Forums  

Go Back   Visual Basic .NET Forums > VB.NET > Winforms Data Access

Winforms Data Access VB.NET development for data access and back-end related areas

VB.NET Forums Newsletter Signup:
Email address:


Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 11-27-2008, 7:54 PM
VB.NET Forum Newbie
.NET Framework: .NET 3.5 (VS 2008)
 
Join Date: Oct 2008
Posts: 12
Reputation: 11
penno90 is on a distinguished programming path ahead
Default Parent Child Update problem

hi people

i realise that this is a subject that has been covered a lot here but can't quite seem to nail this problem i'm having....

i have a vb.net 2008 frontend with access 2008 database. Parent 'DGV Clients' and Child 'DGV Orders' on a form. They are linked successfully but my problem lies in making updates.

If i add a new client and then an order for the new client, it appears to save ok, but when i try and reload the app. i get the error message that 'ContraintException was unhandled' on my Orders tableapapter.fill.

i can see what the problem is if i open my access database directly. the new Client is being added to the database and being assigned PK of ClientID, but the Child record is not changing to the same ID and is staying with -1. Hence, when i try to reopen the app i get the problem.

I have set up Relation and Foreign key constraints on the relation to 'cascade'. Changed update code of table adapters to update as follows

Code:
ClientsTableAdapter.Update(Bd1DataSet.Clients)
OrdersTableAdapter.Update(Bd1DataSet.Orders)

as i understand that the database needs to obviously add parent records to the db first. i am sure i am missing something simple here????

any help greatly appreciated.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 11-28-2008, 8:41 PM
cjard's Avatar
VB.NET Forum All-Mighty
.NET Framework: .NET 2.0 (VS 2005)
 
Join Date: Apr 2006
Age: 65
Posts: 6,137
Reputation: 658
cjard has a reputation beyond reputecjard has a reputation beyond reputecjard has a reputation beyond reputecjard has a reputation beyond reputecjard has a reputation beyond reputecjard has a reputation beyond reputecjard has a reputation beyond reputecjard has a reputation beyond reputecjard has a reputation beyond reputecjard has a reputation beyond reputecjard has a reputation beyond repute
Default

It sounds like, when the database calculates the AutoNumber for the parent record, the tableadapter/dataset code is not picking up the change and setting it in the parent (which the client side relation then causes to transmit into the child table)

Right click your parent tableadapter, click Advanced.. Tell me, is the tickbox "Refresh Changes in the Database Back Into The Dataset" (or words to that effect) box ticked?
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 11-29-2008, 6:42 AM
VB.NET Forum Newbie
.NET Framework: .NET 3.5 (VS 2008)
 
Join Date: Oct 2008
Posts: 12
Reputation: 11
penno90 is on a distinguished programming path ahead
Default

Hiya cjard

when i right click on the parent table adapter i only have options for View Code, Edit Queries, Add Query, Preview Data and Properties. In the properties i don't see anything like what you're describing, which is a shame cos it sounds absolutely the kind of thing that i thought i would need! I am using VB.Net 2008.

Any ideas?
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 11-29-2008, 7:35 AM
cjard's Avatar
VB.NET Forum All-Mighty
.NET Framework: .NET 2.0 (VS 2005)
 
Join Date: Apr 2006
Age: 65
Posts: 6,137
Reputation: 658
cjard has a reputation beyond reputecjard has a reputation beyond reputecjard has a reputation beyond reputecjard has a reputation beyond reputecjard has a reputation beyond reputecjard has a reputation beyond reputecjard has a reputation beyond reputecjard has a reputation beyond reputecjard has a reputation beyond reputecjard has a reputation beyond reputecjard has a reputation beyond repute
Default

in vs2005 i would:

right click tableadater
choose configure
in the first one or two pages of the wizard (i forget which, and no vs in front of me i'm afraid) there is an advanced button
the advanced page contains 3 options:
* (something i forgot, maybe generate I/U/D statements)
* use optimistic concurrency
* refresh the dataset
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #5 (permalink)  
Old 11-29-2008, 9:55 AM
VB.NET Forum Newbie
.NET Framework: .NET 3.5 (VS 2008)
 
Join Date: Oct 2008
Posts: 12
Reputation: 11
penno90 is on a distinguished programming path ahead
Default

Hi cjard

i found the options now! iwas right clicking the table adapter on my form and not in the dataset.xsd

the third box is there as you said for 'Refresh the data table'. Unfortunately this is greyed out, and i am unable to tick the box? any idea why that might be?

cheers
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #6 (permalink)  
Old 11-30-2008, 5:12 AM
cjard's Avatar
VB.NET Forum All-Mighty
.NET Framework: .NET 2.0 (VS 2005)
 
Join Date: Apr 2006
Age: 65
Posts: 6,137
Reputation: 658
cjard has a reputation beyond reputecjard has a reputation beyond reputecjard has a reputation beyond reputecjard has a reputation beyond reputecjard has a reputation beyond reputecjard has a reputation beyond reputecjard has a reputation beyond reputecjard has a reputation beyond reputecjard has a reputation beyond reputecjard has a reputation beyond reputecjard has a reputation beyond repute
Default

I was sure this worked on Access but more and more I'm seeing users who say this option is greyed out for Access databases. Have a read of the DW2 link in my signature, section on:

Displaying Related Data
Saving Related Data
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #7 (permalink)  
Old 12-01-2008, 11:02 AM
VB.NET Forum Master
.NET Framework: .NET 2.0 (VS 2005)
 
Join Date: Sep 2007
Age: 22
Posts: 268
Reputation: 72
Stonkie probably authored a book by nowStonkie probably authored a book by nowStonkie probably authored a book by now
Default

Do both tables have defined primary keys and using autonumbers? I mean formally defined as such in Access? It would be impossible for the table adapter to know what filter to give the select if it can't tell which column is the primary key and what is the next value of the newly inserted autonumber.

I'm not sure if that helps, but I've had to work with poorly designed Access databases and I had a few options disabled like this because sometimes, the primary key was just a normal autonumber without a primary key status within Access and the designer couldn't tell it was the primary key. It may be something similar that is happening, like you forgot to set a property on a field somewhere...

Just an idea, if there are more than one autonumber on the same table (which is not allowed on SQL Server, but I think it is in Access), it may be the reason why the reason why the designer cannot tell which one of the two autonumber sequence to use for telling what is the newly added primary key.
__________________
The human mind's capability to comprehend abstract concepts is limited to the vocabulary it can use to describe it. The more precise the building blocks, the more complex the thoughts that can emerge... Sounds like the evolution of programming languages doesn't it?
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #8 (permalink)  
Old 12-01-2008, 8:28 PM
VB.NET Forum Newbie
.NET Framework: .NET 3.5 (VS 2008)
 
Join Date: Oct 2008
Posts: 12
Reputation: 11
penno90 is on a distinguished programming path ahead
Default

Both tables have defined primary keys using autonumbers.

Followed the link in your signature cjard. i went through the code for 'Walkthrough: Saving Data to a Database (Multiple Tables)' making appropriate changes for my code. Still the same problem. Interestingly enough at the bottom of this walkthrough, someone has posted ...

Quote:
Adding a customer and order at the same time is giving me an error. A message pops up that complains about a foreign key constraint. This occurs even though the code above has the customer changes taking place before adding orders.

Unfortunately still no solution though??
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #9 (permalink)  
Old 12-02-2008, 12:16 AM
VB.NET Forum Master
.NET Framework: .NET 2.0 (VS 2005)
 
Join Date: Sep 2007
Age: 22
Posts: 268
Reputation: 72
Stonkie probably authored a book by nowStonkie probably authored a book by nowStonkie probably authored a book by now
Default

It is possible that that person's problem (and maybe yours) is that he didn't set the relation in the dataset designer to "ON UPDATE CASCADE". You must do this manually so when the data is refreshed from the database when you insert the master row, it updates the id of the master row and the detail's foreign key refering to the row is updated as well (instead of remaining to -1 which is not a real master row).

To change this setting in the dataset designer, double click on the lines that appear between your tables and you will have access to the relationship's properties.

Maybe this is what is happening to you? I always thought this should have been the default to begin with, but I think it takes the settings from your database. And since in real life, the id from your database will never change, you don't need to do this on your database, only on the dataset.
__________________
The human mind's capability to comprehend abstract concepts is limited to the vocabulary it can use to describe it. The more precise the building blocks, the more complex the thoughts that can emerge... Sounds like the evolution of programming languages doesn't it?
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #10 (permalink)  
Old 12-02-2008, 9:54 AM
cjard's Avatar
VB.NET Forum All-Mighty
.NET Framework: .NET 2.0 (VS 2005)
 
Join Date: Apr 2006
Age: 65
Posts: 6,137
Reputation: 658
cjard has a reputation beyond reputecjard has a reputation beyond reputecjard has a reputation beyond reputecjard has a reputation beyond reputecjard has a reputation beyond reputecjard has a reputation beyond reputecjard has a reputation beyond reputecjard has a reputation beyond reputecjard has a reputation beyond reputecjard has a reputation beyond reputecjard has a reputation beyond repute
Default

OK, well the option may be greyed out because:

It is not available, or
It always happens and you cannot turn it off

I'd have expected unticked-greyed in the former and ticked-greyed in the latter. However, we can answer this question more directly:

Put a breakpoint on the line BEFORE you call Update. Look at the row values using the dataset visualizer (highlight datatable variable, point to it, click magnifying glass in the tooltip). You see your normal autonumber from the DATASET, right?

Now step over the Update (point of note: youre Update() the ORIGINAL datatable, NOT the copy that GetChanges gives you, right? Do not use GetChanges()!)

Now look at the datatable visualizer again. Crucial question: Did the value change (to the autonumber from the DB) or not?


If your DB is blank or empty then PLEASE DONT START your dataset autonumber from the SAME seed as the DB! (For the reason that if the DS calc an ID of 1, and then the DB also calcs 1, it wont look like it changed. Use a seed of -1 and a step of -1 in the dataset if needs be)
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On





All times are GMT -4. The time now is 9:23 PM.

Powered by vBulletin® Version 3.8.3
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.0


For advertising opportunities click here.