none
How to verify if a Numeric field is null or empty or nothing RRS feed

  • Question

  • If I want to verify if a field that has an ID number or currency is empty or null or nothing, I don't know which code to use.

    When I use string.IsNullorEmpty I get an error message "this is not a string".

    If I use (field).toString.length>0  then I get an error message "this is not a string".

    If Not IsDBNull it does not give accurate results, because it displays even fields that are empty.

    I look forward to your quick reply.

    Wednesday, January 15, 2020 6:56 PM

Answers

  • i treid

    If (DSRptC.Tables("PrprRS").Rows(XsRpt).Item("PrId").ToString) Is DBNull.Value Then

    i got this error

    Exception thrown: 'System.NullReferenceException' in ...exe
    Additional information: Object reference not set to an instance of an object.

     
    • Marked as answer by MosheS Singer Thursday, January 23, 2020 9:30 PM
    Tuesday, January 21, 2020 7:07 PM

All replies

  • the Code: If (VGS_5(VGS_5.Row, "Refund-Exp").Text).ToString.Length > 0 Then

    The Error:

    Exception thrown: 'System.MissingMemberException' in Microsoft.VisualBasic.dll
    Additional information: Public member 'Text' on type 'DBNull' not found.

    Wednesday, January 15, 2020 7:06 PM
  • Hello,

    What exactly are you test with e.g. a column in a DataTable as you can check for null as follows

    dim dt as new DataTable
    dt.Columns.Add(New DataColumn() With{.ColumnName = "Column1", .DataType = GetType(Integer)})
    
    dt.Rows.Add(new Object() {1})
    dt.Rows.Add(new Object() {nothing})
    dt.Rows.Add(new Object() {nothing})
    dt.Rows.Add(new Object() {nothing})
    dt.Rows.Add(new Object() {12})
    
    For rowIndex As Integer = 0 To dt.Rows.Count -1
        If dt.Rows(rowIndex)("Column1") is DBNull.Value
            Console.WriteLine(rowIndex)
        End If
    Next

    Since 0 is the default value we can use this

    dim dt as new DataTable
    dt.Columns.Add(New DataColumn() With{.ColumnName = "Column1", .DataType = GetType(Integer)})
    
    dt.Rows.Add(new Object() {1})
    dt.Rows.Add(new Object() {nothing})
    dt.Rows.Add(new Object() {nothing})
    dt.Rows.Add(new Object() {nothing})
    dt.Rows.Add(new Object() {0})
    
    For rowIndex As Integer = 0 To dt.Rows.Count -1
        If dt.Rows(rowIndex)("Column1") is DBNull.Value OrElse  dt.Rows(rowIndex).Field(of Integer)("Column1") = 0
            Console.WriteLine(rowIndex)
        End If
    Next

    If the field from a database table is nullable then you can use FieldName.HasValue then if there is a value use .Value to get the actual value.

    Then we have

    Dim myVar? As Integer = Nothing
    Console.WriteLine(If(myVar, 7))


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange




    Wednesday, January 15, 2020 7:51 PM
    Moderator
  • i treid

    If (DSRptC.Tables("PrprRS").Rows(XsRpt).Item("PrId").ToString) Is DBNull.Value Then

    i got this error

    Exception thrown: 'System.NullReferenceException' in ...exe
    Additional information: Object reference not set to an instance of an object.

     
    • Marked as answer by MosheS Singer Thursday, January 23, 2020 9:30 PM
    Tuesday, January 21, 2020 7:07 PM
  • i treid

    If (DSRptC.Tables("PrprRS").Rows(XsRpt).Item("PrId").ToString) Is DBNull.Value Then

    i got this error

    Exception thrown: 'System.NullReferenceException' in ...exe
    Additional information: Object reference not set to an instance of an object.

     


    What if you tried

    If DSRptC.Tables("PrprRS").Rows(XsRpt).Item("PrId") Is DBNull.Value Then


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange




    Tuesday, January 21, 2020 7:37 PM
    Moderator