Solved

Unable to print each column of a DataRowView to console

Posted on 2006-07-09
7
504 Views
Last Modified: 2006-11-18
Fairly simple request...

I have an array of DataRowViews, based upon a MyDataView.FindRows search (FirstRunDataRowViews()).

The above array a looped for a second search criteria. Every time a condition becomes true, the whole
dataRowView that meets the second criteria is put into a second DataViewArray (SecondRunDataRowViews())



The code:

Dim FirstRunDataRowViews() As DataRowView = Me.dvContracts.FindRows("AllRowsContainingThisString")
Dim SecondRunDataRowViews() As DataRowView = New DataRowView(0) {}

        'If one or more rows were found....
        If FirstRunDataRowViews.Length > 0 Then
            ' Loop through the FirstRunDataRowViews array.
            Dim dataRowViewType As DataRowView
            Dim iteration As Integer = 0
            For Each dataRowViewType In FirstRunDataRowViews
                If SearchCondition1IsTrue And SearchCondition2IsTrue Then
                    'Row Found
                    'Increase the size of SecondRunDataRowViews by 1, then add this DataRowView
                    ReDim Preserve SecondRunDataRowViews(SecondRunDataRowViews.GetUpperBound(0) + 1)
                    SecondRunDataRowViews(iteration) = dataRowViewType
                 End If
                iteration += 1
            Next
         End If

Then for each row in the array SecondRunDataRowViews(), I want to print to console each of the columns in each of the
remaining rows.

The Code:

  ' Loop through the SecondRunDataRowViews array.
        Dim dataRowViewType As DataRowView
        Dim iteration As Integer = 0
        If SecondRunDataRowViews.Length > 0 Then
            For Each dataRowViewType In SecondRunDataRowViews
                For y As Integer = 0 To MyDataSet.MyTable.Columns.Count - 1      'How can I find the no. of columns in a DataRowView?
                    If Not dataRowViewType(iteration) Is DBNull.Value Then     'This is where is crashes
                        Debug.Write(dataRowViewType(iteration).ToString)
                    Else
                        Debug.Write(" ? ")
                    End If
                    Debug.Write(ControlChars.CrLf)
                 Next
                iteration += 1
            Next
          End If

I keep getting a  'System.NullReferenceException' thrown every time dataRowViewType is encountered.

Any ideas please?
0
Comment
Question by:IvanHowarth
  • 3
  • 2
  • 2
7 Comments
 
LVL 96

Expert Comment

by:Bob Learned
ID: 17067913
What is 'iteration'?  

Shouldn't it be this?
    If Not dataRowViewType(y) Is DBNull.Value Then    

You don't need that check (unless you are looking for null values), since ToString will return an empty string for nulls:

Bob
0
 

Author Comment

by:IvanHowarth
ID: 17068080
iteration is just how many times it has looped. Unlike a traditional 'For-Next' loop, I don't know how to tell which loop it is in when using a 'For Each-In' loop. Would be really helpful if you could shine some light on this too.

Point taken with the null values - I'll remove it.
0
 
LVL 34

Accepted Solution

by:
Sancler earned 500 total points
ID: 17068782
Your "iteration" variable is, in effect, a "rowcounter".  As you say, a For Each approach does not provide one, so you have to "roll your own".

If you really want a system-produced one then you could use

            For Each iteration As Integer = 0 To SecondRunDataRowViews.Length - 1

instead of

            For Each dataRowViewType In SecondRunDataRowViews

If you did that you would need the iteration variable to be order to access specific DataRowView object instances by index within SecondRunDataRowViews - like this

    SecondRunDataRowViews(iteration)

And then, because you want to reference specific fields/columns within that specific instance of the DataRowView, you would need to do that with the field/column index - like this

    SecondRunDataRowViews(iteration).Item(y)

or, because Item is the default property of DataRowView, like this

    SecondRunDataRowViews(iteration)(y)

But as you are using

            For Each dataRowViewType In SecondRunDataRowViews

that returns, on each pass through the loop, the next in the series of instances of the DataRowView object that are in SecondRunDataRowViews you have no need to access the instances by index in the array.  In this case, you don't need an "iteration" variable.   On every pass through the loop dataRowViewType is a specific instance, so all you need to do to get at a specific field/column within that is

    dataRowViewType.Item(y)

or

    dataRowViewType(y)

In answer to your question

                For y As Integer = 0 To MyDataSet.MyTable.Columns.Count - 1     'How can I find the no. of columns in a DataRowView?

that's the right way to do it - assuming that that is the table from which the DataRowView is drawn.  A DataRowView has, so far as I know, no property/method of its own which exposes the count of its Items.

Finally - and off the topic of you actual question - this looks an awfully complicated way of achieving what you want.  Are your three search criteria - the one from MyDataView.FindRows search and the two (SearchCondition1 and SearchCondition2) from your first block of code - particularly complex?  So complex, indeed, that you cannot put them all into a single filter expression and use .Select directly on the datatable rather than (a) getting a dataview from the datatable (b) filtering that into an array of datarowviews and (b) further filtering that array into another array of datatowviews?

Roger
0
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 

Author Comment

by:IvanHowarth
ID: 17068878
Roger,

Thanks for the trouble for your answer, I'll look at it properly tomorrow.

As for your final part, lol :-0 , story of my life! I appreciate the complexity of what I have done (and actually does have some merits for this project), nevertheless and after many hrs, I have got it working seamlessly and my original problem over come. I'll post what was wrong again tomorrow.

As always, you comments are greatly valued.

Ivan
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 17068962
Roger, that was a very long and detailed comment, showing just what I was trying to describe.

Bob
0
 
LVL 34

Expert Comment

by:Sancler
ID: 17069031
Bob

I do go on a bit, don't I ;-)  And even then I got it wrong ;-(  I actually meant to say

            For iteration As Integer = 0 To SecondRunDataRowViews.Length - 1

But For ... Next and For Each ... Next can be confusing and a "worked example" seemed worthwhile.  And - Sunday afternoon, dog walked, dinner not ready yet - it seemed an ideal way of passing the time.

Roger
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 17069038
Roger,

You're a lawyer, I would expect that words would have been your trade ;)

Bob
0

Featured Post

ScreenConnect 6.0 Free Trial

Check out the updates in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI that improves session organization and overall user experience. See the enhancements for yourself!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Article by: Kraeven
Introduction Remote Share is a simple remote sharing tool, enabling you to see, add and remove remote or local shares. The application is written in VB.NET targeting the .NET framework 2.0. The source code and the compiled programs have been in…
Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

773 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question