Last post Sep 29, 2011 11:19 AM by gourib
Sep 27, 2011 11:51 PM|gourib|LINK
I have a objectdatasource whose type name is a custom class (Say class Person). The class maintains a phone collection as List<Phone> where Phone is also a custom class.
I am facing problem while updating phone data. The phone data will be updated while updating Person data. Updating phone data fails for one case.
For a person, the collection will have either home phone or cell phone no. Phone table has fields PersonID, PhoneNo & PhoneType(enum, where 1 indicates HomePhone & 2 indicates cellphone).
While updating a person which has both the phone numbers set and now I am trying to remove a phone number, say I am deleting a previously added phonenumber, then the phone number for which a row already existed in Phone table should be deleted, but I am
not getting how to achieve this ?
I hope the above problem does make any sense.
Please suggest some solution or ask me any questions if you have doubt on above explanation.
Sep 28, 2011 12:20 AM|ramnalawade1986|LINK
You can update the Phonetable with PersonId(Primary key).
Update table Phonetable set PhoneNumber=123, PhoneType=1 or 2 where personid=YOUR Person id.
and update the same.
why you want to delete the row, if the work can be done with update statement.
if there is any doubt let me know.
Sep 28, 2011 08:59 AM|gourib|LINK
Thank you for your reply. Please go through below scenarios.
For a person, CellPhone is compulsory but not HomePhone.
In my case, during update, existing numbers are getting updated. If the person did not suppy the homephone no during insert, and now wishes to supply it in update, then this no also gets added during update.
Now consider the case not working.
The person has entered both the numbers during insert operation.
My phone table has following entries:
PersonID Phone No PhoneType
1 xxx 1
2 yyy 2
Now the person wishes to remove his homephone while updating and hence sets the value as empty or blank in the textbox provided. Since the value is null, it is not getting added to the List<Phone> collection. Therefor I am not getting how to delete this
previously added row during update. Since during update the person sets one of the phonenumber to null which already existed, then the row must get deleted from database table.
Sep 29, 2011 02:03 AM|ramnalawade1986|LINK
You can Delete all the records from phone table where personid=selected.
and insert the record which he selected. I think this is the fastest process.
otherwise you have to evalute 2 to 4 condition before updating table.
Sep 29, 2011 02:27 AM|kratos_Vimal|LINK
There are 2 solution to your problem
1. usually it is not considered a good DB practice to remove a record from your database completely instead a Column like IsActive is used which signifies whether the record has been deleted.
2. In your Collection the List phone would be containing all the Phone records, Create another list of the same type and add records which have been deleted
and write a query like this
objDataContext.Phone.DeleteAllOnSubmit(q);// Where objDatacontext is the instance of your BO ,Phone is the table and q is the List of removed //numbers
Sep 29, 2011 11:19 AM|gourib|LINK
Thank you to both of you.
Since cell phone is compulsory and home phone can be left blank, I maintained static peoperties to keep track of deleted home phone for a person instead of a collection. I am new to Linq, so I just used a stored procedure to delete the phone number whose
value I stored in static property.