question

NazHim-9882 avatar image
0 Votes"
NazHim-9882 asked NazHim-9882 commented

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

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


dotnet-csharp
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

1 Answer

Viorel-1 avatar image
0 Votes"
Viorel-1 answered NazHim-9882 commented

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();


· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

hi Viorel-1
thanks

0 Votes 0 ·