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(), 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
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(), 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