views:

268

answers:

2

Hello all. VB.NET newbie here.

I've learned (through this site) how to negotiate a dataset via:

For Each dRow In quanDS.Tables(0).Rows
  'do something to each row
Next

I now need to figure out now to loop through a subset of the records returned in the dataset - here's a VB6 example I need to convert:

strSQL = "select * from tblDQ order by xid, xcode, xDOS"
rsMaster.Open strSQL, conDB, adOpenDynamic, adLockOptimistic
rsMaster.MoveFirst

Do While Not rsMaster.EOF
    strxID = Trim(rsMaster!xID)
    strxCode = Trim(rsMaster!xcode)
    dblQuan = rsMaster!units
    Do While Trim(rsMaster!xID) = strxID And Trim(rsMaster!xcode) = strxCode
        rsMaster!unitdif = rsMaster!units - dblQuan
        rsMaster.Update
        dblQuan = rsMaster!units
        rsMaster.MoveNext
        If rsMaster.EOF Then
            Exit Do
        End If
    Loop
Loop

rsMaster.Close

Any help at all would be appreciated!

A: 

You could translate that directly. The rs!fieldName syntax translates to row("fieldName") in VB.NET.

It would be easier if you were using LINQ, though.

John Saunders
I may have to look into LINQ. I'm afraid I don't know enough about the VB.NET layout of the thing to start translating. Don't know how to declare/initialize a recordset, etc.
Dave
You don't declare recordsets. You've already got the DataSet, and your code shows you know how to loop through the rows.
John Saunders
Isn't row!fieldname legal in VB.NET too? I know it is for SQLDataReader
MarkJ
John - the current challenge is the rsMaster.open statement. I'm getting a lot of COM exception errors when trying to open the recordset - the system chokes on both the source and the connection. If I could get the recordset open, I'm great. It's just prying that thing open that's proving to be the challenge...
Dave
@Dave: don't you already have DataSets working? Don't use recordsets at all. Only use DataSet.
John Saunders
@John: Heh - came to that conclusion myself a couple of hours ago. Time to pry my fingers off of the vb6 carcass anyway. This module needs to have nested DataSets both coming from the same source with the inner DataSet modifying the data in the table. Working on getting that going now. Already found out I can't use a Reader for the outer and a DataSet for the inner - here's hoping inner and outer DataSets will work!
Dave
Dave, did you know that a DataSet can contain multiple, related tables? If you're trying to do master/detail, you can do it in a single DataSet.
John Saunders
@Downvoter: why? What's wrong with my answer?
John Saunders
A: 

It would be pretty straight forward to convert that to a SQLDataReader to replace the recordset. Basically, the syntax is

    using conn as new sqlconnection({connection string})
   using cmd as new sqlcommand("select * from tblDQ order by xid, xcode, xDOS", conn)
      cmd.connection.open()
      using reader as SQLDataReader = cmd.ExecuteDataReader()
          while reader.read
                  do your thing here, referencing reader("field")
          end while
      end using  'dispose of the reader
    end using  'dispose of teh command
end using  'close and dispose of the connection
rjrapson
Ack! Almost, but isnt' the reader a read-only construct? I need to update a field in the database with the code.
Dave
Oops - yup. missed that - didn't pay close enough attention. LINQ would definately be better then.
rjrapson