473,785 Members | 3,214 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

DataSets, DataAdapters, DataTables and Relations

balabaster
797 Recognized Expert Contributor
I have my SQL database with the following structure:

Table1(
PrimaryKey Int Not Null Identity(1, 1) Primary Key,
TextData Varchar(50) Not Null
)

Table2(
PrimaryKey Int Not Null(Identity(1 , 1) Primary Key,
ForeignKey Int Not Null Foreign Key References Table1(PrimaryK ey),
TextData Varchar(50) Not Null
)

So now I'm wanting to access all my data using a DataAdapter and a DataSet.

I've set up my tables in my disconnected dataset and have created the relations that join the tables such that Table2.ForeignK ey is related to Table1.PrimaryK ey. I am using the FillSchema method to grab the primary key and column types.

I fill my datatables with data from my tables in my database. If I insert or modify data in Table1 and tell my data adapter to update, it runs off and adds/updates records in my database...same with Table2.

Now, I'm trying to wrap my head around the following scenario - I load my data into my dataset and add a new entry into Table1 in my DataSet (still disconnected), then someone else comes along and adds a record into Table1 in the database itself. Now the key I have in my DataTable clashes with the one in my SQL database. However, if I execute the DataAdapter.Upd ate method, it still runs off and processes the insert - it finds that there's an existing record in the SQL database and just inserts using the next available identity instead of the one it has - which I guess is good...it means my application doesn't crash after all.

So I start the process again and grab all my data, insert a record into the DataSet version of Table1 so that I'm given the next new key, lets say the next key is 11. I add a new record into Table1 in the database itself so that come time to execute the update record 11 already exists in the SQL database. But before I execute the update, I also want to add a row into Table2 in my dataset. So now I have one record in each table that are related to each other (presumably by the offline identity that was assigned when I created the row in Table1)

How do I figure out the correct information for the ForeignKey field in Table2 or is there some way of linking to a DataTable record uniquely such that if and when the key is changed in Table1, it cascades that change to linked records for Table2?
Apr 4 '08 #1
0 964

Sign in to post your reply or Sign up for a free account.

Similar topics

45
2452
by: cody | last post by:
I've seen an Introduction on ADO.NET with its Datasets on .NET TV and Iam now wondering how it is realized/used in real world applications. I don't believe that one would create a dataset and add relations to it and so on for every table in the application, this would be a real mess and this has nothing to do with OOP. Normally, would would create a class Customer, a class Invoices, Positions, Articles and so on. But how can this be...
2
2318
by: malcolm | last post by:
Hello, We have a robust (.NET 1.1 c# winforms) client-server application that utilizes many typed DataSets, typed DataTables and typed DataRows. Our application is a series of windows and popup windows where you can edit information and data, nothing out of the ordinary. I estimate we have something like 50 to 100 tables and/or views in our database each of which map to one strongly typed DataTable. Now we have some odd 20 to 30 typed...
2
1357
by: Adam Clauss | last post by:
Alright, as much as I've done CSharp, I've done very little with databases with it. So I'm just kind of messing around with, learning how it works. I am using the ADO.NET adapter provided with MySQL in a very simple web application. I have the connection, data adapter, and typed datasets created. In response to a button submit, I perform the following: accountsDataAdapter.Fill(accountsDS1); Label_Accounts.Text =...
4
1111
by: Agnes | last post by:
I got an invheader and invdetail in one datasetl with the foreign key to link up. It display in two separate datagrid, now If i only want to insert one record in invheader, as I process daInv.update() It warns me that I need to fill invdetail also... Do I need to create another dataadaptor .dataset for invheader again ?? Thanks
3
1589
by: cj | last post by:
I've used datatables and datasets before. Datasets being able to hold more than one table and datatables being only one table. My mind keeps coming up with recordsets. I can't remember how they fit into the picture. I'm going to be reading some records from a table in a sql db.
0
1222
by: S.Tedeschi | last post by:
Hi all; as posted some days ago, I'm converting an on-line app; I used to heavily rely on strongly-typed DataSets directly dropped onto pages, and so viewed by code(-behind) as well. In the next two weeks I discovered that such objects are no more directly usable in pages, namely in DataGrid which don't see them any more. Even if rebuilt in Component Designer, and so visible in code, DataSets are invisible in Page Designer, so now it's...
3
2126
by: =?Utf-8?B?SXbDoW4gUMOpcmV6?= | last post by:
Hi there, I have a question that has been raised on how an application should handle the data that is travelling through a three-layered application. Our data is going to be handled by Dataset object(s), and the data to be handled is as follows: We have N Companies with data, which is distributed among 1 table with all the data that is similar on all Companies, and 1 table for each Company that has specific data to store, so in case we...
0
887
by: Teo | last post by:
Hey guys!! I am looking at a user friendly tutorial on how to use Dataadapters, datatables and datasets to fill datagrids, etc. I am confused on using them a lot. Now I mostly use Datareaders and Nonqueries to enter and retrieve data in and out of DB. Please help. Thanks, Teo
12
3604
by: BillE | last post by:
I'm trying to decide if it is better to use typed datasets or business objects, so I would appreciate any thoughts from someone with more experience. When I use a business object to populate a gridview, for example, I loop through a datareader, populating an array list with instances of a custom class in the middle tier, and then send the array list up to the presentation layer and bind the gridview to it. If I use a typed dataset, I...
0
9480
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10315
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...
1
10085
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
8968
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
6737
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
5379
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
4045
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
2
3645
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2877
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.