Solved

VB.Net - DataTable Row Reference

Posted on 2013-01-17
9
676 Views
Last Modified: 2013-01-18
Good Day Experts!

I have a little project that I am working on - VB6 to VB.Net conversion.  The original VB6 code uses a recordset to loop through the records.  Within the loop, the code checks for EOF. If EOF, then the code backs up one record to get data.  

So, now I am trying to figure out how to make that happen in .Net.  

1) Is there a way at the bottom of my For DataTable loop to check if the current row of  
    the DataTable is the last one?
2) Then, if it is the last row of the DataTable, back up a row in the DataTable so I can
    reference that row data to run a Select query?

Thanks for the help,
jimbo99999
0
Comment
Question by:Jimbo99999
  • 5
  • 4
9 Comments
 
LVL 70

Expert Comment

by:Éric Moreau
ID: 38787465
if you use a "For Each x as DataRow in YourDataTable.Rows"instead of a regular For, you don't need to check for EOF
0
 

Author Comment

by:Jimbo99999
ID: 38787649
But I need to check for it so I can do 2).
0
 
LVL 70

Expert Comment

by:Éric Moreau
ID: 38787662
then you can create your own counter and check if it equals YourDataTable.Rows.Count()
0
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 

Author Comment

by:Jimbo99999
ID: 38789010
Thanks

This is where I am at so far with referencing the DataTable items based on row Position in the DataTable.  

sqlRStbl.Rows(sqlRStbl_rowPosition)("DIRECTION").Value

Do I need a .Value or .toString  after the Item reference? If so, how do I know which one to use when?

Thanks,
jimbo99999
0
 
LVL 70

Expert Comment

by:Éric Moreau
ID: 38789054
>>Do I need a .Value or .toString  after the Item reference? If so, how do I know which one to use when?

Very often you don't need because there are some default properties but I personally prefer to always write them.
0
 

Author Comment

by:Jimbo99999
ID: 38789252
Ok, thanks.

One more item and I think I will have enough knowledge to give this a shot here.

Normally when I iterate through a DataTable I use the following:

For Each sqlRSProcessing In sqlRStbl.Rows
     Direction = sqlRSProcessing("DIRECTION").Value
Next

In order to be able to reference previous rows in the DataTable,  Is this what I will have to use to loop through the DataTable and reference the values in a row?

Do While sqlRStbl_rowPosition < (sqlRStbl.Row.Count - 1)
     Direction = sqlRStbl.Rows(sqlRStbl_rowPosition)("DIRECTION").Value
Loop

Thanks,
jimbo99999
0
 
LVL 70

Accepted Solution

by:
Éric Moreau earned 500 total points
ID: 38789273
you want to compare the current value to the previous one?

for i as integer = 0 to sqlRStbl.Row.Count -1
     DirectionCurrent = sqlRStbl.Rows(i)("DIRECTION").Value
     if i > 0 then DirectionPrevious = sqlRStbl.Rows(i-1)("DIRECTION").Value
Loop
0
 

Author Comment

by:Jimbo99999
ID: 38789354
Trying to "mimic" the VB6 behavior is harder than I thought...thanks for the patience.

Ok, I just found out that the original VB6 code was moving previous at the EOF to get the last records data...I am not a VB6 coder and did not realize that VB6 EOF puts you past the end of the data.  So, I htink I am good there.

When inside the VB6 Do Until sqlRS.EOF = True loop, several references are made to sqlRS.MoveNext based on code conditions.  To do that I am envisioning having to increment my RowPosition.  So, thinking out loud, then the next time I hit the end of the Loop my Rowposition will then increment again(which is what I want).

Thanks,
jimbo99999
0
 

Author Closing Comment

by:Jimbo99999
ID: 38793685
Thanks for the dialogue.  I am all set now.
0

Featured Post

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

Suggested Solutions

I think the Typed DataTable and Typed DataSet are very good options when working with data, but I don't like auto-generated code. First, I create an Abstract Class for my DataTables Common Code.  This class Inherits from DataTable. Also, it can …
It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, just open a new email message. In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…

816 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

10 Experts available now in Live!

Get 1:1 Help Now