Solved

Unable to print each column of a DataRowView to console

Posted on 2006-07-09
7
506 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

 

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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Help with preventing downloading a zip file 10 49
MailAddress in vb 4 36
How to share My.Settings in multiple application instances ? 7 53
Access/Visual Basic Question 3 43
This tutorial demonstrates one way to create an application that runs without any Forms but still has a GUI presence via an Icon in the System Tray. The magic lies in Inheriting from the ApplicationContext Class and passing that to Application.Ru…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Are you ready to implement Active Directory best practices without reading 300+ pages? You're in luck. In this webinar hosted by Skyport Systems, you gain insight into Microsoft's latest comprehensive guide, with tips on the best and easiest way…

733 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