Solved

VB.Net How to reference  rs.EOF , Move First, Move Last, Move Next like in ACCESS

Posted on 2009-07-13
10
8,020 Views
Last Modified: 2012-05-07
Converting a Access project over to VB.Net ..

This is their code in Access

       strPayoutACC = "SELECT statement"
        Set rsInvoice = db.OpenRecordset(strPayoutACC, dbOpenDynaset, dbSeeChanges)
        If rsInvoice.RecordCount > 0 Then
            Do Until rsInvoice.EOF
 -------------------------------------------------------------------------    
This is mine converted to Vb.Net

            strPayOutACC = "SELECT Statement"
            da4 = New SqlDataAdapter(strPayOutACC, con)
            da4.Fill(ds4, "PayOutAcc")
            lnCount = ds4.Tables(0).Rows.Count

If lnCount > 0 Then

How do you say do something until the end of the file in VB.Net??? Also how would move it to the first record in dataset..

Any help or insight would be much appreciated...
0
Comment
Question by:nomar2
[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
10 Comments
 
LVL 5

Expert Comment

by:Aaronroach
ID: 24842752
for each row as datarow in ds4.tables.rows
'do something
Next
0
 
LVL 28

Expert Comment

by:strickdd
ID: 24842758
Try the following, it is the .Net way of doing things
Dim connection As SqlConnection = new SqlConnection(myConnectionString)
Dim command As SqlCommand = new SqlCommand("SELECT STATEMENT", connection)
Dim reader As SqlDataReader
 
connection.Open()
reader = command.ExecuteReader()
 
while(reader.Read)
{
}
 
connection.Close()

Open in new window

0
 
LVL 21

Expert Comment

by:Craig Wagner
ID: 24842771
An ADO.NET DataSet/DataTable is fundamentally different than an ADO RecordSet.

If you want to loop over the rows in the DataTable, use a For Each loop:

        For Each row As DataRow In ds4.Tables(0).Rows
           ' get values from the row using row(columnIndex) or row("columnName")
        Next

You don't need to 'move' to the first row. If you want to reference the first row, just use ds4.Tables(0).Rows(0).
0
Technology Partners: 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!

 
LVL 8

Expert Comment

by:Bob Hoffman
ID: 24842791
Use a DataReader nto an Adapter
Dim strFieldName as string = ""
 
Dim oCmd As New SqlCommand("SELECT Statement", con)
Dim oDr As SqlDataReader = oCmd.ExecuteReader
While oDr.Read()
  strFieldName = oDr("FieldName") 
End While
oDr.Close()

Open in new window

0
 

Author Comment

by:nomar2
ID: 24843055
The data reader seems to be what I was looking for...is there any bulit functionality to get the totals records as I refernced earlier when using the dataset
0
 
LVL 8

Expert Comment

by:Bob Hoffman
ID: 24843150
The dataReader doesn't have a count. If you need the count on first read you could add it as a sub-select in your SQL statement. If that's not feesible you may need to user your dataAdaptor and load the records into a DataSet or Table and get your count there.
0
 

Author Comment

by:nomar2
ID: 24843377
Within this code I have inner blocks of code based on the variable coming from the outer blocks of code...can I have multiple datareaders open at once...I have the following...

            Dim oCmd As New SqlCommand(strPayOutACC, con)
            Dim oDr As SqlDataReader = oCmd.ExecuteReader
            While oDr.Read()
                lnStatusID = oDr("status")

                If (lnStatusID = 3) Or (lnStatusID = 5) Then
                    lInvoice = oDr("invoicenumber")
                    'write the header
                    strPayOut = "SELECT statement"

                    Dim oCmd1 As New SqlCommand(strPayOut, con)
                    Dim oDr1 As SqlDataReader = oCmd1.ExecuteReader
                    While oDr1.Read()
                        a = a + 1
                    End While

.but I get ..

"There is already an open DataReader associated with this Command which must be closed first."

Can I have multiple datareaders open??
0
 
LVL 8

Expert Comment

by:Bob Hoffman
ID: 24843434
No you cant, You need a seperate connection object for each dataReader.
0
 
LVL 8

Accepted Solution

by:
Bob Hoffman earned 161 total points
ID: 24843489
Might be cleaner to use a DataSet
Dim strFieldName as string
Dim oData As SqlDataAdapter
Dim oDs As DataSet
 
 
oData = New SqlDataAdapter("Select Statement", con)
oData.Fill(dsLineItems, "Results")
 
Dim row As DataRow
For Each row in oDs.Tables(0).Rows
  strFieldName =  row("FieldName")  
Next
 
oDs.Close

Open in new window

0
 

Author Comment

by:nomar2
ID: 24843762
I will look at this again... when I did what you said up above ... it would only spit out the last record when I used datasets...it ignores all the other rows..

I will fix up and post some code..maybe you can see something obivious

0

Featured Post

Technology Partners: 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

Well, all of us have seen the multiple EXCEL.EXE's in task manager that won't die even if you call the .close, .dispose methods. Try this method to kill any excels in memory. You can copy the kill function to create a check function and replace the …
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 …
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…
Finding and deleting duplicate (picture) files can be a time consuming task. My wife and I, our three kids and their families all share one dilemma: Managing our pictures. Between desktops, laptops, phones, tablets, and cameras; over the last decade…

734 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