Handling Output Parameters from a Stored Procedure : Stored procedure « ADO.net Database « ASP.NET Tutorial






<%@ Page Language="C#" AutoEventWireup="true"%>
<%@ Import Namespace="System.Text" %>
<script runat="server">
  void deptSource_Selected(Object sender, System.Web.UI.WebControls.SqlDataSourceStatusEventArgs e)  
  {
      StringBuilder builder = new StringBuilder();      
      foreach (System.Data.SqlClient.SqlParameter param in e.Command.Parameters)
      {
          builder.Append(Server.HtmlEncode(param.ParameterName) + "=");
          builder.Append(Server.HtmlEncode(param.Value.ToString()) + " (");          
          builder.Append(Server.HtmlEncode(param.Value.GetType().ToString()) + ")<br>");          
      }
      lblResult.Text = "Output Parameter : " + builder.ToString();
  }
</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
  <head id="Head1" runat="server">
    <title>Handling Output Parameters from a Stored Procedure</title>
  </head>
  <body>
    <form id="form1" runat="server">
      <asp:GridView ID="deptView" AllowSorting="true" AllowPaging="true" Runat="server"
        DataSourceID="deptSource" DataKeyNames="DepartmentID"
        AutoGenerateColumns="False" HeaderStyle-HorizontalAlign="Center" HeaderStyle-Font-Bold="True"
        HeaderStyle-BackColor="blue" HeaderStyle-ForeColor="White">
        <Columns>
          <asp:BoundField ReadOnly="true" HeaderText="Department ID" DataField="DepartmentID" SortExpression="DepartmentID" />
          <asp:BoundField HeaderText="Name" DataField="Name" SortExpression="Name" />
          <asp:BoundField HeaderText="Group Name" DataField="GroupName" SortExpression="GroupName" />
          <asp:BoundField HeaderText="ModifiedDate" DataField="ModifiedDate" SortExpression="ModifiedDate" />
        </Columns>
      </asp:GridView>
      <asp:SqlDataSource ID="deptSource" Runat="server" SelectCommandType="StoredProcedure"
        SelectCommand="GetDepartments"        
        ConnectionString="<%$ ConnectionStrings:AdventureWorks%>" OnSelected="deptSource_Selected">
        <SelectParameters>
          <asp:Parameter Direction="Output" Name="Count" Type="Int32" />          
        </SelectParameters>
      </asp:SqlDataSource>
      <asp:Label runat="server" Font-Bold="true" ID="lblResult" />
    </form>
  </body>
</html>








18.55.Stored procedure
18.55.1.Call stored procedure to query data table (VB.net)
18.55.2.Use SQL Server stored procedures to insert a new record and then retrieve the identity value
18.55.3.Handling Return Parameters from a Stored Procedure
18.55.4.Handling Output Parameters from a Stored Procedure