resetting child records when delete parent

acorn

Active member
Joined
Mar 8, 2008
Messages
32
Programming Experience
5-10
I have two tables- one called territory and the other called contacts. Each contact can belong to a territory.
On my form, i allow the user to delete a territory. However, i don't want to actually delete the contacts associated with this territory from the database. I just want to set the territoryID on them to DBNULL.
I don't have any relationships set at the database level that will cause an automatic deletion of these child records.
Right now, when the user clicks on the delete button, the territory row is properly removed from the datagrid control. But now I'd like to set up my SAVE button so that I have a change to reset the territoryIDS on the child records... and then delete the territory from the database.
So far, in my save button I have
Dim TerritoryDeletions() As DataRow = TerritoryDS.Territory.Select("", "", DataViewRowState.Deleted)

I don't know how to loop through this territoryDeletions object and get all the territory IDs.
 
PS, Visual studio names relations like FK_Parent_Child, so why does the naming of your relation suggest that Contacts is the parent, but the error message suggests that contact is the child?
 
re: naming convention for my relationship

The only thing i can say is that perhaps the naming convention is different in this case because visually... on the data designer surface, my child table is to the left of the parent??
i dunno... but i am 100% sure that under the Parent Table drop down list, the territory table is specified, and under the Child Table drop down list, the contacts / householder table is selected.

Regarding your other question about whether there are child records or not, the answer is yes, there would be. but i'm not necessarily asking the system to delete when this message appears.

Since adding the relationship as recommended / described in this post, i can't ever click on my refresh button to refill my dataadapters....

when i start the application, it loads fine... and the code is quite similar on the load event to the refresh. Immediately after start up, if i try to click refresh, it bombs.

??

why is the system trying to clear the TABLE? aren't i just asking it to re-select records and fill the adapter? maybe this is a stupid question...
 
The only thing i can say is that perhaps the naming convention is different in this case because visually... on the data designer surface, my child table is to the left of the parent??
Ehm.. If you mean in the relation designer, the child table is the one on the right underneath where it says "Child".. See the attached screenshot
If you mean the designer surface (what you said) i.e. that thing you see in the DDS link in my signature, the grey desktop kinda thing with your datatables and you drag them round.. dude, it doesnt matter where the heck you position your tables on there. The spatial arrangement does not define the relationship!

Regarding your other question about whether there are child records or not, the answer is yes, there would be. but i'm not necessarily asking the system to delete when this message appears.
Huh? Yes you are! youre doing SOMETHING that is trying to clear the parent table, but child rows exist. Youre not allowed to leave children orphaned. Clear the children first!

Since adding the relationship as recommended / described in this post, i can't ever click on my refresh button to refill my dataadapters....
I've never seen this refresh button? You dont refill a data adpater, and you arent using data adapters. Youre using tableadapters and they dont hold data, datatables do. Tableadapters push data back anf forth between database and app. Right now youre doing the programming equivalent of asking me for a screwdriver, and when I hand it you you say "no, the thing you bang nails in with" -> thats a hammer.
Please try to call things by their proper names because your posts are getting really confusing

when i start the application, it loads fine... and the code is quite similar on the load event to the refresh. Immediately after start up, if i try to click refresh, it bombs.
I believe you, but it doesnt help me.

why is the system trying to clear the TABLE?
Er. Because when the tableadapter's .ClearBeforeFIll property is set to true, it will attempt to clear a datatable before filling it? Shoot me for pointing out the obvious...

aren't i just asking it to re-select records
Im not sure youre sure what youre asking yourself? How about you provide every line of code in the problematic method, and also comment it so I know what you think that line of code does, cause I guarantee that somewhere, what it does and what you think it does, are quite different...

and fill the adapter?
You dont fill an adapter any more than you fill a tap with water when you wash the car. You fill a bucket with water using a tap
 

Attachments

  • untitled.PNG
    untitled.PNG
    16.9 KB · Views: 24
Naming Convention:
My response to you is: Ok.
I'm not sure what else I can say / what other info to provide. You're asking why the naming convention is different from what you feel it should be. I didn't explicitly name it myself. it was generated. So... it was just a guess as to why the system would pick up the child table name first.
I'm a newbie, not an idiot - So I do understand that the location of the tables on the designer doesn't define the relationship. I was thinking it might affect which table was picked up first for the NAME of the relationship - which was your question, was it not?
As stated before, and as confirmed by your snapshot, there is nothing wrong with the way the relationship itself has been defined.

Huh? Yes you are! youre doing SOMETHING that is trying to clear the parent table, but child rows exist. Youre not allowed to leave children orphaned. Clear the children first!

I think it's obvious that the system is trying to do SOMETHING that clears the tables in an order that it doesn't like... but I don't know what I've done to trigger this error... which is why i've started this post.
I understand what the error message is saying... and I also know what orphaned records are. But i don't understand why (in this specific event) i can't fill my parent table first via the parent "TABLE ADAPTER" first before calling the fill method on the child TABLE ADAPTER.

