how to insert, update and delete in datatable without database using c#

NazHim 206 Reputation points
2021-05-14T11:13:52.133+00:00

Hi All

how to insert, update and delete in datatable without using database in c#

i'am tried to insert row. it's working perfectly
but tried to update existing value unfortunately failed.
stuck proccessing.
used code attaching below

foreach (DataRow dr in Items.Rows)
{
     if (dr["IndId"].ToString() == indid)
     {
            // update
            dr["IndQty"] += 1;
            break;
     }
     else
     {
          insertNewRow();
     }
}

if have only one row. updated currently. have if more than a row. not updating. processing stuck
also problem inserting too. have if more than two rows
how can do this?

with best regards
NazHim

Developer technologies | C#
0 comments No comments
{count} votes

Accepted answer
  1. Viorel 122.6K Reputation points
    2021-05-14T14:01:30.22+00:00

    To update multiple rows, remove break, according to previous suggestions.

    You probably want to insert a new row if the row does not exist, then consider an approach like this:

    bool found = false;
    
    foreach (DataRow dr in Items.Rows)
    {
       if (dr["IndId"].ToString() == indid)
       {
          found = true;
    
          // update
          . . .
          break;
       }
    }
    
    if( ! found ) insertNewRow();
    

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.