2013-03-17 65 views
13

內部異常,當我在一個列表框中刪除項目,我得到這個問題的錯誤如下面的截圖: error更新條目時發生錯誤。詳情請參閱

我不知道在哪裏的內部異常的,但我想嘗試,趕上,但我在問題中得到了同樣的錯誤。

這裏的所有代碼:

namespace WpfApplication7 
{ 
/// <summary> 
/// Interaction logic for Edit_Rooms.xaml 
/// </summary> 
public partial class Edit_Rooms : Window 
{ 
    public Edit_Rooms() 
    { 
     InitializeComponent(); 
    } 

    //initialises entities 
    WpfApplication7.AllensCroftEntities1 allensCroftEntities1 = new WpfApplication7.AllensCroftEntities1(); 
    private Room ObjectIndex; 

    private void Window_Loaded(object sender, RoutedEventArgs e) 
    { 
     // Load data into Rooms. 
     System.Windows.Data.CollectionViewSource roomsViewSource = ((System.Windows.Data.CollectionViewSource)(this.FindResource("roomsViewSource"))); 
     //runs a query to go the roomsQuery to get the rooms table from the entities 
     System.Data.Objects.ObjectQuery<WpfApplication7.Room> roomsQuery = this.GetRoomsQuery(allensCroftEntities1); 
     //used when adding new rooms 
     roomsViewSource.Source = roomsQuery.Execute(System.Data.Objects.MergeOption.AppendOnly); 
    } 

    private System.Data.Objects.ObjectQuery<Room> GetRoomsQuery(AllensCroftEntities1 allensCroftEntities1) 
    { 
     System.Data.Objects.ObjectQuery<WpfApplication7.Room> roomsQuery = allensCroftEntities1.Rooms; 
     // Returns an ObjectQuery. 
     return roomsQuery; 
    } 

    private void btnDelete_Click(object sender, RoutedEventArgs e) 
    { 
     //prevents user trying to delete nothing/unselected row 
     if (ObjectIndex == null) 
     { 
      MessageBox.Show("Cannot delete the blank entry"); 
     } 
     else 
     { 
       //deletes object from dataset, saves it and outputs a message 
       allensCroftEntities1.DeleteObject(ObjectIndex); 
       allensCroftEntities1.SaveChanges(); 
       MessageBox.Show("Room Deleted"); 
     } 
    } 

    private void btnSave_Click(object sender, RoutedEventArgs e) 
    { 
     try 
     { 
      //attempts to save changes 
      allensCroftEntities1.SaveChanges(); 
      MessageBox.Show("Saved"); 
     } 
     catch (Exception ex) 
     { 
      //if unsuccessful, outputs an error message 
      MessageBox.Show(ex.ToString()); 
     } 
    } 

    private void btnFirst_Click(object sender, RoutedEventArgs e) 
    { 
     listbox.SelectedIndex = 0; 
    } 

    private void btnPrevious_Click(object sender, RoutedEventArgs e) 
    { 
     //prevents user going to the previous item before the first item 
     if (listbox.SelectedIndex > 0) 
     { 
      listbox.SelectedIndex -= 1; 
     } 
    } 

    private void btnNext_Click(object sender, RoutedEventArgs e) 
    { 
     //prevents user going after last item and throwing up an error 
     if (listbox.SelectedIndex < listbox.Items.Count) 
     { 
      listbox.SelectedIndex += 1; 
     } 
    } 

    private void btnLast_Click(object sender, RoutedEventArgs e) 
    { 
     listbox.SelectedIndex = listbox.Items.Count - 1; 
    } 

    private void listbox_SelectionChanged(object sender, SelectionChangedEventArgs e) 
    { 
     //outputs index of the selected room 
     ObjectIndex = listbox.SelectedItem as Room; 
    } 
} 
} 

回答

37

單擊「查看詳細信息...」將打開一個窗口,您可以在其中展開「內部異常」,我的猜測是當您嘗試刪除記錄時存在參考約束違規。內部異常會給你更多的信息,所以你可以在刪除記錄之前修改你的代碼以刪除任何引用。

enter image description here

7

點擊「查看詳情」,找到內部異常。

相關問題