display total number of records in a label

ssfftt

Well-known member
Joined
Oct 27, 2005
Messages
163
Programming Experience
1-3
[solved]display total number of records in a label

In SQL I can do:
SELECT COUNT(*) AS Expr1
FROM TEST
to get total number of records, how can i display this number into a lblTotal.text in VB.NET?

plz help in code
 
Last edited:
hi,
you can use this,

Dim SelStr As String = "SELECT COUNT(*) AS Expr1 FROM TEST"
Dim Selcmd As New Odbc.OdbcCommand
Dim dr As Odbc.OdbcDataReader
dim Con as new odbc.odbcConnection
con.ConnectionString="ur connection String to db"
dim TmpStr as string
Try
Selcmd.Connection = Con
Selcmd.CommandText = SelStr
Con.Open()
dr = Selcmd.ExecuteReader(CommandBehavior.SingleRow)
While dr.Read
TmpStr = dr.GetValue(0).ToString
End While
If TmpStr = string.Empty Then
lblTotal.text =0
Else
lblTotal.text = TmpStr
EndIF
Catch ex As Exception
End Try
Selcmd = Nothing
dr.Close()
Con.Close()
 
If you are using a query that returns a single value then call ExecuteScalar instead of ExecuteReader:
VB.NET:
myConnection.Open()
myLabel.Text = myCommand.ExecuteScalar().ToString()
myConnection.Close()
 
thx Jeel and jmcilhinneyI used the simple solution from jmcilhinney, and it works fine so far, thx a lot :)
 
Back
Top