# DataSet Operations

## Adding a new row to the dataSet

```csharp
public virtual dsRm.rueckm_afoRow RueckmAfo(dsBab.bab_afoRow afoRow)
{
    // prevent the case when datacache is null
    if (this.DataCache == null)
    {
        LoggerSink.PushMessage(this, "busRM.RueckmAfo fehlgeschlagen: TimeLine Data Cache wird geladen! Bitte versuchen Sie es später erneut.");
        return null;
    }
    var row = tSet.rueckm_afo.NewRow();
    row.lfd_nr = ParaUtils.GetNextRueckmAfoLfdNr();
    row.rm_typ = 10; 
    
    row.datum = DateTime.Now;
    row.erstellt_am = DateTime.Now;
     
    //.....         
    
    return row;
}
```

{% hint style="warning" %}
The primary keys for a new row are set only at saving
{% endhint %}

RowStates:

* For the operations order New->Add->Initialisation the RowState of the new row will be EditingNew
* For the operations order New->Initialisation->Add the RowState of the new row will be New

For all RowStates see corresponding chapter.

## Deleting a row from the dataSet

```csharp
public virtual DeleteAfo(dsBab.bab_afoRow afoRow)        
{
    afoRow.Row.Delete();
}
```

## Duplicate an existing row

```csharp
public virtual DataRow AddNewRueckmFertigart(DataRow fertigRow)
{
    if (fertigRow == null)
    {
        return null;
    }

    this.tSet.rueckm_fertigart.Table.ImportRow(fertigRow);
    return fertigNew;
}
```

## Copy a table

Copy() creates a new DataTable with the same structure and data as the original DataTable.

```csharp
var docPosDetailRows = BusObjDoc.tSet.belpos_detail.Copy();
```

## Ignore change on a column

This is useful when we don’t want to receive a save message when a specific column, that comes from a db compute (not updateable), has changed.

Structure:

```csharp
dSet.IgnoreStateChange(tableName, fieldName);
```

Example:

```csharp
private void SetBestandValues()
{
    dSet.IgnoreStateChange("Versandvorschlag", "cf_bestand");

    foreach (var item in tSet.Versandvorschlag)
    {
        item.cf_bestand = BestandUtils.Instance.GetBestand(item.artnr, item.belpos_lager_nr.ToInt(-1), item.bel_typ, item.bel_nr, item.posnr);
    }
}
```

## Retrieve on one table

```csharp
tSet.Documents.Retrieve(docTyp, docNr);
```

## Save

```csharp
public override int Save()
{      
    //...
    
    foreach (var dataSet in AllChildDataSets)
    {
        dataSet.SetEditCheckpoint();
    }

    int ret = BookRM(commit: false);
    try
    {
        foreach (var dataSet in AllChildDataSets)
        {
            dataSet.CommitEditCheckpoint();
        }
    }
    catch (Exception ex)
    {
        foreach (var dataSet in AllChildDataSets)
        {
            dataSet.RevertEditCheckpoint();
        }
        LoggerSink.PushMessage(this, l.Translate(ex.Message), Severity.Exception);
    }

    return ret;
}
```

dataSet.SetEditCheckpoint() is used for prevent the possibility that an exception to be thrown. In that case, if SetEditCheckpoint()  is not used, the DataBase will return to its previous state, but the DataSet will not.

{% hint style="info" %}
In conclusion, SetEditCheckpoint() is used for rollbacks to be done on DataSets as well.
{% endhint %}
