public void AddPhone(int custID, string phoneType, string phoneNumber) { using (var context = new VehicleEntities()) { PhoneNumber queryAdd = new PhoneNumber { customerID = custID, phoneType = phoneType, number = phoneNumber }; try { context.PhoneNumbers.AddObject(queryAdd); context.SaveChanges(); // if context.SaveChanges() succeeded add text to the label Add -> lbl.Add.Text // lblAdd.Text += string.Format("<br/>Added: number {0} of type: {1}", queryAdd.number.ToString(), queryAdd.phoneType); //if context.savechanges() succeded then delete phone only if old values are DIFFERENT then new values // Delete phone !only! if NEW values AREN'T same as OLD values!! phoneTypeOld = ViewState["phoneTypeOld"].ToString(); phoneNumberOld = ViewState["phoneNumberOld"].ToString(); if (phoneType != phoneTypeOld || phoneNumber != phoneNumberOld) { DeletePhone(custID, phoneTypeOld, phoneNumberOld); } } catch (OptimisticConcurrencyException ocex) { // Resolve the concurrency conflict by refreshing the // object context before re-saving changes context.Refresh(RefreshMode.StoreWins, queryAdd); lblAdd.Text = ocex.ToString(); //throw; } catch (InvalidOperationException ioex) { context.Refresh(RefreshMode.StoreWins, queryAdd); lblAdd.Text = ioex.ToString(); } catch (SqlException sqlexc) { context.Refresh(RefreshMode.StoreWins, queryAdd); lblAdd.Text = sqlexc.ToString(); } catch (Exception) { if (phoneType == phoneTypeOld && phoneNumber == phoneNumberOld) { lblAdd.Text = ""; } else { lblAdd.Text = "Error while trying to update record. The object could not be added." + "Make sure that the same phone does not aleady exist."; } } } }
public void InsertPhone(int custID, string phoneType, string phoneNumber) { using (var context = new VehicleEntities()) { PhoneNumber queryInsert = new PhoneNumber { customerID = custID, phoneType = phoneType, number = phoneNumber }; try { context.PhoneNumbers.AddObject(queryInsert); context.SaveChanges(); } catch (OptimisticConcurrencyException) { // Resolve the concurrency conflict by refreshing the // object context before re-saving changes context.Refresh(RefreshMode.StoreWins, queryInsert); lblInsertPhone.Text = "The phone number you attempted to insert was already inserted by " + "another user. The insert operation was canceled."; } catch (UpdateException) { lblInsertPhone.Text += "There is already same phone number"; } catch (Exception) { lblInsertPhone.Text += "Exception on Inserting New Phone number"; } } }