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

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
commented: Solved! Close both, reader & connection +11

thank You. I Will try it...

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.