Results 1 to 9 of 9

Thread: Rows to list or array select ? linq?

  1. #1

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

    Rows to list or array select ? linq?

    Code:
    Public emailadd As New List(Of String)
        Public Function GetEmailAdd()
            If Table.Rows.Count <= 0 Then
                MsgBox("There is No Data")
            End If
            For Each row As DataRow In Table.Rows
                Dim email As String = (row(txttocol.Text))
                emailadd.Add(email)
            Next
            Return emailadd
        End Function
    I am trying to populate a list of email addresses and what I have is working fine except for when I have a row with a blank field(no email address)

    Not sure the best way to handle this and even if what I am currently doing is the best way to go about my program ie list etc...

    This is how i later use the emails in the list

    Code:
    GetEmailAdd()
               For Each email In emailadd
    I was thinking of only populating with my list of records that contained an email address and avoiding blanks not sure if linq or select statment is best method or another way?

    Can someone help me with a select or linq statement or other idea

    Thanks

  2. #2
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,424

    Re: Rows to list or array select ? linq?

    try this:

    Code:
    For Each row As DataRow In Table.Rows
        if not row(txttocol.Text) is nothing then
            Dim email As String = (row(txttocol.Text))
            emailadd.Add(email)
        end if
    Next

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

    Re: Rows to list or array select ? linq?

    Quote Originally Posted by .paul. View Post
    try this:

    Code:
    For Each row As DataRow In Table.Rows
        if not row(txttocol.Text) is nothing then
            Dim email As String = (row(txttocol.Text))
            emailadd.Add(email)
        end if
    Next
    Assuming that this data has come from a database, that's not going to work. Any row that has no value in that column will contain DBNull.Value, not Nothing.

    Conventional:
    Code:
    Dim columnName = txttocol.Text
    
    For Each row As DataRow In Table.Rows
        If Not row.IsNull(columnName) Then
            emailadd.Add(CStr(row(columnName)))
        End If
    Next
    LINQ:
    Code:
    Dim columnName = txttocol.Text
    Dim emailAddresses = (From row In Table.AsEnumerable()
                          Where Not row.IsNull(columnName)
                          Select row.Field(Of String)(columnName)).ToArray()
    Note that that second code is going to produce an array rather than a List. You should always use an array in preference to a List unless you specifically need a List. In this case, you don't need to call Add because the query creates the array in one go so, unless you need to Add or Remove elsewhere, go with the array.
    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

  4. #4

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

    Re: Rows to list or array select ? linq?

    The data is coming from a txt file that i use textfieldparser to read into DataTable

    The conventional solution does not avoid fields without an email address

    I can not seem to get the The linq solution implemented.

    I get an error object reference not set to instance of object on this line


    GetEmailAdd()
    For Each email In emailAddresses


    On a side note, I dont how to use code tags when replying? what the heck am i missing?

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

    Re: Rows to list or array select ? linq?

    Quote Originally Posted by billboy View Post
    The data is coming from a txt file that i use textfieldparser to read into DataTable

    The conventional solution does not avoid fields without an email address

    I can not seem to get the The linq solution implemented.

    I get an error object reference not set to instance of object on this line


    GetEmailAdd()
    For Each email In emailAddresses
    You're not giving us the whole story. If you're using a TextFieldParser then, if I'm not mistaken, you'll be using a empty String as the email address, not Nothing and not DBNull.Value. Have you actually checked to see the actual source of your NullReferenceException, i.e. EXACTLY where it is thrown and what reference is null at the time? Anyway, to avoid using blank email address values you can simply use String.IsNullOrEmpty in the spot where I've used DataRow.IsNull. As the name suggests, it will detect empty and null Strings.
    Quote Originally Posted by billboy View Post
    On a side note, I dont how to use code tags when replying? what the heck am i missing?
    The buttons have not been added back to the basic editor since the last site upgrade. Tags are just part of the text of your post though, so you can simply type them yourself. If you want to be able to use buttons to add then then you must use the advanced editor.
    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

  6. #6

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

    Re: Rows to list or array select ? linq?

    Code:
    Dim columnName = txttocol.Text
    
            For Each row As DataRow In Table.Rows
                If Not String.IsNullOrEmpty(columnName) Then
    
                    emailadd.Add(CStr(row(columnName)))
    
                End If
            Next
            Return emailadd
    Still getting the empty email

    when i put a break at the creation of the list it shows "" for the field with no email

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

    Re: Rows to list or array select ? linq?

    Code:
    If Not String.IsNullOrEmpty(columnName) Then
    Why are you checking whether the column name is null or empty? I'll tell you why. You read this:
    you can simply use String.IsNullOrEmpty in the spot where I've used DataRow.IsNull
    and followed it blindly without actually thinking about what you were doing. Obviously it's the field in the row with that column name that you want to test, not the column name itself. What are you adding to the list of email addresses?
    Code:
    emailadd.Add(CStr(row(columnName)))
    If that's what you're adding then obviously that is what you have to test to see whether it's null or empty.
    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

  8. #8

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

    Re: Rows to list or array select ? linq?

    Thanks, fyi I wasnt blindly following anything I was actually trying to implement it in correctly

    Perhaps you can help me learn why the linq query isnt working

    Set aside for a moment that it will need to altered for String.IsNullorEmpty

    I get an object reference error when I go to use the array created by the query

    My debugging has led me to discover that the array is created and gone once the GetEmailAdd() function is done

    Code:
    Public emailAddresses As Array
    Public Function GetEmailAdd()
    Dim columnName = txttocol.Text
            Dim emailAddresses = (From row In Table.AsEnumerable() Where Not row.IsNull(columnName) Select row.Field(Of String)(columnName)).ToArray()
    
            Return emailAddresses
    The when i go to get the array in another sub

    For Each email In emailAddresses

    its gone (forgive my incorrect terminology) it doesnt exist

    I am thinking its because i have to declare a "New" Array ? but ihave no idea really I do not understand

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

    Re: Rows to list or array select ? linq?

    In the code in post #8, you have two variables named 'emailAddresses': a member variable, i.e. a field, and a local variable inside the GetEmailAdd method. Inside the method you create an array and assign it to the local variable and then you return the value of that local variable. Judging by post #1, when you call that method you simply discard the value returned by the method and use the member variable. You've never assigned anything to that member variable so it is Nothing, hence the NullReferenceException.

    There's lots of little things going wrong there. Firstly, let's look at this:
    Code:
    Public emailAddresses As Array
    That's a public field. Why is it public? Is there a genuine need for that object to be accessed from outside that class? If not then it shouldn't be public. If so then it still shouldn't be public and you should be exposing it via a public property. Also, you should almost never declare anything as type Array. That field is supposed to refer to a String array and only a String array, so that's the type it should be declared as, i.e. String(). Finally, do you need that field at all? Are you planning on assigning an array to that field and then accessing it from multiple methods? If not then you don't need a field. If you're just going to call that method and use the array there and then then there's no point to the field.

    Now let's look at your method. You should NEVER declare a function without a return type. That function is supposed to return a String array so that's the type it should be declared as. Also, should it be a function at all? If you do need a field then that method should be a subroutine and assign a value to that field. If you don't need a field then get rid of it and use the value returned by the function.
    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