Question

tl;dr: I want to update ONE DataTable row and then re-cache this whole DataTable(The DataTable not just the row) so I can use it later on

I'm using a cache to store a large DataTable that I've filled with a SqlAdapter based on my MSSQL database

I use this cache to get the DataTable then use it to display a table inside a webpage, nothing odd here.

But this DataTable contains a list of users and I want to be able to edit these users(edit them in the MSSQL database that is) which is easy to do.

The issue is that after each SQL Update you have to re-cache the DataTable(otherwise it'll only be updated in the database but not in the DataTable/webpage) and since it is very large it's very annoying and makes the very simple user update take a very long time since it'll also have to a SQL SELECT to get all posts and then re-cache it

Because of this I want to update that specific row in the DataTable directly after doing my SQL update, this way I don't have to re-fetch the whole SQL table (It is the SQL SELECT part that takes a while since it is so large)

So far I've done this

//We just updated our user, now we'll fetch that with SQL and put it in a new fresh DataTable(that contains just that one row) - since this is much faster than getting the whole table
//Then we'll use that DataTable containing one fresh row to update our old DataTable and re-cache it
DataTable newDataTable = getUserByID(userID); //Get our just edited DataTable row
DataTable cachedDataTable = getSetUserCache(); //Get our cached DataTable

DataRow oldRow = cachedDataTable.Select(string.Format("id = {0}", userID)).FirstOrDefault(); //Get the old row that contains the correct ID

string test = oldRow["status"].ToString(); //Contains the old and cached value before it got edited

oldRow = newDataTable.Rows[0]; //Update the old row with the new row

string test2 = oldRow["status"].ToString(); //Now it contains the new edited value

//Here I should update the cachedDataTable with the new row updated row

DataRow oldRowAfterUpdated = cachedDataTable.Select(string.Format("id = {0}", userID)).FirstOrDefault(); //Get the old row that now should be updated but isn't

string test3 = oldRowAfterUpdated["status"].ToString(); //Still contains the old and cached value before it got edited

success = updateUserCache(cachedDataTable); //Update the DataTable cache that we'll be using later on

I only see posts on how you update the rows, but how do you actually update the DataTable itself with the new row?

Solution :

cachedDataTable.Select(string.Format("id = {0}", userID)).FirstOrDefault().ItemArray = newDataTable.Rows[0].ItemArray;
Was it helpful?

Solution

I think that you may use ItemArray property of DataRow:

void Main()
{
    DataTable tableOld = new DataTable();
    tableOld.Columns.Add("ID", typeof(int));
    tableOld.Columns.Add("Name", typeof(string));

    tableOld.Rows.Add(1, "1");
    tableOld.Rows.Add(2, "2");
    tableOld.Rows.Add(3, "3");

    DataTable tableNew = new DataTable();
    tableNew.Columns.Add("ID", typeof(int));
    tableNew.Columns.Add("Name", typeof(string));

    tableNew.Rows.Add(1, "1");
    tableNew.Rows.Add(2, "2");
    tableNew.Rows.Add(3, "33");

    tableOld.Rows[2].ItemArray = tableNew.Rows[2].ItemArray; //update specific row of tableOld with new values

    //tableOld.Dump();
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top