Atl LED
Atl LED

Reputation: 666

Overload/Override SQLDataSource Update Events

I have a SqlDataSource that loads/selects the User and role information from an ASP.net side quite well. I like the ability to take advantage of the auto generated edit/update link buttons that are formed (the edit event seems perfectly fine, and I'd love to be able to use it).

But, given what I learned yesterday about how I should Insert/Delete into the AspNetUserRoles table, I was wanting to wrap the update of AspNetUsers and the Insert/Delete queries into one transaction.

I was able to get the code to work if I assigned a sub to the GridView.RowUpdating event, but then the "main" update command from the SqlDataSource seems to fire after the fact.

Is there a way to override the automatically generated SqlDataSource events? I looked at this, but it doesn't really get at what I'm trying to do.

I think because I'm trying to update two tables, and for one table use Insert/Delete commands, and because I need to manipulate the variables taking values from the edited row (to lookup the RoleID from the RoleName), that I really need to override it.

So far I've only found examples of people who really didn't need to override the auto-generated scripts, not an example where someone did. In that case, is there a way to change the initial set up for this? I would think not, but perhaps I'm wrong.

Below are the GridView and SqlDataSource I'm using. I think my actual sub is fine because it fires correctly, just not instead of the update command (shoots to soon joke here).

<asp:GridView ID="URdataGridView" runat="server" 
     AllowPaging="True" AllowSorting="True" AutoGenerateColumns="False" 
     CellPadding="4" DataSourceID="MainUser" ForeColor="#333333" 
     GridLines="None" DataKeyNames="Id">
     <AlternatingRowStyle BackColor="White" ForeColor="#284775" />
     <Columns>
         <asp:CommandField ShowDeleteButton="True" ShowEditButton="True" />
         <asp:BoundField DataField="Id" HeaderText="Id" SortExpression="Id" ReadOnly="True" />
         <asp:BoundField DataField="UserName" HeaderText="UserName" SortExpression="UserName" />
         <asp:BoundField DataField="Email" HeaderText="Email" SortExpression="Email" />
         <asp:BoundField DataField="PhoneNumber" HeaderText="PhoneNumber" SortExpression="PhoneNumber" />
         <asp:BoundField DataField="Name" HeaderText="Name" SortExpression="Name" />
         <asp:CheckBoxField DataField="LockoutEnabled" HeaderText="LockoutEnabled" SortExpression="LockoutEnabled" />
     </Columns>
     <EditRowStyle BackColor="#999999" />
     <FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
     <HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
     <PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
     <RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
     <SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
     <SortedAscendingCellStyle BackColor="#E9E7E2" />
     <SortedAscendingHeaderStyle BackColor="#506C8C" />
     <SortedDescendingCellStyle BackColor="#FFFDF8" />
     <SortedDescendingHeaderStyle BackColor="#6F8DAE" />
</asp:GridView>

<asp:SqlDataSource ID="MainUser" runat="server" 
     ConnectionString="<%$ ConnectionStrings:DefaultConnection %>" 
     DeleteCommand="DeleteUserOnlyQuery" DeleteCommandType="StoredProcedure" 
     SelectCommand="SelectUsersRoles" SelectCommandType="StoredProcedure" 
     UpdateCommand="UpdateUserOnlyQuery" UpdateCommandType="StoredProcedure">
    <DeleteParameters>
        <asp:Parameter Name="original_Id" />
        <asp:Parameter Name="original_Email" />
        <asp:Parameter Name="original_UserName" />
        <asp:Parameter Name="original_PhoneNumber" />
        <asp:Parameter Name="original_LockoutEnabled" />
    </DeleteParameters>
    <UpdateParameters>
        <asp:Parameter Name="original_Id" />
        <asp:Parameter Name="Email" />
        <asp:Parameter Name="UserName" />
        <asp:Parameter Name="PhoneNumber" />
        <asp:Parameter Name="LockoutEnabled" />
    </UpdateParameters>
</asp:SqlDataSource>

Upvotes: 0

Views: 711

Answers (1)

Andrei
Andrei

Reputation: 56716

Yes, there is a way to override the Update. For that you need to subscribe to the Updating event, do whatever you want and then either cancel the DB operation, or just proceed. In code:

<asp:SqlDataSource ID="MainUser" runat="server"
                   OnUpdating="MainUser_Updating"
                   ...

protected void MainUser_Updating(Object source, SqlDataSourceCommandEventArgs e)
{
    // here run your custom logic
    // perhaps use e.Command to see parameters and such
    // you can even do DB updates here manually

    // ....

    // if you still want initial DB update to occur, simply do nothing in the end
    // if however you need to cancel the DB operation, indicate that
    e.Cancel = true;        
}

Upvotes: 1

Related Questions