This thread looks to be a little on the old side and therefore may no longer be relevant. Please see if there is a newer thread on the subject and ensure you're using the most recent build of any software if your question regards a particular product.
This thread has been locked and is no longer accepting new posts, if you have a question regarding this topic please email us at support@mindscape.co.nz
|
Hello, I have a web project (ASP.net C#) and I've encountered a problem when I'm trying to insert in database some information stored in a generic list. The code is looking like this: 1) In usercontrol: public List<Sn_cda_tuburi> lstTuburi; public Sn_cda_tuburi tube; .... //add information to the lstTuburi ......... tube = new Sn_cda_tuburi(); //here I put the information in the tube object and add it to the list lstTuburi } //here is the event when I'm saving the data that I've inserted protected void btnSave_Click(object sender, EventArgs e) Sn_cda_tuburi_Controller ctrl = new Sn_cda_tuburi_Controller(); //comanda is another object. The function returns true when everything is ok bool val_return = ctrl.InsertTubesList(lstTuburi, comanda, out ret_nrCda); .............. } Now, Sn_cda_tuburi_Controller is treated in a library. The function InsertTubesList is looking like this: public bool InsertTubesList(IList<Sn_cda_tuburi> tubesList, Sn_comenzi comanda, out string nrcda)
The scenario is the following: the user fills an order (comanda) with some generic information (i.e. client's name, date etc) stored in comanda object and chooses a list of products (stored in the lstTuburi list). After this is done, he pushes the Save button and the order is saved in database. The problem occurs when the script tries to insert the products in the corresponding table. Althought the order is inserted in its table, the products are not inserted. At a glance, everything seems to be fine (the InsertTubesList function returns true, each product is valid and not null) but i don't understand why I have this problem. i tried to change the function InsertTubesList in the following way and the products were inserted: ....................... try UnitOfWork.SaveChanges(); ................................. Even it worked I know that's not right (because I have to make some validations). Do you have any idea how to solve this? Thank you, Catalin
|
||
|
|
Hello Catalin, Your fix is correct -- you need to do the IUnitOfWork.Add before the SaveChanges. (SaveChanges persists the unit of work, and your new tubes are not part of the UOW until they've been Added.) If you want to include only valid items in the UOW, then call ValidateItem before doing the Add. |
|