简体   繁体   中英

WPF datagrid update database

I am pretty new in C#. I was writing a small GUI database application which uses a datagrid to show the table data. When I edit a row in the grid press the update button the update function of tableadapter is called and the change is propagated back to DB. Then I have implemented a handler for row changed event which in turns call the update function of tableadapter. But this time when some value is changed and enter key is pressed the handler is called and the InvalidOperationException is raised with the message "There is already an open DataReader associated with this Command which must be closed first." but the database is changed anyways. How to stop the exception to be raised. I have multiple active resultsets set to true in the connection string.

The constructor

public WSim(MainWindow h)
{
   InitializeComponent();

   //database connection with strongly typed dataset
   usersAdapter = new testDBDataSetTableAdapters.usersTableAdapter();
   users = usersAdapter.GetUsers();

   users.usersRowChanged +=new testDBDataSet.usersRowChangeEventHandler(users_usersRowChanged);

   this.DataContext = users.DefaultView;
}

The row changed event handler

private void users_usersRowChanged(object sender, testDBDataSet.usersRowChangeEvent e) 
{
    output.Content = "Row Modified";
    usersAdapter.Update(users);
}

The datagrid xaml

<DataGrid Height="200" HorizontalAlignment="Left" Margin="26,249,0,0" Name="userGrid" VerticalAlignment="Top" Width="381" AutoGenerateColumns="True"
              SelectionMode="Extended" SelectionUnit="FullRow"  ItemsSource="{Binding}"
              CanUserReorderColumns="True" CanUserResizeColumns="True" CanUserSortColumns="True"
              CanUserAddRows="True" CanUserDeleteRows="True"/>

As the exception says, you should close the reader first. You have not provided enough information and I cannot explain more; But See this link . I think it will help you.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM