0

Please help me how to count the specific records on my database using VB.Net

I dont know the code to count the number of male in my database using VB.Net

What i want to happen is when I click the cmd totalmale, The Total number of male in my database will appear on maletextbox. and when i click cmd totalfemale, the total number of female will appear on femaletextbox. please help me with these..

Thank You

2
Contributors
2
Replies
3
Views
6 Years
Discussion Span
Last Post by Macky Dave
1

You can use the following code. Code if for MS SQL Server, but if use for example Access database change "SqlConnection" to "OleDbConnection" or "OdbcConnection".

Private Function count() As Long
   SqlConnection connection = New SqlConnection("connectionString")
   SqlCommand command = New SqlCommand("SELECT COUNT(*) FROM tableName WHERE condition = ''",connection)
   SqlDataReader reader

   Long result = 0

   Try
      connection.Open()
      reader = command.ExecuteReader(CommandBehaviour.Default)

      If (reader .Read()) Then
         result = reader.GetValue(0)
      Else
         result = 0
      End If

      connection.Close()
   Catch ex As Exception
      ' ERROR
   End Try

   Return result;
End Function
Votes + Comments
Solved! Close both, reader & connection
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.