Results 1 to 6 of 6

Thread: If row .IsNullOrEmpty

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Aug 2009
    Location
    Los Angeles
    Posts
    1,335

    If row .IsNullOrEmpty

    I need some hlep detecteing an empty row

    HTML Code:
    Dim row As DataRow
            row = CType(Form1.DataGridView1.SelectedRows(0).DataBoundItem, DataRowView).Row
    My error here is Option Strict On disallows late binding
    HTML Code:
    If CStr(row("Comp Address").IsNullOrEmpty) Then
                compaddress.Text = " None Available"
            End If
            compaddress.Text = CStr(row("Comp Address"))

  2. #2
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,109

    Re: If row .IsNullOrEmpty

    Row("Comp Address") is returning type Object. How about converting that object to a string and comparing to Null or Empty rather than doing the conversion to string on the return from the method. Alternatively, you could also perform a two step process where the first If checks for Null, then for an empty string. That's not as nice, though.
    My usual boring signature: Nothing

  3. #3

    Thread Starter
    Frenzied Member
    Join Date
    Aug 2009
    Location
    Los Angeles
    Posts
    1,335

    Re: If row .IsNullOrEmpty

    I thought thats what I was doing with

    Cstr(row("Comp Address")

  4. #4
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,109

    Re: If row .IsNullOrEmpty

    Almost, but you put the second ) in the wrong place.

    What I was suggesting looks like this:

    CStr(row("Comp Address")).IsNullOrEmpty

    The problem is that you could also write that using .ToString rather than CStr, and one of the two throws an exception when the value is Null, but I can't remember which. I think CStr is the one you want to use.
    My usual boring signature: Nothing

  5. #5

    Thread Starter
    Frenzied Member
    Join Date
    Aug 2009
    Location
    Los Angeles
    Posts
    1,335

    Re: If row .IsNullOrEmpty

    I had tried that previously, but I get blue squirrley line with message "Access of shared member, constant member or nested type through an instance; qualifying expression will not be evaluated

    This seems to working well:

    Code:
    If row.IsNull("Comp Address") Then
                compaddress.Text = " None Available"
            Else : compaddress.Text = CStr(row("Comp Address"))
            End If

  6. #6
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: If row .IsNullOrEmpty

    IsNullOrEmpty is, as the error message suggests, a Shared member, not an instance member. You should know how to use Shared members by now. You call it on the type and then pass the instance rather than calling it on the instance. Not surprisingly, the documentation has a code example but, not surprisingly, the documentation was apparently ignored.

    First of all, the code you have now will display "None Available" if the field is NULL but not if it is empty. Is that really what you want? Will the address ever actually be empty as opposed to NULL? If you want to replace empty values as well as NULLs then that code will not work. If you only want to replace NULLs then that code can be written more succinctly as:
    vb.net Code:
    1. compaddress.Text = If(row.IsNull("Comp Address"), "None Available", CStr(row("Comp Address"))
    If you want to replace NULL and empty values then you can do this:
    vb.net Code:
    1. Dim address = row("Comp Address").ToString()
    2.  
    3. compaddress.Text = If(String.IsNullOrEmpty(address), "None Available", address)
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width