Solved

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

Posted on 2009-07-13
10
7,905 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
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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Article by: jpaulino
XML Literals are a great way to handle XML files and the community doesn’t use it as much as it should.  An XML Literal is like a String (http://msdn.microsoft.com/en-us/library/system.string.aspx) Literal, only instead of starting and ending with w…
If you're writing a .NET application to connect to an Access .mdb database and use pre-existing queries that require parameters, you've come to the right place! Let's say the pre-existing query(qryCust) in Access takes a Date as a parameter and l…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial

685 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