SqlDataSource - using output parameter values from stored procedure
StoredProcedureOutputParameter.aspx
<%@ Page Language="C#" AutoEventWireup="true" %>
<%@ Import Namespace="System.Text" %>
<!DOCTYPE html>
<script runat="server">
void GridView1_Selected(object sender, SqlDataSourceStatusEventArgs e)
{
StringBuilder txtBuilder = new StringBuilder();
foreach(System.Data.SqlClient.SqlParameter param in e.Command.Parameters)
{
txtBuilder.Append(Server.HtmlEncode(param.ParameterName) + ": Value=");
txtBuilder.Append(Server.HtmlEncode(param.Value.ToString()) + " [");
txtBuilder.Append(Server.HtmlEncode(param.Value.GetType().ToString()) + "]");
Label1.Text = txtBuilder.ToString();
}
}
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
<title>SqlDataSource - using output parameter values from Stored Procedure</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<h2 style="color:Navy; font-style:italic;">SqlDataSource Example: Stored Procedure and Output Parameter</h2>
<asp:SqlDataSource
ID="SqlDataSource1"
runat="server"
ConnectionString="<%$ ConnectionStrings:NorthwindConnectionString %>"
SelectCommand="GetProducts"
SelectCommandType="StoredProcedure"
OnSelected="GridView1_Selected"
>
<SelectParameters>
<asp:Parameter Direction="Output" Name="Count" Type="Int32" />
</SelectParameters>
</asp:SqlDataSource>
<asp:Label
ID="Label1"
runat="server"
Font-Bold="true"
Font-Italic="true"
Font-Size="Large"
ForeColor="BurlyWood"
>
</asp:Label>
<br /><br />
<asp:GridView
ID="GridView1"
runat="server"
DataSourceID="SqlDataSource1"
AutoGenerateColumns="false"
AllowPaging="true"
PageSize="8"
BorderColor="Salmon"
Font-Names="Comic Sans MS"
Width="650"
>
<HeaderStyle BackColor="OrangeRed" ForeColor="Snow" Height="45"/>
<RowStyle BackColor="DeepPink" ForeColor="Snow" Font-Italic="true" />
<PagerStyle
Height="45"
HorizontalAlign="Right"
BackColor="BurlyWood"
Font-Bold="true"
Font-Size="X-Large"
ForeColor="Snow"
/>
<PagerSettings Mode="Numeric" />
<Columns>
<asp:BoundField HeaderText="Product ID" DataField="ProductID" />
<asp:BoundField HeaderText="Product Name" DataField="ProductName" />
<asp:BoundField HeaderText="Unit Price" DataField="UnitPrice" DataFormatString="{0:C}" />
<asp:BoundField HeaderText="Quantity Per Unit" DataField="QuantityPerUnit" />
</Columns>
</asp:GridView>
</div>
</form>
</body>
</html>
GetProducts [Stored Procedure]
CREATE PROCEDURE [dbo]. [GetProducts]
@Count smallint OUTPUT
AS
SELECT ProductID, ProductName, QuantityPerUnit, UnitPrice FROM Products
SELECT @Count = Count(ProductID) FROM Products


- How to select records using StoredProcedure and GridView in asp.net
- SqlDataSource SelectCommandType Text and StoredProcedure using in a asp.net page
- DataAdapter example: how to use DataAdapter in asp.net
- GridView example: how to populate GridView from SqlDataSource in asp.net
- How to use SelectParameters and asp:Parameter in SqlDataSource
- How to delete records (data) using GridView and SqlDataSource