Solved

Unable to print each column of a DataRowView to console

Posted on 2006-07-09
7
503 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

A while ago, I was working on a Windows Forms application and I needed a special label control with reflection (glass) effect to show some titles in a stylish way. I've always enjoyed working with graphics, but it's never too clever to re-invent …
Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.
A company’s greatest vulnerability is their email. CEO fraud, ransomware and spear phishing attacks are the no1 threat to a company’s security. Cybercrime is responsible for the largest loss of money to companies today with losses projected to r…

930 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

14 Experts available now in Live!

Get 1:1 Help Now