Solved

Unable to print each column of a DataRowView to console

Posted on 2006-07-09
7
502 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
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 

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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Suggested Solutions

Article by: jpaulino
XML Literals are a great way to handle XML files and the community doesn’t use it as much as it should.  An XML Literal is like a String (http://msdn.microsoft.com/en-us/library/system.string.aspx) Literal, only instead of starting and ending with w…
Introduction When many people think of the WebBrowser (http://msdn.microsoft.com/en-us/library/2te2y1x6%28v=VS.85%29.aspx) control, they immediately think of a control which allows the viewing and navigation of web pages. While this is true, it's a…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

707 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now