Countries


List of examples:

Countries

Creating a new country



// Creates a new country object
CountryInfo newCountry = new CountryInfo();

// Sets the country properties
newCountry.CountryDisplayName = "New country";
newCountry.CountryName = "NewCountry";

// Saves the new country into the database
CountryInfo.Provider.Set(newCountry);

> Back to list of examples

Updating an existing country



// Gets the country
CountryInfo updateCountry = CountryInfo.Provider.Get("NewCountry");
if (updateCountry != null)
{
    // Updates the country properties
    updateCountry.CountryDisplayName = updateCountry.CountryDisplayName.ToLower();

    // Saves the change to the database
    CountryInfo.Provider.Set(updateCountry);
}

> Back to list of examples

Updating multiple countries



// Gets all countries whose name starts with 'NewCountry'
var countries = CountryInfo.Provider.Get().WhereStartsWith("CountryName", "NewCountry");

// Loops through individual countries
foreach (CountryInfo country in countries)
{                   
    // Updates the country properties
    country.CountryDisplayName = country.CountryDisplayName.ToUpper();

    // Saves the change to the database
    CountryInfo.Provider.Set(country);
}

> Back to list of examples

Deleting a country



// Gets the country
CountryInfo deleteCountry = CountryInfo.Provider.Get("NewCountry");

if (deleteCountry != null)
{
    // Deletes the country
    CountryInfo.Provider.Delete(deleteCountry);
}

> Back to list of examples

States

Creating a new state under a country



// Gets the country
CountryInfo country = CountryInfo.Provider.Get("NewCountry");
if (country != null)
{
    // Creates a new state object
    StateInfo newState = new StateInfo();

    // Sets the state properties
    newState.StateDisplayName = "New state";
    newState.StateName = "NewState";
    newState.CountryID = country.CountryID;

    // Saves the new state into the database
    StateInfo.Provider.Set(newState);
}

> Back to list of examples

Updating an existing state



// Gets the state
StateInfo updateState = StateInfo.Provider.Get("NewState");
if (updateState != null)
{
    // Updates the state properties
    updateState.StateDisplayName = updateState.StateDisplayName.ToLower();

    // Saves the change to the database
    StateInfo.Provider.Set(updateState);
}

> Back to list of examples

Updating multiple states



// Gets the parent country of the states
CountryInfo country = CountryInfo.Provider.Get("NewCountry");
if (country != null)
{
    // Gets a collection of states within the parent country
    var states = StateInfo.Provider.Get().WhereEquals("CountryID", country.CountryID);
    
    // Loops through individual states
    foreach (StateInfo state in states)
    {                       
        // Updates the state properties
        state.StateDisplayName = state.StateDisplayName.ToUpper();

        // Saves the change to the database
        StateInfo.Provider.Set(state);
    }
}

> Back to list of examples

Deleting a state



// Gets the state
StateInfo deleteState = StateInfo.Provider.Get("NewState");

if (deleteState != null)
{
    // Deletes the state
    StateInfo.Provider.Delete(deleteState);
}

> Back to list of examples