Validation.Unique on table with number or strings (column datataype:nvarchar(250))

Validation.Unique on table with number or strings (column datataype:nvarchar(250))

eyal_hbeyal_hb Posts: 98Questions: 31Answers: 0

Validation.Unique not working when in column of navarcher have
numbers alone or string.. for exmple:

when i try to insert value that already in table its says:
"Conversion failed when converting the nvarchar value 'Not Defined' to data type int."

Answers

  • colincolin Posts: 14,520Questions: 1Answers: 2,481

    Can you post your server-side and client code, please, and if possible, link to your page/test case.

    Colin

  • eyal_hbeyal_hb Posts: 98Questions: 31Answers: 0

    this is my server-side code:

     [AcceptVerbs(HttpVerbs.Get | HttpVerbs.Post)]
            public ActionResult GetDataHotelRating()
            {
                var settings = Properties.Settings.Default;
                var formData = HttpContext.ApplicationInstance.Context.Request;
    
                using (var db = new Database(settings.DbType, settings.DbConnection))
                {
                    var editor = new Editor(db, "HotelRating", "Hotel_Rating_id")
                       .Model<Model.HotelRating>("HotelRating")
                         .Field(new Field("Hotel_Rating_id")
                        .Validator(Validation.Numeric())
                    )
                    .Field(new Field("Hotel_Rating_Name").Xss(false)
                        .Validator(Validation.MaxLen(50))
                        .Validator(Validation.NotEmpty(new ValidationOpts
                        {
                            Message = "חובה להזין דירוג"
                        })).Validator(Validation.Unique(new ValidationOpts { Message = "שם דירוג קיים כבר בטבלה" }, "Hotel_Rating_Name", "HotelRating", db)));
    
                    ////Pre functions
                    editor.PreEdit += (sender, e) => prev_Values = Common.Classes.Functions.getPrevValues(db, editor.Table()[0], "Hotel_Rating_id", e.Id);
                    editor.PreRemove += (sender, e) => prev_Values = Common.Classes.Functions.getPrevValues(db, editor.Table()[0], "Hotel_Rating_id", e.Id);
    
    
                    ////Post functions
                    editor.PostCreate += (sender, e) => Functions._LogChange(db, "רמת כוכבים למלון", "create", e.Id, e.Values, prev_Values);
                    editor.PostEdit += (sender, e) => Functions._LogChange2(db, "רמת כוכבים למלון", "edit", e.Id, e.Values, prev_Values);
                    editor.PostRemove += (sender, e) => Functions._LogChange(db, "רמת כוכבים למלון", "remove", e.Id, e.Values, prev_Values);
    
                    editor.Process(formData.Unvalidated.Form);
                    DtResponse data = editor.Data();
    
    
                    return Json(data, JsonRequestBehavior.AllowGet);
                }
            }
    

    this is my client code:

     var editor; // use a global for the submit and return data rendering in the examples
    
        editor = new $.fn.dataTable.Editor({
    
                ajax:'@Url.Action("GetDataHotelRating", "SystemConstants")',
                table: "#example",
                formOptions: {
                    main: {
                        onBackground: null
                    }
                },
                fields: [
                    {
                        label: "דרוג מלון:",
                        name: "Hotel_Rating_Name",
    
                    }
    
    
    
            ],
            i18n: DTE_i18n
    
    
        });
        // Activate an inline edit on click of a table cell
        $('#example').on('click', 'tbody td:not(:first-child)', function (e) {
            editor.inline(this);
        });
           var table=  $('#example').DataTable({
                dom: "Bfrtip",
                ajax: '@Url.Action("GetDataHotelRating", "SystemConstants")',
                  "language": {
                    "url": "@HttpContext.Current.Application["VirDir"]/media/language/de_HEB.json"
               },
                      columnDefs: [
                    {
                        targets: [2],
                        visible: ('@list.Contains("Edit")'==="True") ? true : false
                    },
                    {
                        targets: [3],
                        visible: ('@list.Contains("Copy")'==="True") ? true : false
                    },
                   {
                        targets: [4],
                        visible: ('@list.Contains("Delete")'==="True") ? true : false
                    },
    
                ],
                columns: [
                    //{
                    //    data: null,
                    //    defaultContent: '',
                    //    className: 'select-checkbox',
                    //    orderable: false
                    //},
                    { data: "Hotel_Rating_id" },
                    { data: "Hotel_Rating_Name" },
                    {
                        data: null,
                        "orderable": false,
                        className: "center",
                        defaultContent: "<button class='btn btn-outline btn-success btn-sm editor_edit' style='border-radius: 40%;margin: auto;display: block'><i class='fas fa-pen' style='margin: 2px -2px 0px -1px;font-size:19px'></i></button>"
    
                    },
    
                    {
                        data: null,
                        className: "center",
                        "orderable": false,
                        defaultContent: "<button class='btn btn-outline btn-primary btn-primary btn-sm editor_duplicate' style='border-radius: 40%;margin: auto;display: block'><i class='fas fa-clone' style='margin: 2px -2px 0px -1px;font-size:19px'></i></button>"
    
                    },
                    {
                        data: null,
                        className: "center",
                        "orderable": false,
                        defaultContent: "<button class='btn btn-outline btn-primary btn-danger btn-sm editor_remove' style='border-radius: 40%;margin: auto;display: block'><i class='fas fa-trash-alt' style='margin: 2px -2px 0px -1px;font-size:19px'></i></button>"
    
                    },
    
                ],
                select: {
                    style: 'os',
                    selector: 'td:first-child'
                },
                buttons: {
                buttons: [
                    { extend: "create", editor: editor, text: "חדש", className:"btn-primary myclassB" },
                    //{
                    //    extend: 'excel',
                    //    exportOptions: {
                    //        orthogonal: null,
                    //        columns: [0, 1, 2, 3, 4, 5]
                    //    },
                    //    className: "btn-outline btn-success",
                    //    text: '<i class="fas fa-file-excel"> יצוא לאקסל</i>',
                    //},
    
                ],
                dom: {
                    button: {
                        tag: "button",
                        className: "btn"
                    },
                }
            }
        });
    
                   //disable Create/excel Button with if condition
        table
            .on('preDraw', function () {
                if ('@list.Contains("Insert")' === "False") {
                    table.buttons('.myclassB').nodes().addClass('hidden');
    
                }
                @*if ('@list.Contains("Excel")' === "False") {
                    table.buttons('.myClassE').nodes().addClass('hidden');
    
                }*@
            });
    
        //call to extrnal javascript For Edit/Remove/Duplicate Funciton
        tableFucntion(table);
    
    
  • eyal_hbeyal_hb Posts: 98Questions: 31Answers: 0

    hey i found that:

    .Field(new Field("Serial")
       .Validator(Validation.Unique<string>(new ValidationOpts
        {
             Message = "An asset with this Serial Number already exists."
         }))
    )
    

    this fixed my problem! thanks

This discussion has been closed.