By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,052 Members | 1,303 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,052 IT Pros & Developers. It's quick & easy.

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

P: n/a
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
Share this question for a faster answer!
Share on Google+

This discussion thread is closed

Replies have been disabled for this discussion.