473,497 Members | 2,166 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

C# insertion of a record with CurrencyManager AddNew() method during the insertion of a father table record (with CurrencyManager AddNew() too)

Hi,
I have noticed a strange behaviour of CurrencyManager objects in C# (I
use Visual Studio 2005 Professional Edition).
Suppose that you have a SQL Server database with 2 tables called
"Cities" and "Persons", and that:
"Cities" has 2 fields called "IDCity" and "NameCity"
"Persons" has 3 fields called "IDPerson", "NamePerson" and
"IDCityAddress"
with "IDCity" and "IDCityAddress" fields relationed with the
classical father-child relation (the value for "IDCityAddress" is
required, you can't set it to null).
Suppose to define 2 DataTable objects called "dtCities" and
"dtPersons" (bound to the 2 tables), and 2 CurrencyManager objects
in this way:
cmCities = (CurrencyManager)BindingContext[dtCities];
cmPersons = (CurrencyManager)BindingContext[dtPersons,
"Cities_Persons"];
(where "Cities_Persons" is the relation that binds "IDCity" of
"dtCities" to "IDCityAddress" of "dtPersons").
Now, think to insert a new city with the statement:
cmCities.AddNew();
and, before confirming the "dtCities" record insertion with the
cmCities.EndCurrentEdit() statement, insert a new person with the
statement:
cmPersons.AddNew();
(of course in the C# form there will be some TextBox controls bound to
the various fields of the 2 current records of the tables).
Ok, now you can't confirm the "dtPersons" record insertion with
the cmPersons.EndCurrentEdit() statement before confirming the
"dtCities" record insertion (the "IDCityAddress" value is
required in "dtPersons", and that value doesn't exist yet in the
"IDCity" field of "dtCities"), so you must necessarily give
this order to the statements:
cmCities.EndCurrentEdit();
cmPersons.EndCurrentEdit();
but when the first statement is executed, cmPersons.Position is set to
-1, and the "dtPersons" new record isn't saved in
"dtPersons".
This behaviour doesn't happen if I add a "dtPerson" record during
the modify of a "dtCities" record (cmPersons remains set to its
value and the cmPersons.EndCurrentEdit() statement saves the new record
correctly, adding it to the table).

Can you explain me why??? Is there a way to insert a person during the
insertion of a city without being forced to introduce other additional
code?

Thank you very much

Sep 27 '06 #1
0 3822

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

Similar topics

6
1769
by: Christopher Benson-Manica | last post by:
I have some markup like the following: <form> <table> <script> <!-- Write the table markup //--> </script> </table> <form>
1
5663
by: Jon Trelfa | last post by:
I have been fighting with this script for several days and I'm finally at the end of my rope. Here's the scenario: -I have to add an entry into 1 table, called "calendar" -I must retrieve the...
2
10610
by: William Ortenberg | last post by:
I'm creating a new row in a table with via the AddNew method, where the table ID is an Autonumber field. I want to capture the value of the ID after I create the row. There are no other unique...
3
1392
by: hazz | last post by:
if i have a table record called Rule 1 with 1. table name 2. table column name 3. operator (>,< =) 4. value (0 to n) 5. score how could I create code to create a rule automatically so...
3
2393
by: PAUL | last post by:
Hello, I have 2 datasets I am trying to update. The parent table seems to update fine but when I go update the chiled table I get an error message that says I need a related record in the parent...
2
12428
by: Thammarat charoenchai. | last post by:
After I use BindingSource.AddNew Method. (in my code is OrderBindingSource.AddNew). How can I set field value. like Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As...
1
2033
by: back100y | last post by:
I have an application where I am using a DataGridView control to display DataTables stored in a DataSet. What I need to do is be able to prevent a user from adding a new if there is already a...
4
3666
by: salzan | last post by:
Hello, I'm trying to insert a new record in my recordset using AddNew method. I can't figure out how to declare values for the controls in my recordset. Can someone give me an example. Thank you...
3
1637
by: salzan | last post by:
The code below works in that I can insert records into the recordset. Sub SaveRecord() With rsRecord .AddNew ! = Me!Proj_Id ! = Me!Proj_Name ! = Me!Cat_Id ...
0
7120
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,...
0
6991
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...
0
7196
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...
1
6878
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...
0
7373
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...
0
5456
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,...
0
3088
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...
1
649
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
286
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...

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.