aspdotnet-suresh offers articles and tutorials,csharp dot net, articles and tutorials,VB.NET Articles,Gridview articles,code examples of 2.0 /3.5,AJAX,SQL Server Articles,examples of .net technologies

ExecuteScalar Example in Asp.Net Using C# and VB.NET

Sep 20, 2012

In this article I will explain executescalar example in using and VB.NET.


In previous post I explained ExecuteNonQuery Example and differences between ExecuteNonQuery, ExecuteReader and ExecuteScalar in Now I will explain ExecuteScalar concept with one example in using, VB.NET.


Execute Scalar will return first row first column value i.e. it will return single value and ignore other values on execution of SQL Query or Stored procedure using command object. It’s very fast to retrieve single values from database.

Before implement this example first design one table UserInformation in your database as shown below

Column Name
Data Type
Allow Nulls
Once table designed in database enter some dummy data to test after that write the following code in your aspx page

<html xmlns="">
<head id="Head1" runat="server">
<title>Example of ExecuteNonQuery in</title>
<form id="form1" runat="server">
<asp:Button ID="btnSubmit" runat="server" Text="Submit" onclick="btnSubmit_Click" /><br />
<b>First Row First Column Value: </b><asp:Label ID="lblDetails" runat="server" />
Now add the following namespaces in code behind

C# Code

using System;
using System.Data.SqlClient;
After add namespaces write the following code in code behind

protected void Page_Load(object sender, EventArgs e)

protected void btnSubmit_Click(object sender, EventArgs e)
using (SqlConnection con = new SqlConnection("Data Source=SureshDasari;Integrated Security=true;Initial Catalog=MySampleDB"))
SqlCommand cmd = new SqlCommand("Select UserName,LastName,Location FROM UserInformation", con);
string result = (string)cmd.ExecuteScalar();
if (!string.IsNullOrEmpty(result))
lblDetails.Text = result;
lblDetails.Text =  "No value Selected" ;

Imports System.Data.SqlClient
Partial Class ExecuteScalar
Inherits System.Web.UI.Page
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)

End Sub
Protected Sub btnSubmit_Click(ByVal sender As Object, ByVal e As EventArgs)
Using con As New SqlConnection("Data Source=SureshDasari;Integrated Security=true;Initial Catalog=MySampleDB")
Dim cmd As New SqlCommand("Select UserName,LastName,Location FROM UserInformation", con)
Dim result As String = DirectCast(cmd.ExecuteScalar(), String)
If Not String.IsNullOrEmpty(result) Then
lblDetails.Text = result
lblDetails.Text =  "No value Selected"
End If
End Using
End Sub
End Class

If you observe above code I written select query like getting all the data from UserInfromation table but whenever we click on button we are getting only one value (First Row First Column value) and ignoring other row and column values.

If you enjoyed this post, please support the blog below. It's FREE!

Get the latest,, VB.NET, jQuery, Plugins & Code Snippets for FREE by subscribing to our Facebook, Twitter, RSS feed, or by email.

subscribe by rss Subscribe by RSS subscribe by email Subscribe by Email


Unknown said...

You have missed out on the fact that executescalar also returns the 1st column of the first row if the query returns more than one value

Anonymous said...

cmd.ExecuteScalar() is return only value, not display the details

Anonymous said...

whether it works well for delete query?

Give your Valuable Comments

Note: Only a member of this blog may post a comment.

© 2015 All Rights Reserved.
The content is copyrighted to Suresh Dasari and may not be reproduced on other websites without permission from the owner.