Hi phonghue,
Check this example and take its reference.
Database
For this example I have used of Northwind database that you can download using the link given below.
Download Northwind Database
SQL
CREATE PROCEDURE [dbo].[City]
AS
SELECT DISTINCT TOP 10 City
FROM Customers
WHERE City IS NOT NULL
GO
CREATE PROCEDURE [dbo].[city_Sp]
AS
SELECT DISTINCT TOP 5 City
FROM Customers
WHERE City IS NOT NULL
HTML
<asp:Button Text="A" runat="server" ID="btnA" OnClick="OnA" />
<asp:Button Text="B" runat="server" ID="btnB" OnClick="OnB" /><br /><br />
<asp:GridView runat="server" ID="gvCities" AutoGenerateColumns="false">
<Columns>
<asp:BoundField DataField="City" HeaderText="City" />
</Columns>
</asp:GridView>
Namespaces
C#
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
VB.Net
Imports System.Configuration
Imports System.Data
Imports System.Data.SqlClient
Code
C#
protected void OnA(object sender, EventArgs e)
{
GetCity("City");
}
protected void OnB(object sender, EventArgs e)
{
GetCity("city_Sp");
}
private void GetCity(string procedure)
{
using (SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["conString"].ConnectionString))
{
using (SqlCommand cmd = new SqlCommand(procedure, con))
{
cmd.CommandType = CommandType.StoredProcedure;
using (SqlDataAdapter sda = new SqlDataAdapter())
{
sda.SelectCommand = cmd;
DataTable dt = new DataTable();
sda.Fill(dt);
gvCities.DataSource = dt;
gvCities.DataBind();
}
}
}
}
VB.Net
Protected Sub OnA(ByVal sender As Object, ByVal e As EventArgs)
GetCity("City")
End Sub
Protected Sub OnB(ByVal sender As Object, ByVal e As EventArgs)
GetCity("city_Sp")
End Sub
Private Sub GetCity(ByVal procedure As String)
Using con As SqlConnection = New SqlConnection(ConfigurationManager.ConnectionStrings("conString").ConnectionString)
Using cmd As SqlCommand = New SqlCommand(procedure, con)
cmd.CommandType = CommandType.StoredProcedure
Using sda As SqlDataAdapter = New SqlDataAdapter()
sda.SelectCommand = cmd
Dim dt As DataTable = New DataTable()
sda.Fill(dt)
gvCities.DataSource = dt
gvCities.DataBind()
End Using
End Using
End Using
End Sub
Screenshot