My apologies if I'm not using the right lingo - just figured that by this post, you'd understand what it is that I'm trying to accomplish via the error messages and code snippets.

to clarify my question about why the attempt to clear the table is taking place - I'm trying to understand why this step is even necessary. For eg. Why do i have to set something to NULL first (let me guess, this is a bad example...?) before I set it to object X? I'll do my own research on this... it was just a question that came to my head as i was responding.

I do appreciate you trying to help out but the sarcasm doesn't really help much. For some of us learners, it's hard to articulate questions clearly when you have pressure to get something out the door and something's not working quite right.
 
Naming Convention:
My response to you is: Ok.
I'm not sure what else I can say / what other info to provide. You're asking why the naming convention is different from what you feel it should be. I didn't explicitly name it myself. it was generated. So... it was just a guess as to why the system would pick up the child table name first.
It doesnt; that's what I'm saying.. I cant work out why it would be named that way round.. If in doubt, I'd actually delete the relationship and recreate it, ensuring that the parent and child are correct

I'm a newbie, not an idiot - So I do understand that the location of the tables on the designer doesn't define the relationship. I was thinking it might affect which table was picked up first for the NAME of the relationship - which was your question, was it not?
OK, maybe youre not used to asking questions in forums, but I really have only a very limited awareness of what is going on in your system, and the way you phrase things, and the words you choose make it very hard. In computer terms the best style of posting is to be very literal, not use colloqialisms, post all error messages in full and the lines on which they occur and to name everything correctly. I give a huge amount of my free time to people of this forum for free, so please dont sit there and criticize something you dont like about my tone, when I'm drilling you for information you havent provided and struggling to get you to explain your problem clearly

As stated before, and as confirmed by your snapshot, there is nothing wrong with the way the relationship itself has been defined.
I definitely recommend deleting it and starting another. CLick the column in the parent, point to the grey square to the left of the name, click, drag and drop into the grey square in the child that you wish to link. Set it to being an FK, if that's your requirement.. Quick check of all columns and that should be it


I think it's obvious that the system is trying to do SOMETHING that clears the tables in an order that it doesn't like...
It's not artificially intelligent. The presence or absence of a datarelation will not make things clear and fill in the right logical order, you have to do this as the programmer. If youre calling Fill, you hav eto know that by default it will first invoke a clear. If you try and fill the parent it will fail because child rows exist and the TA is trying to do a clear before a fill

Learning to work the debugger in vs2005 would save a lot of these troubles


i can't fill my parent table first via the parent "TABLE ADAPTER" first before calling the fill method on the child TABLE ADAPTER.
I think I said 3 or 4 posts ago that TA will clear first, before fill (otherwise you'd either get constraint errors or just keep appending the same records). You understand that a clear can create orphans, and that's not allowed, but you didnt seem to make this link between "fill clears first" and creating orphans yet. Here is some code, see the comments for why i do it:


VB.NET:
childDT.Clear() 'kill children before they are orphaned
parentTA.Fill(parentDT) 'clear and refill parents

childTA.ClearBeforeFill = False 'dont clear children first, see below
ForEach parentRo as ParentRow in parentDT 'for every parent
  childTA.FillByParentID(childDT, parentRo.ID) 'fill the children of that parent, note if we cleared first, we'd only have last parent's children!, see above
Next parentRo


My apologies if I'm not using the right lingo - just figured that by this post, you'd understand what it is that I'm trying to accomplish via the error messages and code snippets.
I probably could, but I really dont have the time. I'm like a computer myself; feed me literal info that my brain can process and i'll get the answer very quickly.. Have me start inventing and guessing at what you mean and I cant be bothered to answer beyond a few sentences that are designed to get the info i want because so many times in the past (and ive racked up tens of thousands of posts on various places) someone's asked a question, its been ambiguous or hard to understand, I've put 20 minutes into writie a long and explanatory post of every option i can think of.. and its turne dout to be "none of the above" - i lose 20 minutes. As such, I dont do it any more. If a post is crap and hard to understand, I simply ask for clarification

to clarify my question about why the attempt to clear the table is taking place
Becuase most the time when you fill you dont wanna fill onto the end of your existing data?
As such, rahter than have you call CLear() yourself every time, MS just made the TA do it and switched it via a boolean for the few times you wanna append, like in my code example where I have maybe 10 out of 10,000 parents loaded, and i just wanna fill the children of those 10, not the children of all 10,000 (which might be many more than 10,000)


Why do i have to set something to NULL first (let me guess, this is a bad example...?) before I set it to object X?

You mean:

Dim s as Stirng = Nothing
s = "Something"

?

You dont

Dim s as String = "Something"

Will work just fine



For some of us learners, it's hard to articulate questions clearly when you have pressure to get something out the door and something's not working quite right.
I appreciate that, i've been in that situation, i've delayed projects while my knowledge updated and it's a hard place to be.. I did, however, when asking for help in forums and was asked to do things, do them as asked..
You might just have to put up with the sarcasm though; it's part of my sense of humour ;)
 
Last edited:
Back
Top