Page

Stored Procedure and Return Parameter in asp.net

//ReturnParameterExample.aspx


<%@ Page Language="C#" %>  
<%@ Import Namespace="System.Data.SqlClient" %>  
  
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">  
  
<script runat="server">  
    void SqlDataSource1_Selected(object sender, System.Web.UI.WebControls.SqlDataSourceStatusEventArgs e) {  
        foreach(SqlParameter myParam in e.Command.Parameters)  
        {  
            Label1.Text += myParam.ParameterName.ToString();  
            Label1.Text += ": ";  
            Label1.Text += myParam.Value;  
            Label1.Text += "<br />";  
        }  
    }  
</script>  
  
<html xmlns="http://www.w3.org/1999/xhtml">  
<head runat="server">  
    <title>asp.net Stored Procedure and Return Parameter example</title>  
</head>  
<body>  
    <form id="form1" runat="server">  
    <div>  
        <h2 style="color:Red">Return Parameter Example</h2>  
        <asp:Label ID="Label1" runat="server" Font-Size="Large" ForeColor="DodgerBlue">  
        </asp:Label>  
        <br />  
        <asp:SqlDataSource   
            ID="SqlDataSource1"  
            runat="server"  
            ConnectionString="<%$ ConnectionStrings:NorthwindConnectionString %>"  
            SelectCommand="GetProducts"  
            SelectCommandType="StoredProcedure"  
            OnSelected="SqlDataSource1_Selected"  
            >  
            <SelectParameters>  
                <asp:Parameter Name="TotalProducts" Type="Int32" Direction="ReturnValue" />  
            </SelectParameters>  
        </asp:SqlDataSource>  
        <asp:GridView   
            ID="GridView1"  
            runat="server"  
            DataSourceID="SqlDataSource1"  
            AllowPaging="true"  
            DataKeyNames="ProductID"  
            BackColor="Crimson"  
            ForeColor="AntiqueWhite"  
            BorderColor="LightPink"  
            HeaderStyle-BackColor="DarkOrange"  
            GridLines="Horizontal"  
            Font-Italic="true"  
            >  
        </asp:GridView>  
    </div>  
    </form>  
</body>  
</html>  






GetProducts [Stored Procedure]

CREATE PROCEDURE [dbo].[GetProducts] AS
SELECT ProductID, ProductName, UnitPrice FROM Products

DECLARE @CountProducts smallint
SELECT @CountProducts = Count(ProductID) FROM Products

RETURN @CountProducts

No comments:

Post a Comment