Problems counting the number of rows in DataTable

Posted on 2013-10-29
Medium Priority
Last Modified: 2013-10-30

I am currently trying to create a function to count the number of rows held in a DataTable however I keep getting an Object reference not set to an instance of an object error

 Function itemcount() As Integer

        Dim itemcounter As Integer = 0

        For Each Me.objDR In objDT.Rows
            itemcounter = itemcounter + 1

        Return itemcounter

    End Function

Open in new window

Can anyone help with this please?
Question by:TPLLimited

Accepted Solution

dustock earned 2000 total points
ID: 39609943
Why not just use objDT.Rows.Count.ToString instead of using a foreach?
LVL 71

Expert Comment

by:Éric Moreau
ID: 39610401
I agree that you should really use the Count method.

>>For Each Me.objDR In objDT.Rows

your iterator (objDR) should be a new variable:

For Each dr as DataRow In objDT.Rows

Featured Post

Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
An ASP.NET Web Form User Control is not newly introduced in ASP.NET. In fact, it was an old technology yet still playing a role to generate web content, especially when we want to use it to have a better and easy way to control part of the web conte…
This video tutorial shows you the steps to go through to set up what I believe to be the best email app on the android platform to read Exchange mail.  Get the app on your phone: The first step is to make sure you have the Samsung Email app on your …
In the video, one can understand the process of resizing images in single or bulk. Kernel Bulk Image Resizer is an easy to use tool for resizing large number of images. One can add and resize multiple images with this tool in single go. The video sh…

624 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