Link to home
Start Free TrialLog in
Avatar of wrwiii12
wrwiii12Flag for United States of America

asked on

Page refresh duplicates inserts and footer totals not updating

Please reference this:
https://www.experts-exchange.com/questions/23787751/Gridview-Edit-Doesn't-Work.html

Working like a charm. BUT! If i add a new employee, it adds it ok. If i refresh the page, it adds it again. Also the totals don't refresh unless i leave the page and re-enter. Just refreshing the page don't do the trick.

Imports Security_Check
Imports System.Data
 
 
Partial Class IT_DigitalCertificate
    Inherits System.Web.UI.Page
    Dim SM(10) As Integer
 
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Dim sss As String
        Dim pf As Boolean
        Dim i As Integer
        Dim j As Integer
        Dim k As Integer
        'this gets current windows user
        sss = User.Identity.Name.ToString
        sss = Mid(sss, InStr(sss, "\") + 1).ToLower
        pf = PASS("DigitalCertificateEDITS", sss)
        'the PASS function determinds whether this user needs to edit the data and
        'whether the user needs to enter data. 
        'this test will either make the edit/delete visible false and the detailsview
        'visible or not! 
        If pf = False Then
            GridView1.Columns(0).Visible = False
            DetailsView1.Visible = False
        End If
 
        Dim arg As DataSourceSelectArguments
        arg = New DataSourceSelectArguments
        Dim view As System.Data.DataView = CType(SqlDataSource1.Select(arg), DataView)
 
        view.RowFilter = "QSE='True'"
        SM(1) = view.ToTable().Rows.Count
        view.RowFilter = "TDSP='True'"
        SM(2) = view.ToTable().Rows.Count
        view.RowFilter = "LSE='True'"
        SM(3) = view.ToTable().Rows.Count
        view.RowFilter = "RES='True'"
        SM(4) = view.ToTable().Rows.Count
        view.RowFilter = "TMPA='True'"
        SM(5) = view.ToTable().Rows.Count
        For k = 1 To 5
            GridView1.FooterRow.Cells(k + 3).Text = SM(k).ToString
        Next
        GridView1.FooterRow.Cells(0).Text = "Totals"
    End Sub
End Class
'=========================================
'=========================================
<%@ Page Language="VB" MasterPageFile="~/MasterPage.master" AutoEventWireup="false" CodeFile="DigitalCertificate.aspx.vb" Inherits="IT_DigitalCertificate" title="Digital Certificates Administration" %>
 
<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
 
    <asp:SqlDataSource ID="SqlDataSource1" runat="server" 
        ConnectionString="<%$ ConnectionStrings:IntranetConnectionString %>" 
        DeleteCommand="DELETE FROM [DigitalCertificates] WHERE [id] = @id" 
        InsertCommand="INSERT INTO [DigitalCertificates] ([LastName], [FirstName], [QSE], [TDSP], [LSE], [RES], [TMPA], [EmployeeID], [ExpirationDate]) VALUES (@LastName, @FirstName, @QSE, @TDSP, @LSE, @RES, @TMPA, @EmployeeID, @ExpirationDate)" 
        SelectCommand="SELECT * FROM [DigitalCertificates] ORDER BY [LastName]" 
        UpdateCommand="UPDATE [DigitalCertificates] SET [LastName] = @LastName, [FirstName] = @FirstName, [QSE] = @QSE, [TDSP] = @TDSP, [LSE] = @LSE, [RES] = @RES, [TMPA] = @TMPA, [EmployeeID] = @EmployeeID, [ExpirationDate] = @ExpirationDate WHERE [id] = @id">
        <DeleteParameters>
            <asp:Parameter Name="id" Type="Byte" />
        </DeleteParameters>
        <InsertParameters>
            <asp:Parameter Name="LastName" Type="String" />
            <asp:Parameter Name="FirstName" Type="String" />
            <asp:Parameter Name="QSE" Type="Boolean" />
            <asp:Parameter Name="TDSP" Type="Boolean" />
            <asp:Parameter Name="LSE" Type="Boolean" />
            <asp:Parameter Name="RES" Type="Boolean" />
            <asp:Parameter Name="TMPA" Type="Boolean" />
            <asp:Parameter Name="EmployeeID" Type="String" />
            <asp:Parameter Name="ExpirationDate" Type="DateTime" />
        </InsertParameters>
    </asp:SqlDataSource>
 
        <ContentTemplate>
            <asp:GridView ID="GridView1" runat="server" 
    AutoGenerateColumns="False" BackColor="White" BorderColor="#DEDFDE" 
    BorderStyle="None" BorderWidth="1px" CellPadding="4" DataKeyNames="id" 
    DataSourceID="SqlDataSource1" ForeColor="Black" GridLines="Vertical" 
    ShowFooter="True">
                <FooterStyle BackColor="#CCCC99" />
                <RowStyle BackColor="#F7F7DE" />
                <Columns>
                    <asp:CommandField ShowDeleteButton="True" 
                ShowEditButton="True" />
                    <asp:BoundField DataField="id" HeaderText="id" 
                ReadOnly="True" SortExpression="id" />
                    <asp:BoundField DataField="LastName" 
                HeaderText="LastName" SortExpression="LastName" />
                    <asp:BoundField DataField="FirstName" 
                HeaderText="FirstName" SortExpression="FirstName" />
                    <asp:CheckBoxField DataField="QSE" HeaderText="QSE" 
                SortExpression="QSE" />
                    <asp:CheckBoxField DataField="TDSP" HeaderText="TDSP" 
                SortExpression="TDSP" />
                    <asp:CheckBoxField DataField="LSE" HeaderText="LSE" 
                SortExpression="LSE" />
                    <asp:CheckBoxField DataField="RES" HeaderText="RES" 
                SortExpression="RES" />
                    <asp:CheckBoxField DataField="TMPA" HeaderText="TMPA" 
                SortExpression="TMPA" />
                    <asp:BoundField DataField="EmployeeID" 
                HeaderText="EmployeeID" SortExpression="EmployeeID" />
                    <asp:BoundField DataField="ExpirationDate" 
                HeaderText="ExpirationDate" SortExpression="ExpirationDate" />
                </Columns>
                <PagerStyle BackColor="#F7F7DE" ForeColor="Black" 
            HorizontalAlign="Right" />
                <SelectedRowStyle BackColor="#CE5D5A" Font-Bold="True" 
            ForeColor="White" />
                <HeaderStyle BackColor="#6B696B" Font-Bold="True" 
            ForeColor="White" />
                <AlternatingRowStyle BackColor="White" />
            </asp:GridView>
        <br />
    <asp:DetailsView ID="DetailsView1" runat="server" AutoGenerateRows="False" 
        BackColor="White" BorderColor="#E7E7FF" BorderStyle="None" BorderWidth="1px" 
        CellPadding="3" DataKeyNames="id" DataSourceID="SqlDataSource1" 
        DefaultMode="Insert" GridLines="Horizontal" Height="50px" Width="125px">
        <FooterStyle BackColor="#B5C7DE" ForeColor="#4A3C8C" />
        <RowStyle BackColor="#E7E7FF" ForeColor="#4A3C8C" />
        <PagerStyle BackColor="#E7E7FF" ForeColor="#4A3C8C" HorizontalAlign="Right" />
        <Fields>
            <asp:BoundField DataField="id" HeaderText="id" ReadOnly="True" 
                SortExpression="id" Visible="False" />
            <asp:BoundField DataField="LastName" HeaderText="LastName" 
                SortExpression="LastName" />
            <asp:BoundField DataField="FirstName" HeaderText="FirstName" 
                SortExpression="FirstName" />
            <asp:CheckBoxField DataField="QSE" HeaderText="QSE" SortExpression="QSE" />
            <asp:CheckBoxField DataField="TDSP" HeaderText="TDSP" SortExpression="TDSP" />
            <asp:CheckBoxField DataField="LSE" HeaderText="LSE" SortExpression="LSE" />
            <asp:CheckBoxField DataField="RES" HeaderText="RES" SortExpression="RES" />
            <asp:CheckBoxField DataField="TMPA" HeaderText="TMPA" SortExpression="TMPA" />
            <asp:BoundField DataField="EmployeeID" HeaderText="EmployeeID" 
                SortExpression="EmployeeID" />
            <asp:BoundField DataField="ExpirationDate" HeaderText="ExpirationDate" 
                SortExpression="ExpirationDate" />
            <asp:CommandField ShowInsertButton="True" />
        </Fields>
        <HeaderStyle BackColor="#4A3C8C" Font-Bold="True" ForeColor="#F7F7F7" />
        <EditRowStyle BackColor="#738A9C" Font-Bold="True" ForeColor="#F7F7F7" />
        <AlternatingRowStyle BackColor="#F7F7F7" />
    </asp:DetailsView>
        </ContentTemplate>
 
</asp:Content>

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of NazoUK
NazoUK
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Sorry that should have said Add an OnItemInserted event for your DetailsView
Avatar of wrwiii12

ASKER

Still not working? I tried the OnItemInserted even on the DetailsView but the totals are removed.
Imports Security_Check
Imports System.Data
 
 
Partial Class IT_DigitalCertificate
    Inherits System.Web.UI.Page
    Dim SM(10) As Integer
 
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Dim sss As String
        Dim pf As Boolean
        Dim i As Integer
        Dim j As Integer
        Dim k As Integer
 
        sss = User.Identity.Name.ToString
        sss = Mid(sss, InStr(sss, "\") + 1).ToLower
        pf = PASS("DigitalCertificateEDITS", sss)
 
        If pf = False Then
            GridView1.Columns(0).Visible = False
            DetailsView1.Visible = False
        End If
        If Not Page.IsPostBack Then
            UPDATE_COUNT()
        End If
    End Sub
 
    Public Sub UPDATE_COUNT()
        Dim arg As DataSourceSelectArguments
        arg = New DataSourceSelectArguments
        Dim view As System.Data.DataView = CType(SqlDataSource1.Select(arg), DataView)
 
        view.RowFilter = "QSE='True'"
        SM(1) = view.ToTable().Rows.Count
        view.RowFilter = "TDSP='True'"
        SM(2) = view.ToTable().Rows.Count
        view.RowFilter = "LSE='True'"
        SM(3) = view.ToTable().Rows.Count
        view.RowFilter = "RES='True'"
        SM(4) = view.ToTable().Rows.Count
        view.RowFilter = "TMPA='True'"
        SM(5) = view.ToTable().Rows.Count
 
        For k = 1 To 5
            GridView1.FooterRow.Cells(k + 3).Text = SM(k).ToString
        Next
        GridView1.FooterRow.Cells(0).Text = "Totals"
    End Sub
End Class
'==============================================
<%@ Page Language="VB" MasterPageFile="~/MasterPage.master" AutoEventWireup="false" CodeFile="DigitalCertificate.aspx.vb" Inherits="IT_DigitalCertificate" title="Digital Certificates Administration" %>
 
<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
 
    <asp:SqlDataSource ID="SqlDataSource1" runat="server" 
        ConnectionString="<%$ ConnectionStrings:IntranetConnectionString %>" 
        DeleteCommand="DELETE FROM [DigitalCertificates] WHERE [id] = @id" 
        InsertCommand="INSERT INTO [DigitalCertificates] ([LastName], [FirstName], [QSE], [TDSP], [LSE], [RES], [TMPA], [EmployeeID], [ExpirationDate]) VALUES (@LastName, @FirstName, @QSE, @TDSP, @LSE, @RES, @TMPA, @EmployeeID, @ExpirationDate)" 
        SelectCommand="SELECT * FROM [DigitalCertificates] ORDER BY [LastName]" 
        UpdateCommand="UPDATE [DigitalCertificates] SET [LastName] = @LastName, [FirstName] = @FirstName, [QSE] = @QSE, [TDSP] = @TDSP, [LSE] = @LSE, [RES] = @RES, [TMPA] = @TMPA, [EmployeeID] = @EmployeeID, [ExpirationDate] = @ExpirationDate WHERE [id] = @id">
        <DeleteParameters>
            <asp:Parameter Name="id" Type="Byte" />
        </DeleteParameters>
        <InsertParameters>
            <asp:Parameter Name="LastName" Type="String" />
            <asp:Parameter Name="FirstName" Type="String" />
            <asp:Parameter Name="QSE" Type="Boolean" />
            <asp:Parameter Name="TDSP" Type="Boolean" />
            <asp:Parameter Name="LSE" Type="Boolean" />
            <asp:Parameter Name="RES" Type="Boolean" />
            <asp:Parameter Name="TMPA" Type="Boolean" />
            <asp:Parameter Name="EmployeeID" Type="String" />
            <asp:Parameter Name="ExpirationDate" Type="DateTime" />
        </InsertParameters>
    </asp:SqlDataSource>
 
        <ContentTemplate>
            <asp:Table ID="Table1" runat="server" Width="675px">
            <asp:TableHeaderRow >
            <asp:TableCell><asp:Label ID="Label1" runat="server" Text="DIGITAL CERTIFICATES" Font-Size="XX-Large" ForeColor="Yellow"></asp:Label></asp:TableCell>
            </asp:TableHeaderRow>
    </asp:Table>
            <asp:GridView ID="GridView1" runat="server"
    AutoGenerateColumns="False" BackColor="White" BorderColor="#DEDFDE" 
    BorderStyle="None" BorderWidth="1px" CellPadding="4" DataKeyNames="id" 
    DataSourceID="SqlDataSource1" ForeColor="Black" GridLines="Vertical" 
    ShowFooter="True" Font-Size="Small" Font-Bold="True">
                <FooterStyle BackColor="#CCCC99" />
                <RowStyle BackColor="#F7F7DE" />
                <Columns>
                    <asp:CommandField ShowDeleteButton="True" 
                ShowEditButton="True" />
                    <asp:BoundField DataField="id" HeaderText="id" 
                ReadOnly="True" SortExpression="id" />
                    <asp:BoundField DataField="LastName" 
                HeaderText="LastName" SortExpression="LastName" />
                    <asp:BoundField DataField="FirstName" 
                HeaderText="FirstName" SortExpression="FirstName" />
                    <asp:CheckBoxField DataField="QSE" HeaderText="QSE" 
                SortExpression="QSE" />
                    <asp:CheckBoxField DataField="TDSP" HeaderText="TDSP" 
                SortExpression="TDSP" />
                    <asp:CheckBoxField DataField="LSE" HeaderText="LSE" 
                SortExpression="LSE" />
                    <asp:CheckBoxField DataField="RES" HeaderText="RES" 
                SortExpression="RES" />
                    <asp:CheckBoxField DataField="TMPA" HeaderText="TMPA" 
                SortExpression="TMPA" />
                    <asp:BoundField DataField="EmployeeID" 
                HeaderText="EmployeeID" SortExpression="EmployeeID" />
                    <asp:BoundField DataField="ExpirationDate" 
                HeaderText="ExpirationDate" SortExpression="ExpirationDate" />
                </Columns>
                <PagerStyle BackColor="#F7F7DE" ForeColor="Black" HorizontalAlign="Right" />
                <SelectedRowStyle BackColor="#CE5D5A" Font-Bold="True" ForeColor="White" />
                <HeaderStyle BackColor="#6B696B" Font-Bold="True" ForeColor="White" />
                <AlternatingRowStyle BackColor="White" />
            </asp:GridView>
        <br />
    <asp:DetailsView ID="DetailsView1" runat="server" AutoGenerateRows="False" OnItemInserted="UPDATE_COUNT"  
        BackColor="White" BorderColor="#E7E7FF" BorderStyle="None" BorderWidth="1px" 
        CellPadding="3" DataKeyNames="id" DataSourceID="SqlDataSource1" 
        DefaultMode="Insert" GridLines="Horizontal" Height="50px" Width="125px" 
        Font-Size="X-Small">
        <FooterStyle BackColor="#B5C7DE" ForeColor="#4A3C8C" />
        <RowStyle BackColor="#E7E7FF" ForeColor="#4A3C8C" />
        <PagerStyle BackColor="#E7E7FF" ForeColor="#4A3C8C" HorizontalAlign="Right" />
        <Fields>
            <asp:BoundField DataField="id" HeaderText="id" ReadOnly="True" 
                SortExpression="id" Visible="False" />
            <asp:BoundField DataField="LastName" HeaderText="LastName" 
                SortExpression="LastName" />
            <asp:BoundField DataField="FirstName" HeaderText="FirstName" 
                SortExpression="FirstName" />
            <asp:CheckBoxField DataField="QSE" HeaderText="QSE" SortExpression="QSE" />
            <asp:CheckBoxField DataField="TDSP" HeaderText="TDSP" SortExpression="TDSP" />
            <asp:CheckBoxField DataField="LSE" HeaderText="LSE" SortExpression="LSE" />
            <asp:CheckBoxField DataField="RES" HeaderText="RES" SortExpression="RES" />
            <asp:CheckBoxField DataField="TMPA" HeaderText="TMPA" SortExpression="TMPA" />
            <asp:BoundField DataField="EmployeeID" HeaderText="EmployeeID" 
                SortExpression="EmployeeID" />
            <asp:BoundField DataField="ExpirationDate" HeaderText="ExpirationDate" 
                SortExpression="ExpirationDate" />
            <asp:CommandField ShowInsertButton="True" />
        </Fields>
        <HeaderStyle BackColor="#4A3C8C" Font-Bold="True" ForeColor="#F7F7F7" />
        <EditRowStyle BackColor="#738A9C" Font-Bold="True" ForeColor="#F7F7F7" />
        <AlternatingRowStyle BackColor="#F7F7F7" />
    </asp:DetailsView>
        </ContentTemplate>
</asp:Content>

Open in new window

I added another question to followup on this one.