I have a working GridView in an ASP.Net webapp which tracks DNS records I have requested. It’s defined as follows:
<asp:GridView ID="gvExistingEntries" runat="server" AutoGenerateColumns="False"
DataKeyNames="DNSId" DataSourceID="dsDNS" Width="100%"
BackColor="White" BorderColor="#DEDFDE" BorderStyle="None" BorderWidth="1px"
CellPadding="4" ForeColor="Black" GridLines="Vertical" AllowSorting="True" ShowFooter="true">
<AlternatingRowStyle BackColor="White" />
<Columns>
<asp:BoundField DataField="DNSId" Visible="false" ReadOnly="true" />
<asp:BoundField DataField="DNSName" HeaderText="DNS Name" SortExpression="DNSName" />
<asp:BoundField DataField="IPAddress" HeaderText="IP Address" ReadOnly="True" SortExpression="IPAddress" />
<asp:BoundField DataField="RecordType" HeaderText="Record Type" SortExpression="RecordType" />
<asp:BoundField DataField="RequestTicket" HeaderText="Request Ticket" SortExpression="RequestTicket" />
<asp:BoundField DataField="LastUpdateBy" HeaderText="Updated By" ReadOnly="True" SortExpression="LastUpdateBy" />
<asp:TemplateField ShowHeader="false" ItemStyle-HorizontalAlign="Center">
<ItemTemplate>
<asp:LinkButton ID="updateButton" runat="server" CommandName="Edit" Text="Update" />
<asp:LinkButton ID="deleteButton" runat="server" CommandName="Delete" Text="Delete" onClientClick="return confirm('Are you sure you want to delete this entry?');" />
</ItemTemplate>
<EditItemTemplate>
<asp:LinkButton ID="saveUpdate" runat="server" CommandName="Update" Text="Save" onClientClick="return confirm('Are you sure you want to save these changes?');" />
<asp:LinkButton ID="cancelUpdate" runat="server" CommandName="Cancel" Text="Cancel" />
</EditItemTemplate>
</asp:TemplateField>
</Columns>
<FooterStyle BackColor="#CCCC99" />
<HeaderStyle BackColor="#6B696B" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#F7F7DE" ForeColor="Black" HorizontalAlign="Right" />
<RowStyle BackColor="#F7F7DE" />
<SelectedRowStyle BackColor="#CE5D5A" Font-Bold="True" ForeColor="White" />
<SortedAscendingCellStyle BackColor="#FBFBF2" />
<SortedAscendingHeaderStyle BackColor="#848384" />
<SortedDescendingCellStyle BackColor="#EAEAD3" />
<SortedDescendingHeaderStyle BackColor="#575357" />
</asp:GridView>
The datasource is defined as follows:
<asp:SqlDataSource ID="dsDNS" runat="server"
ConnectionString="<%$ ConnectionStrings:DNSMonConnectionString %>"
SelectCommand="SELECT [DNSId], [DNSName], [RecordType], [StartValidityDate], [EndValidityDate], [IPAddress], [LastUpdate], [RequestTicket], (select [FirstName] + ' ' + [LastName] + ' (' + [Login] + ')' FROM [User] where [User].[UserID] = [DomainName].[LastUpdateBy]) AS [LastUpdateBy] FROM [DomainName] ORDER BY [DNSName]"
UpdateCommandType="StoredProcedure"
UpdateCommand="sp_update_DNSEntry" OnUpdating
DeleteCommandType="StoredProcedure"
DeleteCommand="sp_drop_DNSEntry">
<DeleteParameters>
<asp:Parameter Name="DNSId" Type="Int32" />
</DeleteParameters>
<UpdateParameters>
<asp:Parameter Name="DNSId" />
<asp:Parameter Name="DNSName" />
<asp:Parameter Name="RecordType" />
<asp:Parameter Name="IPAddress" />
<asp:Parameter Name="RequestTicket" />
</UpdateParameters>
</asp:SqlDataSource>
Whenever the user uses the update feature this works and saves it to the database, but I want the code to resolve the IP address before it saves it. I have the method ready to go for this, but I do not know how to incorporate that into the code.
I’d rather avoid having the code on the database if possible.
You can update the IP address before the update by using the
OnUpdatingevent.In the handler code, get the Command property from SqlDataSourceCommandEventArgs, and update the appropriate parameter.