SiteMap  | About    

ASP.NET ExecuteScalar

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.



<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "">
<html xmlns="">
<head id="Head1" runat="server">
	<title>Untitled Page</title>
	<form id="form1" runat="server">
		<asp:Button ID="Button1" runat="server" Text="Button" onclick="Button1_Click" />
	<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>

Click the following links to see full source code

C# Source Code
VB.NET Source Code
default.aspx.vb (C) 2018    Founded by raps mk
All Rights Reserved. All other trademarks are property of their respective owners.