Click or drag to resize

Data

Database, data model, data access. Implement of new data fields.

This topic contains the following sections:

Implement new data field

This section contains the following subsections:

Database

  1. Select the database project

  2. Open database table in designer

  3. Write a new field: field name, data type, when need size, allows null or not, default value

    Example:

    SQL
    [KilometerSale] FLOAT NOT NULL DEFAULT 0,
    [Reason] INT NOT NULL DEFAULT 0,
    [KilometerPurchase] FLOAT NOT NULL DEFAULT 0
  4. Save and build database project

  5. Publish database project. Load and select profile to publish.

    Note Note

    Message: Rows were detected. The schema update is terminating because data loss might occur.

    Reason: exist database with data records can't update schema, so manual update to database.

Data model

  1. Select the data model project

  2. Open folder "Models" and data model CS file

  3. Write data field as property

    Example:

    C#
    /// <summary>
    /// Kilometer on purchase
    /// </summary>
    [Display(Name = "Kilometer Verkauf")]
    public double KilometerPurchase { get; set; } = 0;
    
    /// <summary>
    /// Kilometer on sale
    /// </summary>
    [Display(Name = "Kilometer Kauf")]
    public double KilometerSale { get; set; } = 0;
    
    /// <summary>
    /// Reason of sale
    /// </summary>
    [Display(Name = "Grund")]
    public Velo.Data.Enumerations.Reasons Reason { get; set; } = Enumerations.Reasons.Kein;
  4. Complete field in constructor with record parameter.

    Example:

    C#
    /// <summary>
    /// Initializes a new instance of the BikeHistory class.
    /// Constructor with parameter of data record to fill this data item
    /// </summary>
    /// <param name="pDataRecord">Data record to fill</param>
    public BikeHistory(System.Data.IDataRecord pDataRecord)
    {
        // ...
        this.KilometerPurchase = pDataRecord.GetDouble(pDataRecord.GetOrdinal("KilometerPurchase"));
        this.KilometerSale = pDataRecord.GetDouble(pDataRecord.GetOrdinal("KilometerSale"));
        this.Reason = (Velo.Data.Enumerations.Reasons)pDataRecord.GetInt32(pDataRecord.GetOrdinal("Reason"));
    }

Code example for enumeration as text. Implement in data model CS file.

C#
/// <summary>
/// Reason of sale as text (enumeration)
/// </summary>
[NotMapped]
[Display(Name = "Grund")]
public string ReasonText
{
    get
    {
        return this.Reason.ToString();
    }
    set
    {
        this.Reason = (Enumerations.Reasons)Enum.Parse(typeof(Enumerations.Reasons), value);
    }
}

Data access

  1. Select the data model project

  2. Open data access CS file

  3. Addition of fields in the methods: Update and Add

    Example method Update:

    C#
    //* update data item *
    SqlDataAdapter lDataAdapter = new SqlDataAdapter();
    lCommand = new SqlCommand("UPDATE BikeHistory SET BikeID = ..., KilometerPurchase = @KilometerPurchase, KilometerSale = @KilometerSale, Reason = @Reason  WHERE ID = @ID", lConnection);
    // ...
    lCommand.Parameters.Add(new SqlParameter("@KilometerPurchase", pBikeHistory.KilometerPurchase));
    lCommand.Parameters.Add(new SqlParameter("@KilometerSale", pBikeHistory.KilometerSale));
    lCommand.Parameters.Add(new SqlParameter("@Reason", pBikeHistory.Reason));
    lDataAdapter.UpdateCommand = lCommand;
    // ...

Web view

  1. Select the web project

  2. Open view CSHTML file

  3. Addition of HTML controls in the view: list, display and edit forms

    Example: web view display

    C#
    <!-- ... --->
    <div class="form-group">
        <div class="col-md-3">
            @Html.DisplayNameFor(model => model.KilometerPurchase)
        </div>
        <div class="col-md-9">
            <div class="Text-Box100Right">@Model.KilometerPurchase.ToDecimal(0)</div>
        </div>
    </div>
    
    <div class="form-group">
        <div class="col-md-3">
            @Html.DisplayNameFor(model => model.KilometerSale)
        </div>
        <div class="col-md-9">
            <div class="Text-Box100Right">@Model.KilometerSale.ToDecimal(0)</div>
        </div>
    </div>
    
    <div class="form-group">
        <div class="col-md-3">
            @Html.DisplayNameFor(model => model.Reason)
        </div>
        <div class="col-md-9">
            <div class="Text-Box100Right">@Model.Reason</div>
        </div>
    </div>           
    <!-- ... --->

