You are on page 1of 18

28/10/2014 Select SQL Server Stored Procedures using ASP.Net Example | ASP.Net, C#.Net, VB.

VB.Net, AJAX,JQuery, JavaScript, GridView,SQL Server, RDLC, S…

Tupperware Dell New Philips


Aquasafe Entry 15.6 QT4005
Bottles 1… inch… Trimmer ( M…
Rs.875 Rs.375 Rs.1,586
Upto 70% Cash Back.
Paytm.com Buy Now! Buy Now! Buy Now!

Home
Categories
ASP.Net

C#.Net

JavaScript

ADO.Net

Excel

AJAX

VB.Net

SQL Server

GridView

Issues and Exceptions

Silverlight

Rich Text Editor

JQuery

DataList

Snippets

XML

New Features

.Net 4.0
http://www.aspsnippets.com/Articles/Select-SQL-Server-Stored-Procedures-using-ASPNet-Example.aspx 1/18
28/10/2014 Select SQL Server Stored Procedures using ASP.Net Example | ASP.Net, C#.Net, VB.Net, AJAX,JQuery, JavaScript, GridView,SQL Server, RDLC, S…

TreeView

AJAX Control Toolkit

jQuery Plugins

Third Party Controls

ASP.Net Validators

WCF

Repeater

Regular Expressions

Yahoo API

iTextSharp

FaceBook

Charts

ListView

Tweeter

Google

CSS

SMS

DotNetZip

Crystal Reports

Entity Framework

HyperLink

RDLC Report

SqlDataSource

Menu

http://www.aspsnippets.com/Articles/Select-SQL-Server-Stored-Procedures-using-ASPNet-Example.aspx 2/18
28/10/2014 Select SQL Server Stored Procedures using ASP.Net Example | ASP.Net, C#.Net, VB.Net, AJAX,JQuery, JavaScript, GridView,SQL Server, RDLC, S…

YouTube

Twitter

HTML

XmlDataSource

ListBox

Tips

DataGridView

Cryptography

Windows Forms

LinkedIn

WebUserControl

RSS Feeds

HTML5

Sitemap

IIS

LINQ

DataPager

URL Routing

SqlBulkCopy

OCR

ASP.Net 4.5

Master Pages

MySQL

CSV

http://www.aspsnippets.com/Articles/Select-SQL-Server-Stored-Procedures-using-ASPNet-Example.aspx 3/18
28/10/2014 Select SQL Server Stored Procedures using ASP.Net Example | ASP.Net, C#.Net, VB.Net, AJAX,JQuery, JavaScript, GridView,SQL Server, RDLC, S…

Forums
Contact
Search
Eg. Send Email ASP.Net

Home
Categories
Forums
Contact

Search
Eg. Send Email ASP.Net

Eg. Send Email ASP.Net


Search

Select SQL Server Stored Procedures using ASP.Net Example


22 Sep 2012 Mudassar Khan
8 Comments 41519 Views
ASP.Net ADO.Net SQL Server

Here Mudassar Ahmed Khan has explained how to call Select SQL Server Stored Procedures
using ADO.Net in as ASP.Net Web Application and also how to bind the data returned by the
stored procedures to ASP.Net GridView.

Download

http://www.aspsnippets.com/Articles/Select-SQL-Server-Stored-Procedures-using-ASPNet-Example.aspx 4/18
28/10/2014 Select SQL Server Stored Procedures using ASP.Net Example | ASP.Net, C#.Net, VB.Net, AJAX,JQuery, JavaScript, GridView,SQL Server, RDLC, S…

ASPSnippets
Like

8,001 people like ASPSnippets.


1

ASPSnippets
Here I amFollow
explaining how to create and call stored procedures in .net using ADO.Net classes.
In my previous article I explained Using Stored Procedures in SQL Server Database
Here I am explaining how to call stored procedures that return data using ADO.Net. I’ll also
explain how to bind the results to databound controls like GridView or DataGrid.
336 have us in circles
For this tutorial I am using the Employees Table of NorthWind Database. You can download the
NorthWind Database from here
ASP on Twitter
Follow
Connection String
ASP has 223 Followers
<connectionStrings>
<add name="conString"
connectionString="Data
Jose G. Alvarez
Tim
R. Mugodo
Juanchi Sopan MaitiSource=.\SQLEXPRESS;
Kalpesh Bhadra Katrina 坂爪 美夏 Juan Guillermo
Helio
A. Pereira Brian Downey

database=Northwind;Integrated Security=true"/>
connectionStrings>

Namespaces

You will need to import the following namespaces

http://www.aspsnippets.com/Articles/Select-SQL-Server-Stored-Procedures-using-ASPNet-Example.aspx 5/18
28/10/2014 Select SQL Server Stored Procedures using ASP.Net Example | ASP.Net, C#.Net, VB.Net, AJAX,JQuery, JavaScript, GridView,SQL Server, RDLC, S…

C#

using System.Data;
using System.Data.SqlClient;
using System.Configuration;

VB.Net

Imports System.Data
Imports System.Data.SqlClient
Imports System.Configuration

Select Stored Procedures


Here is a simple Select Stored Procedure that brings all the records from the Employees table

SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[GetAllEmployeeDetails]
AS
BEGIN
SET NOCOUNT ON;
SELECT FirstName, LastName, BirthDate, City, Country
FROM Employees
END

Now I’ll call the stored procedure using ADO.Net in my ASP.Net website and bind the results to
a GridView. Since I need to fetch multiple rows I’ll be using ExecuteReader method of SQL
Command object

C#

String strConnString = ConfigurationManager.ConnectionStrings["conString"].Connec


tionString;
SqlConnection con = new SqlConnection(strConnString);
SqlCommand cmd = new SqlCommand();

http://www.aspsnippets.com/Articles/Select-SQL-Server-Stored-Procedures-using-ASPNet-Example.aspx 6/18
28/10/2014 Select SQL Server Stored Procedures using ASP.Net Example | ASP.Net, C#.Net, VB.Net, AJAX,JQuery, JavaScript, GridView,SQL Server, RDLC, S…

cmd.CommandType = CommandType.StoredProcedure;
cmd.CommandText = "GetAllEmployeeDetails";
cmd.Connection = con;
try
{
con.Open();
GridView1.EmptyDataText = "No Records Found";
GridView1.DataSource = cmd.ExecuteReader() ;
GridView1.DataBind();
}
catch (Exception ex)
{
throw ex;
}
finally
{
con.Close();
con.Dispose();
}

VB.Net

Dim strConnString As String = ConfigurationManager.ConnectionStrings("conString").


ConnectionString
Dim con As New SqlConnection(strConnString)
Dim cmd As New SqlCommand()
cmd.CommandType = CommandType.StoredProcedure
cmd.CommandText = "GetAllEmployeeDetails"
cmd.Connection = con
Try
con.Open()
GridView1.EmptyDataText = "No Records Found"
GridView1.DataSource = cmd.ExecuteReader()
GridView1.DataBind()
Catch ex As Exception
Throw ex

http://www.aspsnippets.com/Articles/Select-SQL-Server-Stored-Procedures-using-ASPNet-Example.aspx 7/18
28/10/2014 Select SQL Server Stored Procedures using ASP.Net Example | ASP.Net, C#.Net, VB.Net, AJAX,JQuery, JavaScript, GridView,SQL Server, RDLC, S…

Finally
con.Close()
con.Dispose()
End Try

Select Stored Procedures with Parameters


Here I will get filtered records based on Employee ID. In the stored procedure below I am
passing Employee ID as parameter and based on the ID the records will be fetched.

SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[GetEmployeeDetailsByID]
@EmployeeID int = 0
AS
BEGIN
SET NOCOUNT ON;
SELECT FirstName, LastName, BirthDate, City, Country
FROM Employees WHERE EmployeeID=@EmployeeID
END

Now in order to call the above stored procedure in an asp.net application using ADO.Net we will
have to do it the following way. You will notice all remains the same except here I am passing
@EmployeeID as parameter. Also you will notice txtID which is a TextBox in which user enters
the EmployeeID of the Employee whose details he wants to view

C#

String strConnString = ConfigurationManager.ConnectionStrings["conString"].Connect


ionString;
SqlConnection con = new SqlConnection(strConnString);
SqlCommand cmd = new SqlCommand();
cmd.CommandType = CommandType.StoredProcedure;
cmd.CommandText = "GetEmployeeDetailsByID";

http://www.aspsnippets.com/Articles/Select-SQL-Server-Stored-Procedures-using-ASPNet-Example.aspx 8/18
28/10/2014 Select SQL Server Stored Procedures using ASP.Net Example | ASP.Net, C#.Net, VB.Net, AJAX,JQuery, JavaScript, GridView,SQL Server, RDLC, S…

cmd.Parameters.Add("@EmployeeID", SqlDbType.Int).Value = txtID.Text.Trim();

cmd.Connection = con;
try
{
con.Open();
GridView1.EmptyDataText = "No Records Found";
GridView1.DataSource = cmd.ExecuteReader();
GridView1.DataBind();
}
catch (Exception ex)
{
throw ex;
}
finally
{
con.Close();
con.Dispose();
}

VB.Net

Dim strConnString As String = ConfigurationManager.ConnectionStrings("conString").


ConnectionString
Dim con As New SqlConnection(strConnString)
Dim cmd As New SqlCommand()
cmd.CommandType = CommandType.StoredProcedure
cmd.CommandText = "GetEmployeeDetailsByID"
cmd.Parameters.Add("@EmployeeID", SqlDbType.Int).Value = txtID.Text.Trim()
cmd.Connection = con
Try
con.Open()
GridView1.EmptyDataText = "No Records Found"
GridView1.DataSource = cmd.ExecuteReader()
GridView1.DataBind()
Catch ex As Exception

http://www.aspsnippets.com/Articles/Select-SQL-Server-Stored-Procedures-using-ASPNet-Example.aspx 9/18
28/10/2014 Select SQL Server Stored Procedures using ASP.Net Example | ASP.Net, C#.Net, VB.Net, AJAX,JQuery, JavaScript, GridView,SQL Server, RDLC, S…

Throw ex
Finally
con.Close()
con.Dispose()
End Try

This completes the article in my next article I’ll be explaining how to call insert stored procedures
in asp.net using ADO.Net. You can download the code in VB.Net and C# along with the SQL
Scripts using the link below

Download

Part - II Calling Insert SQL Server Stored Procedures using ADO.Net >>

http://www.aspsnippets.com/Articles/Select-SQL-Server-Stored-Procedures-using-ASPNet-Example.aspx 10/18
28/10/2014 Select SQL Server Stored Procedures using ASP.Net Example | ASP.Net, C#.Net, VB.Net, AJAX,JQuery, JavaScript, GridView,SQL Server, RDLC, S…

http://www.aspsnippets.com/Articles/Select-SQL-Server-Stored-Procedures-using-ASPNet-Example.aspx 11/18
28/10/2014 Select SQL Server Stored Procedures using ASP.Net Example | ASP.Net, C#.Net, VB.Net, AJAX,JQuery, JavaScript, GridView,SQL Server, RDLC, S…

Related Articles

Calling Update SQL Server Stored Procedures using ADO.Net


Here Mudassar Ahmed Khan has explained how to call stored procedures that update the data in
SQL Server Database using ADO.Net
Calling Insert SQL Server Stored Procedures using ADO.Net
Here Mudassar Ahmed Khan has explained how to call Insert SQL Server Stored Procedures
using ADO.Net in as ASP.Net Web Application
Calling Select SQL Server Stored Procedures using ADO.Net
Here Mudassar Ahmed Khan has explained how to call Select SQL Server Stored Procedures
using ADO.Net in as ASP.Net Web Application and also how to bind the data returned by the
stored procedures to AS
Using Stored Procedures in SQL Server Database
Here Mudassar Ahmed Khan has explained how to create modify and delete Stored Procedures in
Microsoft SQL Server Database

http://www.aspsnippets.com/Articles/Select-SQL-Server-Stored-Procedures-using-ASPNet-Example.aspx 12/18
28/10/2014 Select SQL Server Stored Procedures using ASP.Net Example | ASP.Net, C#.Net, VB.Net, AJAX,JQuery, JavaScript, GridView,SQL Server, RDLC, S…

Comments

Man EanglaJan 26, 2013 10:01 PM 117.20.116.91

thank your of u share source.

Rajini.CJan 28, 2013 05:01 PM 115.111.223.59

Very helping article... It really works. Thanks alot

Rajini.CJan 28, 2013 05:01 PM 115.111.223.59

Very helping article... It really works. Thanks alot

Pawan GowardhanJan 02, 2014 12:01 PM 117.218.169.233

best...

MadasamyFeb 12, 2014 10:02 AM 117.206.82.136

its really very useful

More

http://www.aspsnippets.com/Articles/Select-SQL-Server-Stored-Procedures-using-ASPNet-Example.aspx 13/18
28/10/2014 Select SQL Server Stored Procedures using ASP.Net Example | ASP.Net, C#.Net, VB.Net, AJAX,JQuery, JavaScript, GridView,SQL Server, RDLC, S…

Add Comments

You can add your comment about this article using the form below. Make sure you provide a
valid email address
else you won't be notified when the author replies to your comment
Please note that all comments are moderated and will be deleted if they are
http://www.aspsnippets.com/Articles/Select-SQL-Server-Stored-Procedures-using-ASPNet-Example.aspx 14/18
28/10/2014 Select SQL Server Stored Procedures using ASP.Net Example | ASP.Net, C#.Net, VB.Net, AJAX,JQuery, JavaScript, GridView,SQL Server, RDLC, S…

Not relavant to the article


Spam
Advertising campaigns or links to other sites
Abusive content.

Please do not post code, scripts or snippets.

Name
Name
Email
Email
Comment
Comment

Security code:

Add Comment

What our readers say

Javid
Information provided by you is beneficial for the students as well as for the developers.
Anidesh Madhav
All your example are very helpful. Simple and easy to understand as well.
Vishant
Your code is very helpful and usable..
Thank you for giving help for code and wish to always help us like this.
Vishant
Mary Ticong
It’s really fantastic Thanks
Keep on making this amazing tutorial It’s a great help for us beginners...
Katheeja
This site is like Google for asp.net developers. Simple and worthy..
Dhruv
Nice work ASP Snippets team... Keep it up.
Your methods of explanation are in simple language even non IT person can understand quickly...
Karthik
I have been using many of your posts for reference and it’s very easy... Keep doing this wonderful
work...
JK
This is the most useful .Net site I’ve found - I love it. Thank you for sharing all these tips and tricks.
Umesh
This is very nice and informative website for students, freshers as well as for employee also.
Thanks.
Raju Raval
http://www.aspsnippets.com/Articles/Select-SQL-Server-Stored-Procedures-using-ASPNet-Example.aspx 15/18
28/10/2014 Select SQL Server Stored Procedures using ASP.Net Example | ASP.Net, C#.Net, VB.Net, AJAX,JQuery, JavaScript, GridView,SQL Server, RDLC, S…

Respected sir your articles are awesome. I use this SMS sending trick in my asp.net application in
my study. Thank you. I regularly visit your articles... Please keep it up.
Iron
You are number one. Thanks.
EC
Great website great articles! This is a good site for both newbies and experience developers. It
helps me achieve dome very changing tasks in a short period of time.
Juan Elizalde
I just want to say thank you, I am an active visitor of your site and it is really useful, I work as a
Software Consultant in Mexico.
Congratulations because you help people and the world to change using technology.
Suraj Petkar
Nice ...It really helped....Very good site. Lots of helpful stuff on it.
Thanks.
Petros
The simple ones are the best just plug in a few lines of code and it does the job complete as well
demo and source available
Syed Jalal Tashrrifullahi
The sheer brilliance of Mudassar inspires me. Another great article showing how simplicity is can
be REALLY effective.
vikram parmr
This is the superb site ever found so cool. It’s an amazing thing has been started by khan sir salute
to you.
Lita Pierce
Golden piece of code I appreciate that you have taken a bit of time pulling this together and I just
wanted to say THANK YOU MAN. Good work and keep posting.
Edson Cruz
Thank you Mr. Khan. Your articles have been helped a lot.
That’s it.
Doug
Just want to thank you very much for this article. I have spent hours searching for a solution to a
very similar page I am building and I was giving up hope on getting it to work.
Thanks to this page and this site.
Noorulla Khan
Mudassar bhai you rock man really working fine. May Allah Almighty fulfill your most halal desires.
Ameen.
Mahmoud
I would like to thank you for this site. It is good site for learning.
Shatrughan Singh Kshatri
You are always being honest about your work.
Shoni
Since i started using this site i have gained a lot of experience in C and VB.net as i am currently
working on both of them.
This is the best and the explanation is clear. Keep the good work.
Thumbs Up.
Mayank Pathak
Awesome Mudassar ...
I am already a follower of your posts on forums.asp.net....
Nice example mate...It works superb.
Rupesh
Thanks a lot your articles are very nice. It will help the learners a lot.
Ameer Abulawi
This is Great. This website Rocks.
Thank you Mudassar
Emmanuel
I have read many of your post and they have helped me alot. Thanks Khan.
Asif Aziz
http://www.aspsnippets.com/Articles/Select-SQL-Server-Stored-Procedures-using-ASPNet-Example.aspx 16/18
28/10/2014 Select SQL Server Stored Procedures using ASP.Net Example | ASP.Net, C#.Net, VB.Net, AJAX,JQuery, JavaScript, GridView,SQL Server, RDLC, S…

Very nice explanation of Ajax. This was first time I was learning Ajax. I was explained so nicely that
I understand each step in glance
Nabil Elibairy
Very good detailed step by step with explanation of every how and why. We appreciate your
sincere willing to teach.
Mohammad
Appreciated. Thank you very much. You are a great developer.
Javed Ahmad
I have always visited many times your site and always find Something new for Self as Software
Engineer your site is very very useful for Dot Net Platforms user Allah mercy have on you always.
Anonymous
I have to admit it you are great. I found solutions to my ASP.NET problems two times during the
last 2 days.
Thank you very much.
Alexis
Wow. Thank’s your articles are very GOOD!!!
Jagdish
Please add articles like this.
Your all articles are very useful covers most of …
THANK YOU.
Mayur Nagavadiya
Thank you sir.
After visiting your blog now i understand the concept of procedure it is very good for fresher.
Once again Thanks a lot......
Willfred
You are simply awesome Mudassar Khan.....
Thank you very very much :)
Furqan
Amazing website, Great articles, To the point content.
Keep up the good work.
Ariba Ahmed
Its best guidance for me and for my husband...
kashif
Mudassar Allah aap ko duniya aur aakhrat mae bohat bohat ajar dae Ameen,
Aur meri dua a k Allah ji aap ko jannat mae ek khobsorat baag b daen, Bagh-e-Kashaf k naam sa
:) Thank you so much your blog has helped me so many times.
Abhijit
You are Legend my friend.
Sameer Idris
I have to admit it you are great. I found solutions to my ASP.NET problems two times during the
last 2 days.
Thank you very much.
Nick
Nice guide.
The only working guide to do this I have found (that doesn't use MVC).
FLEITES
Maestro-Teacher Thank you very much for your tutorials!
Dmitry
Man I am really very grateful to you The only short and 100 functioning example in the web.
Vijay
Dear Friend Mudassar
I really want to thank to you cause you are not only helping us by your posts you also helping us to
feed our parents.
Thanks and God Bless
Khwaja Mohammad Sayem
Thank you very much for your "Enlarge Image View in ASP.Net GridView". Two days I have spent
for this solution.
http://www.aspsnippets.com/Articles/Select-SQL-Server-Stored-Procedures-using-ASPNet-Example.aspx 17/18
28/10/2014 Select SQL Server Stored Procedures using ASP.Net Example | ASP.Net, C#.Net, VB.Net, AJAX,JQuery, JavaScript, GridView,SQL Server, RDLC, S…

Thanks from heart once again.


Shadab Beg
Dear Mudassar your website is very very helpful and very easy to understand.
Thanks a lot for helping us.
Rahul
You are doing very good things I am very happy with your application.
All examples are very helpful to me.
Thanks Sir.
Pankaj Sankhla
Hi brother your all post on ASPSnippets are awesome....
I really appreciate your work...
This is a great tribute for fresher and beginners. Thanks brother for your so nice work and
thinking...
Gaja
Excellent site. This is the first time I’m visiting here and i was amazed by the samples and the easy
self-understandable code with a little explanations. Hope I will be vising here often. :)
Prasad
Your articles are of great help to students of .NET technology. Great job.
Julio Uresti
Excellent I have been looking for a sample of this.
Your explanation and example are so clear and straight forward.
You should write support docs for Microsoft’s user guides.
Azeem Ahmed
This website is very useful for me. Whenever i got stuck in my ERP application while coding my
second thought will be at ASPSIPPETS.....I am very thankful to the great, logical, talented,
creative etc.Mudassar Khan
Prerna Mishra
Hello sir,
I am a frequent user of your blog and honestly i have learnt a lot. Thank you so much for your
excellent writing.
Thank you so much!
Suma
I will recommend this site to all my friends and juniors..
Best site keep it up...
Aman Jain
Sir you are doing really nice work. I have learnt lot of things from your website and the way you
present source code and demo is really wonderful.
Thank you very much sir.
Keep it up.

Subscribe

Subscribe

© 2014 www.aspsnippets.com All rights reserved | Privacy Policy | Powered by Excelasoft

Solutions

http://www.aspsnippets.com/Articles/Select-SQL-Server-Stored-Procedures-using-ASPNet-Example.aspx 18/18

You might also like