CSharp Renaming Parameters Passed To Stored Procedure By Handling Sqldatasource Events – ADO.Net Database

<%@ Page Language="C#" AutoEventWireup="true"%>
<script runat="server">
  void deptSource_Updating(Object sender, System.Web.UI.WebControls.SqlDataSourceCommandEventArgs e)
  {
    e.Command.Parameters["@DeptName"].Value = e.Command.Parameters["@Name"].Value;
    e.Command.Parameters["@DeptGroupName"].Value = e.Command.Parameters["@GroupName"].Value;
    e.Command.Parameters.Remove(e.Command.Parameters["@Name"]);
    e.Command.Parameters.Remove(e.Command.Parameters["@GroupName"]);
  }
</script><html xmlns="http://www.w3.org/1999/xhtml" >
  <head id="Head1" runat="server">
    <title>Renaming Parameters passed to a Stored Procedure by handling the SqlDataSource Events</title>
  </head>
  <body>
    <form id="form1" runat="server">
      <asp:GridView ID="deptView" AllowSorting="true" AllowPaging="true" Runat="server"
        DataSourceID="deptSource" AutoGenerateEditButton="true" 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"
        ConnectionString="<%$ ConnectionStrings:AdventureWorks%>"
        SelectCommand= "SELECT DepartmentID, Name, GroupName, ModifiedDate from HumanResources.Department"
        UpdateCommand="UpdateDepartment" UpdateCommandType="StoredProcedure" OnUpdating="deptSource_Updating" >
        <updateParameters>
            <asp:Parameter Name="DeptName" Type="String" />
            <asp:Parameter Name="DeptGroupName" Type="String" />
          </updateParameters>
     </asp:SqlDataSource>
    </form>
  </body>
</html>

Enjoyed this post? Share it!

 

Leave a comment

Your email address will not be published.