VisualBuilder
  Home > Aspnet > Tutorials > Execute Scalar: Getting Single Value from the Database - ASP.NET Database Tutorial
Tell a friend
Link to us
Total Members
      Members: 84657
     
Sitemap Forum Chat
Home
ASP.NET Database Tutorial Home
1 . Introduction to Asp.Net 2.0
2 . Introduction to ADO.Net
3 . Connecting Access Database with AccessDataSource and GridView Control
4 . Accessing SQL SERVER Database [Using SqlDataSource]
5 . Storing Connection String in Web.Config File
6 . Inserting Records in Database
7 . Updating Records in Database
8 . Updating Records in Database
9 . Deleting Records from Database
10 . Execute Scalar: Getting Single Value from the Database
11 . Execute Reader: Fetching Records from Database
12 . Data Adapter
13 . Data Adapter and Database Connection
14 . Performing Batch Updates: Using SqlCommandBuilder
15 . Improving Performance using Stored Procedure
16 . Filtering and Sorting Data Using DataView/DataTable
17 . Data Binding and Data Synchronization
18 . SQL Transaction and Locking
19 . SQL Joins and .Net
20 . Automatically Generation of SQL Statement
 
Aspnet Group Home
Aspnet Discussion (10)
Aspnet Members (2382)
Aspnet Resources
Aspnet Source Code (388)
Aspnet Articles (1)
Aspnet Blogs
Aspnet Jobs
Aspnet Components (201)
Aspnet Books
Aspnet Websites (21)
Aspnet News (105)
Aspnet Q & A (114)
- Aspnet Ask Question
- Aspnet Questions
- Aspnet Unanswered Questions
 
GROUPS
.NET
ASP.NET
.NET
C#
ASP
Visual Basic
Java
Java
JSP
EJB
Other
Delphi
C++
Ajax
UML
JavaScript
PHP
Web Design
Web Hosting
SQL Server
Oracle
Project Management
More Groups

 
LEARNING CENTER
TUTORIALS
.NET
.NET Tutorial
ASP Tutorial
ASP.NET Database Tutorial
ASP.NET Development Tips
ASP.Net Security,Internationalisation And Deployment
ASP.NET Server Controls Tips
ASP.NET Tutorial
C Sharp Tutorial
Web Development
Flex Tutorial
HTML Tutorial
Learn AJAX Tutorial
PHP Tutorial
Software Development
Database Tutorial
SQL Tutorial
UML Tutorial
Java
Ant Tutorial
EJB 3 Tutorial
Hibernate Tutorial
Java Tutorial
Java Web Component Tutorial
Java XML Tutorial
JDBC Tutorial
JDK1.5 Tutorial
JSF Tutorial
JSP And J2EE Design Tutorial
JSP Tutorial
Spring Tutorial
Struts Tutorial

RESOURCES
Q & A (434 )
Source Code (3275 )
Articles (11 )
Components (1595 )
News (888 )
Websites (1207 )

SUBMISSIONS
Submit Article
Submit Website
Submit News
Submit Source Code
Submit Component

COMMUNITY
Members Directory
Discussion Forum
Chat

SITE
About Us
Sitemap
Search
Contact Us
Link To Us
Feedback
Tell a Friend
Partners
Advertise

Aspnet database Tutorial
 Execute Scalar: Getting Single Value from the Database
  << Prev: Deleting Records from Database Next: Execute Reader: Fetching Records from Database >>

Execute Scalar method to retrieve a single value (for example, an aggregate value) from a database. Since the return type of ExecuteScalar is type object, we use a cast operator to convert the value to desired data type. The ExecuteScalar method belongs to the Command object in ASP.Net..


 


Example: Demonstrate Execute Scalar method of the Command Object


 


ExecuteScalar.aspx


 


 


<% @ Page Language ="C#" AutoEventWireup ="true" CodeFile ="ExecuteScalar.aspx.cs" Inherits ="_Default" %>


 


<! DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">


 


<html xmlns ="http://www.w3.org/1999/xhtml" >


<head runat ="server">


</head >


<body >


<form id ="form1" runat ="server">


<div>


<strong> Execute Scalar </strong>


<br />


<br />


<em > Total No. of Records in Each Table: </ em >< strong >< em > Press


Button in front of EachQuery <br />


<br />


</em> &nbsp; &nbsp; &nbsp; < br />


