473,657 Members | 2,567 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Query re ADO.NET, constraints, relations, and cascading updates/deletes

One part of a customer project I'm working on involves what seem
like fairly straightforward updates to a set of related tables.
While I've developed software for a number of years (it only seems
like centuries on days like this <grin>), I'm new to C# and ADO.NET
and I'm running into problems with record deletions.

I (think I am) applying ForeignKeyConst raints correctly. I'm not
applying DataRelations (yet), but those _appear_ to be related to
record retrieval rather than record deletion. Yet my "Cascade"
Rules don't appear to be having the effect I want, that of deleting
child records relating to the parent (Employee) record I'm
attempting to delete.

My fallback is to do it myself: starting a transaction and then
"tree-walking" in my procedural code to delete child and grandchild
records. Is that my only choice? Or am I doing something obviously
wrong, omitting something critical, or simply performing the right
steps in the wrong order?

Any hints will be appreciated (including "it doesn't work in .NET
2003, wait for .NET 2005-or-RealSoonNow(tm) ").

Rather than post a set of clips from about eight modules (so far),
let me lay out what I'm attempting to do and ask whether I seem to
at least be approaching the problem correctly.

----

Imagine I have a staff of employees, each with zero or more Phone
Numbers (office, home, cell, etc.) and zero or more Skills. Each
works on zero or more Projects, and each Project has zero or more
Deadlines:

Employees: Employee_ID (Primary Key, autoincrement),
Employee_Name, etc.

PhoneNumbers: Phone_Item_ID (Primary Key, autoincrement),
Employee_ID, Phone_Number, etc.

Skills: Skill_Item_ID (Primary Key, autoincrement),
Employee_ID, Skill_Descripti on, etc.

Projects: Project_ID (Primary Key, autoincrement),
Employee_ID, Project_Name, etc.

Deadlines: Deadline_ID (Primary Key, autoincrement),
Project_ID, Deadline_Date, Deadline_Descri ption,
etc.

The relationships are fairly straightforward (using [1:M] because I
can't find an "infinity" symbol on my keyboard):

Employees--[1:M on Employee_ID]--PhoneNumbers
Employees--[1:M on Employee_ID]--Skills
Employees--[1:M on Employee_ID]--Projects
Projects---[1:M on Project_ID]---Deadlines

I can load all of the tables from the database into a DataSet using
a set of OleDbDataAdapte rs, one per table. I can browse the
Employee table. But when I attempt to delete a row from the
Employee table, even my best attempts to date continue to yield an
error message equivalent to "The record cannot be deleted or changed
because table 'Skills' includes related records."

At startup, I load each table into my (one) DataSet 'dset':

// Set up the commands and TableMappings
da_em = new OleDbDataAdapte r();
da_em.SelectCom mand =
new OleDbCommand("S ELECT * FROM Employees", conn);
cb_em = new OleDbCommandBui lder(da_em);
da_em.TableMapp ings.Add("Emplo yees", "em");
(4 more similar sections of code, once for each remaining table)

// Fill the table Schema and Tables
da_em.FillSchem a(dset, SchemaType.Mapp ed, "Employees" );
da_em.Fill(dset , "Employees" );
(4 more similar sections of code, once for each remaining table)

// Apply constraints
// Employees [1:M] Skills
fkc_em_sk = new ForeignKeyConst raint(
"Employees_OneT oMany_Skills",
dset.Tables["em"].Columns["Employee_I D"],
dset.Tables["sk"].Columns["Employee_I D"]
);
fkc_em_sk.Delet eRule = Rule.Cascade;
fkc_em_sk.Updat eRule = Rule.Cascade;
fkc_em_sk.Accep tRejectRule = AcceptRejectRul e.Cascade;
dset.Tables["sk"].Constraints.Ad d(fkc_em_sk);
(3 more similar sections of code, once for each remaining FK
constraint)

dset.EnforceCon straints = true;
conn.Close();
Later, when I'm attempting to delete an Employee record, I use the
following code:

// Following the Remarks section under "DataSet Class [C#]"...
// Issue Delete for current record
dset.Tables["em"].Rows[currentposition].Delete();
// Extract changes
DataTable dt = dset.Tables["em"].GetChanges();
if (dset.HasErrors ) {
PgtCommon.PgtPo pUpMsg("Errors in EditEmployee deletions");
}

// Use changes to update DataSet
**daem.Update(d t); // Apply this change via DataAdapter
dset.Merge(dt);
dset.Tables["em"].AcceptChanges( ); // GONE

The ** line marks where the exception occurs.

For what it's worth, the deletion process works just fine when the
Employee records _don't_ have any child/grandchild records.

As I said, any clues will be appreciated.
Frank McKenney, McKenney Associates
Richmond, Virginia / (804) 320-4887
Munged E-mail: frank uscore mckenney ayut minds pring dawt cahm (y'all)
--
Literature exists for the ordinary educated man, and any
literature that actively requires enormous training can be at best
of only peripheral value. Moreover, such a mood in literature
produces the specialist who only knows about literature. The man
who only knows about literature does not know even about
literature. -- Robert Conquest, "The Dragons of Expectation"
--
Nov 17 '05 #1
0 1631

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

16
4196
by: Dave Weaver | last post by:
I'm having severe performance issues with a conceptually simple database. The database has one table, containing weather observations. The table currently has about 13.5 million rows, and is being updated constantly. (The database is running on a dual 550MHz PIII with 512MB RAM. I have PostgreSQL 7.1.3 on i686-pc-linux-gnu, compiled by GCC 2.96 on RedHat 7.2) On the whole, queries are of the form: SELECT ? FROM obs WHERE station = ?
1
318
by: nicolaas | last post by:
Hi everyone Is there someone out there who can tell me how to create relationships between tables using VB??? PS I am using MS ACCESS 2003 and I have read the help... THANK YOU
96
5664
by: Karen Hill | last post by:
SELECT surgeries.*, animals.* FROM surgeries INNER JOIN animals ON .=. AND WHERE ((.=Date()) Or .=Date()); I'm trying to write a query that joins two table together, animals and surgeries where surgeries.id = animals.id and only where the surgery date was date_a or date_b. I'm doing this in Microsoft Access 2000 and am tearing out my hair
3
4507
by: Oleg Lebedev | last post by:
Greetings. Is it possible to modify a foreign key constraint and force it to cascade on update? If there is no such SQL command, then is it possible to update some system tables to accomplish this? The problem is that I have a bunch of tables with FK constraints and I need to update primary key values in a lot of these tables. FK constraints were declared, but without cascading updates. Now, I am trying to modify all of them to...
4
3460
by: G .Net | last post by:
Hi I have a DataSet with several DataTables. I have set up relations between these tables. I want to delete all the tables and re-fill them. However, when I try to do so, even after using Relations.Clear() I still get excecptions, such as: Cannot remove ForeignKeyConstraint - remove the constraint first
26
2090
by: Allen Browne | last post by:
In Access 2000 and later, you can create a relation between tables where related records can be automatically set to Null rather than deleted when the primary record is deleted. I have not seen anyone using or discussing this feature, so there is a new article explaining the concept here: Cascade to Null Relations at: http://allenbrowne.com/ser-64.html
2
17647
by: =?Utf-8?B?Q2hyaXM=?= | last post by:
How can I run this query against a table in my Access database? I don't know hwo to use it in C#. In VB I would use .Recordset = "some sql statement". How do I do this in C#? //I get a vlaue form a cell and apply it to the SQL statement commandCol = scriptDataGridView.FormattedValue.ToString(); string sCommand = "SELECT CommandString FROM Commands WHERE CommandName = " + commandCol; //This reutne a valie SQL statement which will return...
1
1724
by: Coll | last post by:
I'm working on a database that someone else created. I have a form - frm_main that is based on a query that is based on two tables. The join is such that the first table tbl_JCN has all records included in the query, but the second table tbl_SOC only has matching records displayed. On the form (frm_main), there is a combo box - you can make a selection and the form filters to display that record. There is also a delete record button. When...
1
1659
by: CoreyReynolds | last post by:
Hello, I have attached a picture of my Access form. Each record has a delete button beside it, pressing it deletes the record and cascades to the child records. Horray. Except sometimes, for a reason I can't explain it doesn't work. I have to click off a record then back onto it and it will work. I'm using the default VB code that access generates to perform the delete "The table cannot be deleted or changed because table includes...
0
8392
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8823
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8730
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8503
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8605
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7321
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5632
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4151
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
2726
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.