Postgres Null Question

I have a varchar field “modifiers” set to DO NOT ALLOW NULL and set the default value to ‘’ (blank string).

It looks like Xojo is still trying to put in a null value with this code:

[code] If Not(Listbox5.CellTag(i,1) = Nil) then
row.Column(“modifiers”) = Listbox5.CellTag(i,1)
End If


and even this code:

[code] If Not(Listbox5.CellTag(i,1) = Nil) then
row.Column(“modifiers”) = Listbox5.CellTag(i,1)
row.Column(“modifiers”) = “”
End If


Did I make a mistake somewhere?

You can simplify the second version to:

row.Column("modifiers") = Listbox5.CellTag(I, 1).StringValue.Trim

but this issue doesn’t make sense to me. Could it be that you are storing a blank string in your CellTag? What if you change the code to this:

if Listbox5.CellTag(i, 1).StringValue <> "" then
  row.Column("modifiers") = Listbox5.CellTag(i, 1).StringValue
end if

myDbase.InsertRecord("payments", row)

Also, FYI, you can check a variant for nil with if v.IsNull ....

Kem, that worked. Not exactly sure why, but it did.

[quote=390919:@Kem Tekinay]if Listbox5.CellTag(i, 1).StringValue <> “” then
row.Column(“modifiers”) = Listbox5.CellTag(i, 1).StringValue
end if[/quote]

This code seems to work to prevent storing null values in the field “modifiers”. Can you explain why?

Does (null value <> “”) produce a true result?

A variant can hold nil as it’s value and that can be converted to any of the intrinsic types. When converted to a string, it is empty, so my code does not differentiate between a variant that holds nil and ones that is simply an empty string.

The way you asked the question, if written in code, would not compile as you cannot compare nil to a string, but a variant auto-converts. To be on the safe side, I specified the .StringValue function so there would be no confusion.

Why are your RowTags not registering as nil when they have no value? That I can’t tell you as I’d have to examine your code in more detail, but I’d say it doesn’t matter at this point. :slight_smile:

Kem, I appreciate the response. I didn’t realize that you could add functions to Listbox CellTags.

I think we are saying the same thing, but let me clarify. The CellTag returns a variant, and the variant has various features that you can use like Type, StringValue, DoubleValue, etc., and IsNull.