<table border ="0">


<tr><td style ="width: 3px">


    <asp:Label ID ="Label1" runat ="server" Text ="SELECT count(*) FROM Categories"


Width ="262px"></asp:Label ></td>


<td style ="width: 3px">


    <asp:Button ID ="btnCategories" runat ="server" OnClick ="btnCategories_Click" Text ="Count" /></td>


<td style ="width: 3px">


    <asp:Label ID ="lblCategories" runat ="server"></ asp : Label ></ td >


</tr>


<tr><td style ="width: 3px">


    <asp:Label ID ="Label2" runat ="server" Text ="SELECT count(*) FROM Customers" Width ="258px"></ asp : Label ></ td >


<td style ="width: 3px">


    <asp:Button ID ="btnCustomers" runat ="server" OnClick ="btnCustomers_Click" Text ="Count" /></ td >


<td style ="width: 3px">


    <asp:Label ID ="lblCustomers" runat ="server"></ asp : Label ></ td >


</ tr >


<tr>


<td style ="width: 3px">


<asp:Label ID ="Label3" runat ="server" Text ="SELECT count(*) FROM Employees" Width ="257px"></ asp : Label ></ td >


<td style ="width: 3px">


    <asp: Button ID ="btnEmployees" runat ="server" OnClick ="btnEmployees_Click" Text ="Count" /></ td >


<td style ="width: 3px">


<asp:Label ID ="lblEmployees" runat ="server"></ asp : Label ></ td >


</tr>


<tr>


<td style ="width: 3px">


    <asp:Label ID ="Label4" runat ="server" Text ="SELECT count(*) FROM Orders" Width ="257px"></asp:Label ></ td >


<td style ="width: 3px">


    <asp:Button ID ="btnOrders" runat ="server" OnClick ="btnOrders_Click" Text ="Count" /></ td >


<td style ="width: 3px">


<asp:Label ID ="lblOrders" runat ="server"></ asp : Label ></ td >


</tr>


<tr >


<td style ="width: 3px">


    <asp:Label ID ="Label5" runat ="server" Text ="SELECT count(*) FROM Products" Width ="256px"></ asp : Label ></ td >


<td style ="width: 3px">


    <asp:Button ID ="btnProducts" runat ="server" OnClick ="btnProducts_Click" Text ="Count" /></ td >


<td style ="width: 3px">


    <asp:Label ID ="lblProducts" runat ="server"></ asp : Label ></ td >


</tr >


<tr >


<td style ="width: 3px; height: 26px">


<asp:Label ID ="Label6" runat ="server" Text ="SELECT count(*) FROM Region" Width ="253px"></ asp : Label ></ td >


<td style ="width: 3px; height: 26px">


<asp:Button ID ="btnRegion" runat ="server" OnClick ="btnRegion_Click" Text ="Count" /></ td >


<td style ="width: 3px; height: 26px">


<asp : Label ID ="lblRegion" runat ="server"></ asp : Label ></ td >


</tr >


<tr >


<td style ="width: 3px">


    <asp : Label ID ="Label7" runat ="server" Text ="SELECT count(*) FROM Shippers" Width ="254px"></ asp : Label ></ td >


<td style ="width: 3px">


<asp:Button ID ="btnShippers" runat ="server" OnClick ="btnShippers_Click" Text ="Count" /></ td >


<td style ="width: 3px">


<asp : Label ID ="lblShippers" runat ="server"></ asp : Label ></ td >


</tr >


<tr >


<td style ="width: 3px">


    <asp:Label ID ="Label8" runat ="server" Text ="SELECT count(*) FROM Suppliers" Width ="256px"></ asp : Label ></ td >


<td style ="width: 3px">


    <asp : Button ID ="btnSuppliers" runat ="server" OnClick ="btnSuppliers_Click" Text ="Count" /></ td >


<td style ="width: 3px">


<asp:Label ID ="lblSuppliers" runat ="server"></ asp : Label ></ td >


</ tr >


<tr >


<td style ="width: 3px">


<asp:Label ID ="Label9" runat ="server" Text ="SELECT count(*) FROM Territories" Width ="252px"></ asp : Label ></ td >


< td style ="width: 3px">


<asp:Button ID ="btnTerritories" runat ="server" OnClick ="btnTerritories_Click" Text ="Count" /></ td >


