How to add an empty option for a field

How to add an empty option for a field

FabioRFabioR Posts: 2Questions: 1Answers: 1

Hello,
I've upgraded the datatable editor to the latest version and now I have an error in trying to add an empty option on a select field.

This is the C# script I used before and that it worked fine:

.Field(new Field("Goods.ID_Tank")
.Options("Tanks WHERE Is_Drum = 1 AND Enabled = 1 UNION SELECT 0, '' ", "ID", "Code")

Essentially, I read the ID and CODE columns from the Tanks table as the value/label for the options. However, by performing a UNION with an ID of 0 and an empty string (‘’) as the label, I used to add this value to the selectable options. Now, with the new version, I’m encountering an error of ‘INVALID COLUMN NAME’

I attempted to write it like this:

.Field(new Field("Goods.ID_Tank")
.Options("Tanks WHERE Is_Drum = 1 AND Enabled = 1 UNION SELECT 0 as ID, '' as Code ", "ID", "Code")

but the INVALID COLUMN NAME error persists.... any idea how I can solve this issue?

Thanks!

Ciao.

This question has an accepted answers - jump to answer

Answers

  • allanallan Posts: 63,535Questions: 1Answers: 10,475 Site admin

    The fact that it was working before was definitely an error - sorry. The first parameter passed to the Options() method with that overload is the table name. It will be escaped as a field. It sounds like delimiting of that as the field name wasn't being done before.

    The full API docs for that method are here.

    The Editor .NET libraries don't directly have a UNION option, for that you would need a custom function. You can run a plain SQL statement with the Database.Sql() method which you can then use to build the result object.

    Allan

  • FabioRFabioR Posts: 2Questions: 1Answers: 1
    Answer ✓

    Hi allan,
    thanks for your prompt reply.

    I solved it adding this after the first Options method call:

    field.Options().Add("", "0");

    The problem is that I'm not so good with fluent API, so I converted the whole controller like this:

    var field7 = new Field("Goods_Synonyms.ID_Tank");
    field7.Options("Tanks WHERE Is_Drum = 1 AND Enabled = 1", "ID", "Code");
    field7.Options().Add("", "0");
    field7.SetValue(ID_Tank);
    field7.SetFormatter(Format.NullEmpty());
    model.Field(field7);

    But now it works smoothly...

    Thanks again.

    Ciao.

  • allanallan Posts: 63,535Questions: 1Answers: 10,475 Site admin

    Nice one. It is also worth noting that select has placeholder, placeholderDisabled and placeholderValue options which can be used to insert an empty value into a list if all you need is an extra "empty" option.

    Allan

Sign In or Register to comment.