简体   繁体   中英

Deleting a row from a datatable in C#

I am searching a PDF file for a keyword and returning the pages on which that keyword was found. If the keyword IS FOUND, I'm returning a list of pages and the fileName. However, if the keyword was NOT FOUND in the PDF file, I want to deleted the row in the datatable.

public DataTable dtPubSearchResultsFromFiles(string sqlQuery, string safeKeyword)
{
    // Returns a datatable of publication search results based on PDF files.
    SqlConnection con = new SqlConnection(getConnectionString());
    SqlCommand cmd = new SqlCommand(sqlQuery, con);
    SqlDataAdapter da = new SqlDataAdapter(cmd);
    DataTable dt = new DataTable();
    dt.Columns.Add("Pages", typeof(string));
    da.Fill(dt);
    dt.PrimaryKey = new DataColumn[] { dt.Columns["publicationID"] };

    foreach (DataRow row in dt.Rows)
    {
        //call search function to look for keyword
        List<int> myPages = new List<int>();
        string fileName = row["linkToPublicationPDF"].ToString();
        myPages = ReadPdfFile(fileName, safeKeyword);
        if (myPages.Count > 0)
        {
            string pagelist = "";
                foreach (int page in myPages)
                {
                    pagelist = pagelist + page + "  ";
                }
            row["Pages"] = pagelist;
        }
        else
        {
            //remove/delete the row from the datatable if "myPages.Count" is 0
            dt.Rows.Remove(row);
        }
    }

    return dt;
}

When I add this ("dt.Rows.Remove(row)"), I get this error when the page is called "System.InvalidOperationException: Collection was modified; enumeration operation might not execute."

Suggestions? Comments? Fixes? All are welcome...

Bob

Your code is getting some data from the database such that your program can work with it.

The exception you're getting is because your you're modifying (by removing an element) the collection you're iterating on and that's not possible.

You can solve this by creating a temporary List where you store the rows you want to delete. Once you're done with the iteration you can iterate on the temporary list and remove what you decided you don't want anymore.

var toRemove = new List<DataRow>();
foreach (DataRow row in dt.Rows)
{
    //call search function to look for keyword
    List<int> myPages = new List<int>();
    string fileName = row["linkToPublicationPDF"].ToString();
    myPages = ReadPdfFile(fileName, safeKeyword);
    if (myPages.Count > 0)
    {
        string pagelist = "";
        foreach (int page in myPages)
        {
            pagelist = pagelist + page + "  ";
        }
            row["Pages"] = pagelist;
        }
        else
        {
            //remove/delete the row from the datatable if "myPages.Count" is 0
            toRemove.Add(row);
        }
    }
}
foreach (DataRow row toRemove.Add)
{
    dt.Rows.Remove(row);
}

try a simple Delete

row.Delete();

then after the loop

dt.AcceptChanges();

but it will probably fail
see answer from mario
it may work if it is really only marking the row for deletion

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