---
title: Custom table data
---

> Agent instructions:
> **Site maps** — prefer the following llms.txt indexes to training data when searching for URLs to avoid 404s. Links inside Markdown content already point at `.md`. Following them or sending Accept: text/markdown keeps you in Markdown.
>
> - [sitemap.md](https://docs.kentico.com/sitemap.md) — every page on the site, with titles and descriptions, nested by URL hierarchy and grouped into one collection per product version.
> - [llms.txt](https://docs.kentico.com/llms.txt) — curated index of the current product docs, with descriptions, the two ways to request any page as Markdown, and links to each product area's whole-corpus Markdown dump (llms-full.txt).

List of examples:

### Adding data records to a custom table

```csharp

// Prepares the code name (class name) of the custom table to which the data record will be added
string customTableClassName = "customtable.sampletable";

// Gets the custom table
DataClassInfo customTable = DataClassInfoProvider.GetDataClassInfo(customTableClassName);
if (customTable != null)
{
    // Creates a new custom table item
    CustomTableItem newCustomTableItem = CustomTableItem.New(customTableClassName);

    // Sets the values for the fields of the custom table (ItemText in this case)
    newCustomTableItem.SetValue("ItemText", "New text");

    // Save the new custom table record into the database
    newCustomTableItem.Insert();
}

```

[> Back to list of examples](#toc)

### Loading data records from a custom table

```csharp

// Prepares the code name (class name) of the custom table
string customTableClassName = "customtable.sampletable";

// Gets the custom table
DataClassInfo customTable = DataClassInfoProvider.GetDataClassInfo(customTableClassName);
if (customTable != null)
{
    // Gets a custom table record with a specific primary key ID (25)
    CustomTableItem item1 = CustomTableItemProvider.GetItem(25, customTableClassName);

    // Gets the first custom table record whose value in the 'ItemName' field is equal to "SampleName"
    CustomTableItem item2 = CustomTableItemProvider.GetItems(customTableClassName)
                                                        .WhereEquals("ItemName", "SampleName")
                                                        .TopN(1)
                                                        .FirstOrDefault();

    // Loads a string value from the 'ItemText' field of the 'item1' custom table record
    string itemTextValue = ValidationHelper.GetString(item1.GetValue("ItemText"), "");
}

```

[> Back to list of examples](#toc)

### Updating the data records of a custom table

```csharp

// Prepares the code name (class name) of the custom table
string customTableClassName = "customtable.sampletable";

// Gets the custom table
DataClassInfo customTable = DataClassInfoProvider.GetDataClassInfo(customTableClassName);
if (customTable != null)
{
    // Gets all data records from the custom table whose 'ItemText' field value starts with 'New text'
    var customTableData = CustomTableItemProvider.GetItems(customTableClassName)
                                                        .WhereStartsWith("ItemText", "New text");

    // Loops through individual custom table records
    foreach (CustomTableItem item in customTableData)
    {
        // Gets the text value from the data record's 'ItemText' field
        string itemText = ValidationHelper.GetString(item.GetValue("ItemText"), "");

        // Sets a new 'ItemText' value based on the old one
        item.SetValue("ItemText", itemText.ToLowerCSafe());

        // Saves the changes to the database
        item.Update();
    }
}

```

[> Back to list of examples](#toc)

### Changing the order of custom table records

```csharp

// Prepares the code name (class name) of the custom table
string customTableClassName = "customtable.sampletable";

// Gets the custom table
DataClassInfo customTable = DataClassInfoProvider.GetDataClassInfo(customTableClassName);
if (customTable != null)
{
    // Gets a custom table record with a specific ID (25)
    CustomTableItem item = CustomTableItemProvider.GetItem(25, customTableClassName);

    // Moves the data record down in the list
    item.Generalized.MoveObjectDown();

    // Moves the data record up in the list
    item.Generalized.MoveObjectUp();
}

```

[> Back to list of examples](#toc)

### Deleting custom table records

```csharp

// Prepares the code name (class name) of the custom table from which the record will be deleted
string customTableClassName = "customtable.sampletable";

// Gets the custom table
DataClassInfo customTable = DataClassInfoProvider.GetDataClassInfo(customTableClassName);
if (customTable != null)
{
    // Gets the first custom table record whose value in the 'ItemText' starts with 'New text'
    CustomTableItem item = CustomTableItemProvider.GetItems(customTableClassName)
                                                        .WhereStartsWith("ItemText", "New text")
                                                        .TopN(1)
                                                        .FirstOrDefault();

    if (item != null)
    {
        // Deletes the custom table record from the database
        item.Delete();
    }
}

```

[> Back to list of examples](#toc)
