none
DataTable.Rows.Delete() fails to remove rows from DataTable RRS feed

  • Question

  • I have a method that takes in a DataTable, from which I got from an SQLite database. I call this method between printing the entire contents of the DataTable. What my problem is, is that the data table isn't changing. I put testing prints in the if statements to see if they were reaching, and everything checks out. My only issue is that the data rows are not being deleted from the data table, thus nothing is changing. 

    Here is the code:

    public DataTable DeleteFromDataTable(DataTable dt) { dt.AcceptChanges(); for (int i = 1; i < dt.Rows.Count; i++) { //string date = dt.Rows[i]["dt"].ToString(); //DateTime parsedDate = DateTime.ParseExact(date, "yyyy-MM-dd HH:mm:ss.fffffff", null); var array1 = dt.Rows[i].ItemArray; var array2 = dt.Rows[i - 1].ItemArray;

    //Same store number if (array1[0].Equals(array2[0])) { //Percent has changed if (array1[3] == array2[3]) { Console.WriteLine("percent changed"); i++; // skip over one to keep the data } else { Console.WriteLine("percent unchanged"); dt.Rows[i - 1].Delete(); } } } dt.AcceptChanges(); return dt; }



    Monday, July 16, 2018 5:40 PM

Answers

  • You don't want to call AcceptChanges… all that does is update the current value in the DataTable.

    You want to use your data adapter to perform an update operation with the values in that datatable.  The data adapter (table adapter) will take care of calling accept changes after they have been successfully committed to the database.

    Also note that you've posted C# code to the VB.Net language forum.  I'll move this to the C# forum since that seems to be the language you are working with.


    Reed Kimble - "When you do things right, people won't be sure you've done anything at all"

    • Marked as answer by jakedingers Monday, July 16, 2018 9:09 PM
    Monday, July 16, 2018 6:38 PM
    Moderator

All replies

  • You don't want to call AcceptChanges… all that does is update the current value in the DataTable.

    You want to use your data adapter to perform an update operation with the values in that datatable.  The data adapter (table adapter) will take care of calling accept changes after they have been successfully committed to the database.

    Also note that you've posted C# code to the VB.Net language forum.  I'll move this to the C# forum since that seems to be the language you are working with.


    Reed Kimble - "When you do things right, people won't be sure you've done anything at all"

    • Marked as answer by jakedingers Monday, July 16, 2018 9:09 PM
    Monday, July 16, 2018 6:38 PM
    Moderator
  • Use this model 

    private void button2_Click(object sender, EventArgs e)
    {
        var dt = new DataTable();
        dt.Columns.Add(new DataColumn() { ColumnName = "FirstName" });
    
        dt.Rows.Add("Mary");
        dt.Rows.Add("Karen");
        dt.Rows.Add("Jone");
    
        Console.WriteLine(dt.Rows.Count);
        demo(ref dt);
        Console.WriteLine(dt.Rows.Count);
    
    }
    
    private DataTable demo(ref DataTable dt)
    {
        for (int i = 0; i < dt.Rows.Count -1; i++)
        {
            if (dt.Rows[i].Field<string>("FirstName") == "Karen")
            {
                dt.Rows[i].Delete();
            }
        }
    
        return dt;
    }

    Or this model

    private void button2_Click(object sender, EventArgs e)
    {
        var dt = new DataTable();
        dt.Columns.Add(new DataColumn() { ColumnName = "FirstName" });
    
        dt.Rows.Add("Mary");
        dt.Rows.Add("Karen");
        dt.Rows.Add("Jone");
    
        Console.WriteLine(dt.Rows.Count);
        var returningTable = demo(dt);
        Console.WriteLine(returningTable.Rows.Count);
    
    }
    
    private DataTable demo(DataTable dt)
    {
        for (int i = 0; i < dt.Rows.Count -1; i++)
        {
            if (dt.Rows[i].Field<string>("FirstName") == "Karen")
            {
                dt.Rows[i].Delete();
            }
        }
    
        return dt;
    }
    Both have three records to start then 2 on returning from Demo method.


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    Monday, July 16, 2018 6:42 PM
    Moderator
  • You don't want to call AcceptChanges… all that does is update the current value in the DataTable.

    You want to use your data adapter to perform an update operation with the values in that datatable.  The data adapter (table adapter) will take care of calling accept changes after they have been successfully committed to the database.

    Also note that you've posted C# code to the VB.Net language forum.  I'll move this to the C# forum since that seems to be the language you are working with.


    Reed Kimble - "When you do things right, people won't be sure you've done anything at all"

    @ReedKimble , 

    I haven't used a data adapter anywhere in using the data table, to get the database I used

    DataTable.load(cmd.ExecuteReader());

    So what I am getting at is I don't know what you mean by use a data adapter to update. I'm fairly new to c# 


    Monday, July 16, 2018 6:57 PM
  • Use this model 

    private void button2_Click(object sender, EventArgs e)
    {
        var dt = new DataTable();
        dt.Columns.Add(new DataColumn() { ColumnName = "FirstName" });
    
        dt.Rows.Add("Mary");
        dt.Rows.Add("Karen");
        dt.Rows.Add("Jone");
    
        Console.WriteLine(dt.Rows.Count);
        demo(ref dt);
        Console.WriteLine(dt.Rows.Count);
    
    }
    
    private DataTable demo(ref DataTable dt)
    {
        for (int i = 0; i < dt.Rows.Count -1; i++)
        {
            if (dt.Rows[i].Field<string>("FirstName") == "Karen")
            {
                dt.Rows[i].Delete();
            }
        }
    
        return dt;
    }

    Or this model

    private void button2_Click(object sender, EventArgs e)
    {
        var dt = new DataTable();
        dt.Columns.Add(new DataColumn() { ColumnName = "FirstName" });
    
        dt.Rows.Add("Mary");
        dt.Rows.Add("Karen");
        dt.Rows.Add("Jone");
    
        Console.WriteLine(dt.Rows.Count);
        var returningTable = demo(dt);
        Console.WriteLine(returningTable.Rows.Count);
    
    }
    
    private DataTable demo(DataTable dt)
    {
        for (int i = 0; i < dt.Rows.Count -1; i++)
        {
            if (dt.Rows[i].Field<string>("FirstName") == "Karen")
            {
                dt.Rows[i].Delete();
            }
        }
    
        return dt;
    }
    Both have three records to start then 2 on returning from Demo method.


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    Thanks, but I already have the correct approach to deleting rows out of my data table, its just not seeming to change the data in the table. IF you look at my code, I use something similar to the 2nd demo

    Monday, July 16, 2018 6:58 PM
  • In regards to what I have shown, this was not a replacement but to show that the operations works and if this in your way of writing fails then something else is interfering which is not shown, simply as that.

    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    Monday, July 16, 2018 7:21 PM
    Moderator
  • Yes I understand that, I'm trying to figure out what is causing my .Delete() to preform as expected

    Monday, July 16, 2018 7:32 PM
  • Yes I understand that, I'm trying to figure out what is causing my .Delete() to preform as expected

    From what you have presented and validated via setting a break-point I see nothing in that code to cause it not to work.

    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    Monday, July 16, 2018 7:36 PM
    Moderator