更新条目时出错。有关详细信息,请参阅内部异常 [英] An error occurred while updating the entries. See the inner exception for details

查看:2733
本文介绍了更新条目时出错。有关详细信息,请参阅内部异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我删除列表框中的一个项目,我得到的问题中的错误,如下面的截图所示:



我不知道内部异常在哪里,但我尝试try,catch但是我在问题中有相同​​的错误。 p>

以下是所有代码:

 命名空间WpfApplication7 
{
///< summary>
/// Edit_Rooms.xaml的交互逻辑
///< / summary>
public partial class Edit_Rooms:Window
{
public Edit_Rooms()
{
InitializeComponent();
}

//初始化实体
WpfApplication7.AllensCroftEntities1 allensCroftEntities1 = new WpfApplication7.AllensCroftEntities1();
private Room ObjectIndex;

private void Window_Loaded(object sender,RoutedEventArgs e)
{
//将数据加载到Rooms。
System.Windows.Data.CollectionViewSource roomsViewSource =((System.Windows.Data.CollectionViewSource)(this.FindResource(roomsViewSource)));
//运行查询以转到roomsQuery从实体获取rooms表
System.Data.Objects.ObjectQuery< WpfApplication7.Room> roomsQuery = this.GetRoomsQuery(allensCroftEntities1);
//添加新房间时使用
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;
//返回一个ObjectQuery。
return roomQuery;
}

private void btnDelete_Click(object sender,RoutedEventArgs e)
{
//禁止用户尝试删除/取消选择行
if(ObjectIndex == null)
{
MessageBox.Show(无法删除空白条目);
}
else
{
//从数据集中删除对象,保存并输出消息
allensCroftEntities1.DeleteObject(ObjectIndex);
allensCroftEntities1.SaveChanges();
MessageBox.Show(Room Deleted);
}
}

private void btnSave_Click(object sender,RoutedEventArgs e)
{
try
{
// attempts保存更改
allensCroftEntities1.SaveChanges();
MessageBox.Show(Saved);
}
catch(Exception ex)
{
//如果不成功,输出错误消息
MessageBox.Show(ex.ToString());
}
}

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

private void btnPrevious_Click(object sender,RoutedEventArgs e)
{
//禁止用户在第一个项目之前进入上一个项目
if (listbox.SelectedIndex> 0)
{
listbox.SelectedIndex - = 1;
}
}

private void btnNext_Click(object sender,RoutedEventArgs e)
{
//禁止用户在最后一个项目之后运行并抛出错误
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)
{
//输出所选房间的索引
ObjectIndex = listbox.SelectedItem as房间;
}
}
}


解决方案>

单击查看详细信息...将打开一个窗口,您可以在其中展开内部异常我的猜测是,当您尝试删除记录有一个引用约束违反。内部异常将为您提供更多信息,因此您可以在删除记录之前修改代码以删除任何引用。




When i delete an item in a listbox, i get the error in the question as shown in the screenshot below:

I do not know where the inner exception is, but i tried try, catch but i got the same error in the question.

Here is all of the code :

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;
    }
}
}

解决方案

Click "View Detail..." a window will open where you can expand the "Inner Exception" my guess is that when you try to delete the record there is a reference constraint violation. The inner exception will give you more information on that so you can modify your code to remove any references prior to deleting the record.

这篇关于更新条目时出错。有关详细信息,请参阅内部异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