Hi Group, I’m trying to display some date data on a table, some fields may be empty… so when it gets to the null date field, I get error. The problem is that I also have to format the date first … if I enter the data read directly in the ListBox with rows.Column(“DataInvoice”).DateTimeValue it works, but if I first format the data in the date variable DataPrimaScadenzaFormattata and insert this in the ListBox and I read a null field I get an error … how can I solve the problem ?
Dim DataPrimaScadenzaFormattata as new date
DataPrimaScadenzaFormattata=rows.Column(“Rata1”).DateTimeValue
ListBoxIngressi.AddRow(DataPrimaScadenzaFormattataShortDate) 'ERROR WITH VALUE=NIL
ListBoxIngressi.AddRow(rows.Column(“Rata1”).DateTimeValue) 'OK WITH VALUE = NIL
Excuse my ignorance, what do you mean? I know it’s due to NIL/NULL value in database field… now I would like to handle it but I don’t know how. If I insert the database field directly I have no problems and if there is no data value it does not insert anything, if instead I format by declaring a date variable, I have problems with the data variable which returns the error on NIL values
// This should work
Var dateFromDB As DateTime = rows.Column(“Rata1”).DateTimeValue
ListBoxIngressi.AddRow( If(dateFromDB Is Nil, "", dateFromDB.ToString(DateTime.FormatStyles.Short, DateTime.FormatStyles.None)) )
// We assume that the Column "Rata1" is a Date or DateTime Column!
If rows.Column(“Rata1”).Value <> Nil Then
Var dateFromDB As DateTime = rows.Column(“Rata1”).DateTimeValue
ListBoxIngressi.AddRow dateFromDB.ToString(DateTime.FormatStyles.Short, DateTime.FormatStyles.None)
Else
ListBoxIngressi.AddRow ""
End If
// The above Code is untested, written without the Xojo IDE.
First we make sure the Field contains a Value and is not NIL/NULL. Then we add the Date to the ListBox or we add an empty Field to the Listbox.
@Rick_Araujo: Your code is perfectly correct, but too compressed for a lesson. My opinion…
Yep, my style usually don’t focus only on a lesson, sometimes around 3 at once, as I’m very oriented towards good memory use, good speed, and terse readable code.
Thanks Rick A
I tried the code and it’s perfect, I didn’t know you could nest so many instructions… great.
Sasha S
I also understood your code and it works, the problem is that I have other lines of code where this possibility repeats and I should insert too many conditions.
Thanks to both of you anyway, I learned something new today See you next time.
I believe this is an opportunity to use an extension method to add functionality to the Database Column Class. Something like:
Public Function FormattedDate(Extends value As DatabaseColumn) As String
If value.Value Is Nil Then
Return ""
End If
Return value.DateTimeValue.ToString(DateTime.FormatStyles.Long, DateTime.FormatStyles.None)
End Function