Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

RecordCount in VB.NET 1

Status
Not open for further replies.

adrianjohnson

Programmer
May 16, 2002
145
0
0
GB
I have a RecordSet (I know it's old hat, but I'm used to VB6, and can't get the hang of data access in VB.NET), and want a count of the records. However, I get -1. How do you set the CursorType in VB.NET, as it returns an error message when I use it !!!

I can post sample code if that'll help.

Thanks,

Adrian Johnson
 
Adrian, this is probably a case of thinking english when you want to learn Spanish. You should take the time to learn the Dataset Object which is filled with a data adapter. Data adapters can be created by simply dragging a table in from your server explorer and configuring them through a GUI.

If you are translating VB6 literally into .NET, you are not taking advantage of the RAD .NET offers.

Anyway, to answer your question with .NET's dataset, it is DS.Tables(Tablename).rows.count

Dwaine
 
Post the sample code. I will show you both ways to get what you are after so you can see the old and the new side by side. Give enough details so I can know what you are trying to do.

DotNetDoc
M.C.S.D.
---------------------------------------

Tell me and I forget. Show me and I remember. Involve me and I understand.
- Anonymous Chinese Proverb

-----------------------------------

If you can't explain it simply, you don't understand it well enough.
- A. Einstein
 
RS = DBS.Execute("Select * From tblJobs ORDER BY CDate(JobDate)")
RS.MoveFirst()
liCount = RS.RecordCount

Otherwise I just loop and increment the counter instead!

Thanks,

Adrian Johnson
 
There is no recordcount in ADO.Net but this will get you what you are after.
'This connection string connects to access. I am not sure what you are connecting to?

Dim strConn as string ="PROVIDER=Microsoft.Jet.OLEDB.4.0;DATA SOURCE=" & server.mappath("/experiments/data/biblio.mdb") & ";"

'This is your Sql String count will give you a count of rows.

Dim strSQL as string ="select count(*) from publishers where state='NY'"
Dim Conn as New OLEDBConnection(strConn)
Dim Cmd as New OLEDBCommand(strSQL,Conn)
Conn.Open()
'ExecuteScalar returns just one record. The count
Dim countOfRecords as integer
countOfRecords =cmd.executescalar()
Conn.Close()



DotNetDoc
M.C.S.D.
---------------------------------------

Tell me and I forget. Show me and I remember. Involve me and I understand.
- Anonymous Chinese Proverb

-----------------------------------

If you can't explain it simply, you don't understand it well enough.
- A. Einstein
 
Actually, this will give you a record count in ADO.net

intRowCnt = objDataAdapter.Fill(objDataSet)
 
If you just want a record count you don't want the overhead of a DataAdapter and a Dataset. If you need to work with the data afterwards then that would work. If not then executescalar()
is the fastest of all the command methods.
Also what this

"intRowCnt = objDataAdapter.Fill(objDataSet)"

will give you is rows affected not row count. While it should give you the count of records initially, it is important to know what that statement is returning.[thumbsup2]

DotNetDoc
M.C.S.D.
---------------------------------------

Tell me and I forget. Show me and I remember. Involve me and I understand.
- Anonymous Chinese Proverb

-----------------------------------

If you can't explain it simply, you don't understand it well enough.
- A. Einstein
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top