none
Error when saving deleted rows using data-adapter on top of a linq view RRS feed

  • Question

  •  

    Hi

     

    I have created a linq view and bound it to a data grid. When I delete rows and then try and save the deletions using a data-adapter I get an error "Deleted row information cannot be accessed through the row.", if I create standard data view this works fine. I think Linq tries to "re-query" the data table as the data adapter runs and cannot access deleted rows.

     

    Sample code:

     

    try

    {

    /*

    USE [TestDb]

    GO

    SET ANSI_NULLS ON

    GO

    SET QUOTED_IDENTIFIER ON

    GO

    SET ANSI_PADDING ON

    GO

    CREATE TABLE [dbo].[Table1](

    [Id] [int] IDENTITY(1,1) NOT NULL,

    [Text1] [varchar](50) NULL,

    [Number1] [int] NULL,

    CONSTRAINT [PK_Table1] PRIMARY KEY CLUSTERED

    (

    [Id] ASC

    )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]

    ) ON [PRIMARY]

    GO

    SET ANSI_PADDING OFF

    */

    SqlConnection connection = new SqlConnection("data source=.;initial catalog=TestDb;uid=TestUser;pwd=Password");

    SqlCommand selectCommand = new SqlCommand("select * from Table1", connection);

    SqlCommand deleteCommand = new SqlCommand("delete from Table1 where Id=@Id", connection);

    deleteCommand.Parameters.Add(new SqlParameter("@Id", SqlDbType.Int, 4, "Id"));

    SqlDataAdapter dataAdapter = new SqlDataAdapter();

    dataAdapter.SelectCommand = selectCommand;

    dataAdapter.DeleteCommand = deleteCommand;

    DataTable dataTable = new DataTable("Table1");

    dataTable.Columns.Add("Text1", typeof(string));

    dataTable.Columns.Add("Number1", typeof(int));

    try

    {

    connection.Open();

    dataAdapter.Fill(dataTable);

    //WORKING SECTION

    //****************

    //DataView dataView = new DataView(dataTable, null, "Text1", DataViewRowState.CurrentRows);

    //dataView[1].Row.Delete();

    //****************

    //ERROR SECTION

    //***************

    EnumerableRowCollection<DataRow> query =

    from data in dataTable.AsEnumerable()

    orderby data.Field<string>("Text1")

    select data;

    DataView dataView = query.AsDataView();

    dataView[1].Row.Delete();

    //****************

    dataAdapter.Update(dataTable.Select(null, null, DataViewRowState.Deleted));

    }

    finally

    {

    if(connection != null && connection.State == ConnectionState.Open)

    connection.Close();

    }

    }

    catch (Exception ex)

    {

    string message = ex.Message;

    }

    Thursday, May 29, 2008 10:11 AM

All replies

  • This LINQ query is actually utilising LINQ to datasets rather than LINQ to SQL.  I have moved your question to the right forum so the right people will see it.

     

    [)amien

    Tuesday, June 10, 2008 5:22 PM
  • This question is two years old but remains unanswered, and I'm encountering the same behavior (in a .NET 3.5 application).

    I have a list view bound to a DataView created using a Linq query and the AsDataView method. After deleting and while updating the dataset, the sort clause of the Linq query throws an exception (because the sort requires accessing a field of the deleted row).

    What is triggering the sort to be re-evaluated during the update?

    Thursday, August 25, 2011 12:05 AM