<td style ="width: 3px">


    <asp:Label ID ="lblTerritories" runat ="server"></ asp : Label ></ td >


</ tr >


</table>


</strong>


</div>


</form>


</body>


</html>


 


ExecuteScalar.aspx.cs


 


 


using System;


using System.Data;


using System.Configuration;


using System.Web;


using System.Web.Security;


using System.Web.UI;


using System.Web.UI.WebControls;


using System.Web.UI.WebControls.WebParts;


using System.Web.UI.HtmlControls;


using System.Data.SqlClient;


 


public partial class _Default : System.Web.UI. Page


{


  protected void Page_Load( object sender, EventArgs e)


    {


    }


  protected void btnCategories_Click( object sender, EventArgs e)


    { //Categories Table


    string strQuery = "select Count(*) from Categories" ;


    lblCategories.Text = countNumberOfRecords(strQuery).ToString() ;


    }


  protected void btnCustomers_Click( object sender, EventArgs e)


    { //Customers Table


    string strQuery = "select Count(*) from Customers" ;


    lblCustomers.Text = countNumberOfRecords(strQuery).ToString();


    }


  protected void btnEmployees_Click( object sender, EventArgs e)


    { //Employee Table


    string strQuery = "select Count(*) from Employees" ;


    lblEmployees.Text = countNumberOfRecords(strQuery).ToString();


    }


  protected void btnOrders_Click( object sender, EventArgs e)


    { //Orders Table


    string strQuery = "select Count(*) from Orders" ;


    lblOrders.Text = countNumberOfRecords(strQuery).ToString();


    }


  protected void btnProducts_Click( object sender, EventArgs e)


    { //Products Table


    string strQuery = "select Count(*) from Products" ;


    lblProducts.Text = countNumberOfRecords(strQuery).ToString();


    }


  protected void btnRegion_Click( object sender, EventArgs e)


    { //Region Table


    string strQuery = "select Count(*) from Region" ;


    lblRegion.Text = countNumberOfRecords(strQuery).ToString();


    }


  protected void btnShippers_Click( object sender, EventArgs e)


    { //Shipper Table


    string strQuery = "select Count(*) from Shippers" ;


    lblShippers.Text = countNumberOfRecords(strQuery).ToString();


    }


  protected void btnSuppliers_Click( object sender, EventArgs e)


    { //Suppliers Table


    string strQuery = "select Count(*) from Suppliers" ;


    lblSuppliers.Text = countNumberOfRecords(strQuery).ToString();


    }


  protected void btnTerritories_Click( object sender, EventArgs e)


    { //Territories Table


    string strQuery = "select Count(*) from Territories" ;


    lblTerritories.Text = countNumberOfRecords(strQuery).ToString();


    }


 


  private int countNumberOfRecords( string sqlQuery){


    int _noofRecords = 0;


    try


        {


            string strConn = "Data Source=localhost;Initial Catalog= Northwind; user Id=sa;Password = test" ;


            SqlConnection conn = new SqlConnection (strConn);


            SqlCommand cmd = new SqlCommand ();


            cmd.Connection = conn;


            cmd.CommandText = sqlQuery;


            cmd.CommandType = CommandType .Text;


            conn.Open();


            _noofRecords = ( int )cmd.ExecuteScalar();


            conn.Close();


        }catch ( Exception ex) {


            Response.Write(ex.Message);


        }


        return _noofRecords;


    }


 


}


 


 


Output:


 



 


Clicking every count button in front of the query:


 



  << Prev: Deleting Records from Database Next: Execute Reader: Fetching Records from Database >>
Aspnet Database Tutorial Home
Give feedback and win a prize.

 
   Printer Friendly
   Email to a friend
   Add to my Favourites    
  Download PDF version
   Report Bad Submissions
   Submit Feedback
 
  Delicious   Digg   Technorati   Blink   Furl   Reddit   Newsvine   Google Click each image to add
this page to each site.
 
 
Welcome Guest Signup
MEMBER'S PANEL
EMAIL
PASSWORD
Forgot your password?
New User? Click Here!
 
Resend Activation Email!
 
SEARCH
 
 
LINKS
online fax server
MSN
Video Surveillance
Exchange Hosting
Gift to Pakistan
 
ADVERTISEMENT
 
PARTNER LIST

More
 
 
 

Home | Login | About Us | Contact Us | Privacy Policy | Advertising