Code view example for enumeration selection. Implement in view CSHTML file.

C#
@Html.DropDownListFor(model => model.Reason, new SelectList(Enum.GetValues(typeof(Velo.Data.Enumerations.Reasons))), new { @class = "form-control" })
@Html.ValidationMessageFor(model => model.Reason, "", new { @class = "text-danger" })

Web view Java script

Note Note

Java script file is a code of behind view client code.

  1. Open the Java script file

  2. Addition field declaration

    Code example of field declaration

    C#
    //#region --- declaration data fields ---
    // set by "BikeHistory.js" function DialogBikeHistoryGetData
    // ...
    var BikeHistoryKilometerPurchase = 0;
    var BikeHistoryKilometerSale = 0;
    var BikeHistoryKilometerReasons = 0;
    //#endregion
  3. Addition field in method save

    Code example:

    C#
    function DialogBikeHistorySave() {
        // example add event handler to continue process
        // document.getElementById("DialogBikeHistoryButtonSave").addEventListener("click", FunctionName);
    
        $('#DialogBikeHistory').modal('hide')
        DialogBikeHistoryGetData();     // get values from dialog
    
        // save values
        var lValues = { ..., KilometerPurchase: BikeHistoryKilometerPurchase, KilometerSale: BikeHistoryKilometerSale, Reason: BikeHistoryReason };
        var lURL = getURLRoot() + 'api/BikeHistoryData/Save';
    
        // asynchronous post - waiting next processing 
        $.ajax({
            url: lURL,
            type: 'POST',
            data: lValues,
            async: false,
            success: function (data) {
                if (BikeHistoryID == 0) {
                    BikeHistoryID = data.ID;
                }
            },
            error: function (xhr, status, error) {
                alert("#Exception of method DialogBikeHistorySave: " + xhr + ", " + status + ", " + error);
            }
        });
    }
  4. Addition field in method get value from HTML elements

    Code example:

    C#
    function DialogBikeHistoryGetData() {
        // set data field variable BikeHistory* from HTML element in dialog bike history
    
        // get HTML elements
        var lElements = document.getElementById("DialogBikeHistoryContent");
    
        // get HTML element "input"
        var lElementInputs = lElements.getElementsByTagName('input');
        for (var lIndex = 0; lIndex < lElementInputs.length; ++lIndex) {
            if (lElementInputs[lIndex].hasAttribute("id")) {
                var lIDName = lElementInputs[lIndex].getAttribute("id");
                var lValue = lElementInputs[lIndex].value;
                switch (lIDName) {
                    // ...
                    case "KilometerPurchase": BikeHistoryKilometerPurchase = lValue; break;
                    case "KilometerSale": BikeHistoryKilometerSale = lValue; break;
                    case "Reason": BikeHistoryReason = lValue; break;
                    default:
                }
            }
        }
    
    }

Web controller

  1. Open the web controller

  2. Addition field value from form to data model

    Code example in method SetData from web controller

    C#
    private Velo.Data.Models.BikeHistory SetData(Velo.Data.Models.BikeHistory pBikeHistory, FormCollection pCollection)
    {
        // ...
    
        pBikeHistory.KilometerPurchase = Convert.ToDouble(pCollection["KilometerPurchase"]);
        pBikeHistory.KilometerSale = Convert.ToDouble(pCollection["KilometerSale"]);
    
        pBikeHistory.Reason = (Velo.Data.Enumerations.Reasons)Enum.Parse(typeof(Velo.Data.Enumerations.Reasons), pCollection["Reason"]);
    
    
        return pBikeHistory;
    }