ExecuteScalar method uses to retrieve a single value from a database. The ExecuteScalar() executes SQL statements as well as Stored Procedure and returns the first column of the first row in the result set, or a null reference if the result set is empty.
Dim result As Int32 = Convert.ToInt32(cmd.ExecuteScalar)
int result = Convert.ToInt32(cmd.ExecuteScalar());
It is very useful to use with aggregate functions like Count(*) or Sum() etc. When compare to ExecuteReader() , ExecuteScalar() uses fewer System resources.
The following ASP.NET program find number of rows in the author table using ExecuteScalar method.
Click the following links to see full source code