[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1132
  • Last Modified:

Gridview Edit Doesn't Work

I'm using VB.net for any programming. Please review my attached source and see why the edits are not working. No error, just not accepting the row change.
<%@ Page Language="VB" MasterPageFile="~/MasterPage.master" AutoEventWireup="false" CodeFile="DigitalCertificates.aspx.vb" Inherits="IT_DigitalCertificates" title="Untitled Page" %>
 
<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
    <asp:Label ID="Label5" runat="server" Text="Digital Certificates" 
        ForeColor="Yellow" Font-Size="XX-Large"></asp:Label>
    <br />
    <asp:SqlDataSource ID="SqlDataSource1" runat="server" 
        ConnectionString="<%$ ConnectionStrings:IntranetConnectionString %>" 
        DeleteCommand="DELETE FROM [DigitalCertificates] WHERE [LastName] = @LastName AND [FirstName] = @FirstName" 
        InsertCommand="INSERT INTO [DigitalCertificates] ([EmployeeID], [LastName], [FirstName], [QSE], [TDSP], [LSE], [RES], [TMPA], [Expiration_Date]) VALUES (@EmployeeID, @LastName, @FirstName, @QSE, @TDSP, @LSE, @RES, @TMPA, @Expiration_Date)" 
        SelectCommand="SELECT * FROM [DigitalCertificates] ORDER BY [LastName]" 
        
        UpdateCommand="UPDATE [DigitalCertificates] SET [EmployeeID] = @EmployeeID, [QSE] = @QSE, [TDSP] = @TDSP, [LSE] = @LSE, [RES] = @RES, [TMPA] = @TMPA, [Expiration_Date] = @Expiration_Date WHERE [LastName] = @LastName AND [FirstName] = @FirstName">
        <DeleteParameters>
            <asp:Parameter Name="LastName" Type="String" />
            <asp:Parameter Name="FirstName" Type="String" />
        </DeleteParameters>
        <UpdateParameters>
            <asp:Parameter Name="EmployeeID" 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="Expiration_Date" Type="String" />
            <asp:Parameter Name="LastName" Type="String" />
            <asp:Parameter Name="FirstName" Type="String" />
        </UpdateParameters>
        <InsertParameters>
            <asp:Parameter Name="EmployeeID" Type="String" />
            <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="Expiration_Date" Type="String" />
        </InsertParameters>
    </asp:SqlDataSource>
    <br />
    <asp:GridView ID="GridView1" runat="server" AllowSorting="True" 
        AutoGenerateColumns="False" BackColor="White" BorderColor="#DEDFDE" 
        BorderStyle="None" BorderWidth="1px" CellPadding="4" DataSourceID="SqlDataSource1" 
        ForeColor="Black" GridLines="Vertical" ShowFooter="True">
        <FooterStyle BackColor="#CCCC99" />
        <RowStyle BackColor="#F7F7DE" />
        <Columns>
           <asp:CommandField ShowDeleteButton="True" ShowEditButton="True" 
                ShowHeader="True" />
            <asp:BoundField DataField="EmployeeID" HeaderText="EmployeeID" FooterText="Total Qty" 
                SortExpression="EmployeeID" />
            <asp:BoundField DataField="LastName" HeaderText="LastName" ReadOnly="True" 
                SortExpression="LastName" />
            <asp:BoundField DataField="FirstName" HeaderText="FirstName" ReadOnly="True" 
                SortExpression="FirstName" />
            <asp:TemplateField HeaderText="Expiration_Date" 
                SortExpression="Expiration_Date">
                <EditItemTemplate>
                    <asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("Expiration_Date") %>'></asp:TextBox>
                </EditItemTemplate>
                <ItemTemplate>
                    <asp:Label ID="Label1" runat="server" 
                        Text='<%# Bind("Expiration_Date", "{0:MM-dd-yyyy}") %>'></asp:Label>
                </ItemTemplate>
            </asp:TemplateField>
<%----------------------------------------------------------------------%>
            <asp:TemplateField HeaderText="QSE" SortExpression="QSE">
                <EditItemTemplate>
                    <asp:CheckBox ID="CheckBox1" runat="server" Checked='<%# Bind("QSE") %>' />
                </EditItemTemplate>
                <ItemTemplate>
                    <asp:CheckBox ID="CheckBox1" runat="server" Checked='<%# Bind("QSE") %>' 
                        Enabled="false" />
                </ItemTemplate>
                <FooterTemplate>
                    <asp:Label ID="Label1" runat="server"  ></asp:Label>
                </FooterTemplate>
            </asp:TemplateField>
<%----------------------------------------------------------------------%>
             <asp:TemplateField HeaderText="TDSP" SortExpression="TDSP">
                <EditItemTemplate>
                    <asp:CheckBox ID="CheckBox2" runat="server" Checked='<%# Bind("TDSP") %>' />
                </EditItemTemplate>
                <ItemTemplate>
                    <asp:CheckBox ID="CheckBox2" runat="server" Checked='<%# Bind("TDSP") %>' 
                        Enabled="false" />
                </ItemTemplate>
                <FooterTemplate>
                    <asp:Label ID="Label1" runat="server"  ></asp:Label>
                </FooterTemplate>
            </asp:TemplateField>
<%----------------------------------------------------------------------%>            
            <asp:TemplateField HeaderText="LSE" SortExpression="LSE">
                <EditItemTemplate>
                    <asp:CheckBox ID="CheckBox3" runat="server" Checked='<%# Bind("LSE") %>' />
                </EditItemTemplate>
                <ItemTemplate>
                    <asp:CheckBox ID="CheckBox3" runat="server" Checked='<%# Bind("LSE") %>' 
                        Enabled="false" />
                </ItemTemplate>
                <FooterTemplate>
                    <asp:Label ID="Label1" runat="server"  ></asp:Label>
                </FooterTemplate>
            </asp:TemplateField>
<%----------------------------------------------------------------------%>            
            <asp:TemplateField HeaderText="RES" SortExpression="RES">
                <EditItemTemplate>
                    <asp:CheckBox ID="CheckBox4" runat="server" Checked='<%# Bind("RES") %>' />
                </EditItemTemplate>
                <ItemTemplate>
                    <asp:CheckBox ID="CheckBox4" runat="server" Checked='<%# Bind("RES") %>' 
                        Enabled="false" />
                </ItemTemplate>
                 <FooterTemplate>
                    <asp:Label ID="Label1" runat="server"  ></asp:Label>
                </FooterTemplate>
            </asp:TemplateField>
<%----------------------------------------------------------------------%>            
            <asp:TemplateField HeaderText="TMPA" SortExpression="TMPA">
                <EditItemTemplate>
                    <asp:CheckBox ID="CheckBox5" runat="server" Checked='<%# Bind("TMPA") %>' />
                </EditItemTemplate>
                <ItemTemplate>
                    <asp:CheckBox ID="CheckBox5" runat="server" Checked='<%# Bind("TMPA") %>' 
                        Enabled="false" />
                </ItemTemplate>
                <FooterTemplate>
                    <asp:Label ID="Label1" runat="server"  ></asp:Label>
                </FooterTemplate>                
            </asp:TemplateField>
        </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>
    </asp:Content>

Open in new window

0
wrwiii12
Asked:
wrwiii12
  • 3
  • 2
1 Solution
 
NazoUKCommented:
Try putting "FirstName,LastName" into the DataKeysName property of the gridview. This will supply these 2 values to your update statement
Also you don't need to add anything to the updateparameters of the sqldatasource unless you are programatically manipulating them. A call to Bind will automatically match up column names with parameters. This will help you see if any parameters aren't being matched up.

I suspect what is happening is that because you have a FirstName and LastName declared in your objectdatasource but nothing to put in them your sql statemement is being evaluated as WHERE FirstName=NULL AND LastName=NULL which obviously isn't going to work.
0
 
wrwiii12Author Commented:
I deleted my SqlDataSource and the Gridview and created them fresh. This time I did not add the EmployeeID or the Expiration Date. I just added the Lname, Fname, and the booleans. I was thinking it had to do with the text or the date. No luck. I have attached my code as mentioned here. I've done this before, I don't know why I'm having so much trouble now.
<%@ Page Language="VB" MasterPageFile="~/MasterPage.master" AutoEventWireup="false" CodeFile="DigitalCertificates.aspx.vb" Inherits="IT_DigitalCertificates" title="Untitled Page" %>
 
<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
    <asp:Label ID="Label5" runat="server" Text="Digital Certificates" 
        ForeColor="Yellow" Font-Size="XX-Large"></asp:Label>
    <br />
    <asp:SqlDataSource ID="SqlDataSource1" runat="server" 
        ConnectionString="<%$ ConnectionStrings:IntranetConnectionString %>" 
        DeleteCommand="DELETE FROM [DigitalCertificates] WHERE [LastName] = @LastName AND [FirstName] = @FirstName" 
        InsertCommand="INSERT INTO [DigitalCertificates] ([LastName], [FirstName], [QSE], [TDSP], [LSE], [RES], [TMPA]) VALUES (@LastName, @FirstName, @QSE, @TDSP, @LSE, @RES, @TMPA)" 
        SelectCommand="SELECT [LastName], [FirstName], [QSE], [TDSP], [LSE], [RES], [TMPA] FROM [DigitalCertificates] ORDER BY [LastName]" 
        UpdateCommand="UPDATE [DigitalCertificates] SET [QSE] = @QSE, [TDSP] = @TDSP, [LSE] = @LSE, [RES] = @RES, [TMPA] = @TMPA WHERE [LastName] = @LastName AND [FirstName] = @FirstName">
        <DeleteParameters>
            <asp:Parameter Name="LastName" Type="String" />
            <asp:Parameter Name="FirstName" Type="String" />
        </DeleteParameters>
        <UpdateParameters>
            <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="LastName" Type="String" />
            <asp:Parameter Name="FirstName" Type="String" />
        </UpdateParameters>
        <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" />
        </InsertParameters>
    </asp:SqlDataSource>
    <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" 
        BackColor="White" BorderColor="#DEDFDE" BorderStyle="None" BorderWidth="1px" 
        CellPadding="4" DataKeyNames="LastName,FirstName" DataSourceID="SqlDataSource1" 
        ForeColor="Black" GridLines="Vertical">
        <FooterStyle BackColor="#CCCC99" />
        <RowStyle BackColor="#F7F7DE" />
        <Columns>
            <asp:CommandField ShowDeleteButton="True" ShowEditButton="True" />
            <asp:BoundField DataField="LastName" HeaderText="LastName" ReadOnly="True" 
                SortExpression="LastName" />
            <asp:BoundField DataField="FirstName" HeaderText="FirstName" ReadOnly="True" 
                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" />
        </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:Content>

Open in new window

0
 
NazoUKCommented:
Can't see anything wrong there, anything going on in the codebehind that might interfere?
And again I recommend you delete the updateparameters from your sqldatasource for clarity.
0
 
wrwiii12Author Commented:
Here's my latest code. It works if I don't put the VB code along with it. All I'm doing is on page load, counting the number of True's in each column and updating the cells in the footer.
<%@ Page Language="VB" MasterPageFile="~/MasterPage.master" AutoEventWireup="false" CodeFile="DigitalCertificate.aspx.vb" Inherits="IT_DigitalCertificate" title="Untitled Page" %>
 
<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>
    <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>
 
</asp:Content>
 
'=========================================================================
'vb code
'========================================================================
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 i As Integer
        Dim j As Integer
        Dim k As Integer
 
 
 
        countIT()
        SqlDataSource1.SelectCommand = "select * from digitalCertificates"
        GridView1.DataBind()
        For k = 1 To 5
            GridView1.FooterRow.Cells(k + 3).Text = SM(k).ToString
        Next
        GridView1.FooterRow.Cells(0).Text = "Totals"
    End Sub
    Public Sub countIT()
 
        SqlDataSource1.SelectCommand = "select * from digitalCertificates where QSE='True'"
        GridView1.DataBind()
        SM(1) = GridView1.Rows.Count
        SqlDataSource1.SelectCommand = "select * from digitalCertificates where TDSP='True'"
        GridView1.DataBind()
        SM(2) = GridView1.Rows.Count
        SqlDataSource1.SelectCommand = "select * from digitalCertificates where LSE='True'"
        GridView1.DataBind()
        SM(3) = GridView1.Rows.Count
        SqlDataSource1.SelectCommand = "select * from digitalCertificates where RES='True'"
        GridView1.DataBind()
        SM(4) = GridView1.Rows.Count
        SqlDataSource1.SelectCommand = "select * from digitalCertificates where TMPA='True'"
        GridView1.DataBind()
        SM(5) = GridView1.Rows.Count
    End Sub
End Class
 
' I would try a different way to update these counts if someone would point me in the right direction. Thanks!

Open in new window

0
 
NazoUKCommented:
That code is definitely the problem, rebinding the gridview every page_load will cause any events to fail.  If you wrap the code in a if not page.ispostback condition it should fix the problem. Your calculated values should still show up as they will be saved in viewstate.

Perhaps a better way to calculate your totals is to use a dataview to filter all the rows from your sqldatasource:

Dim view as System.Data.DataView = Ctype(SqlDataSource1.Select(),DataView)
view.RowFilter = "QSE='True'"
SM(1) = view.ToTable().Rows.Count
view.RowFilter = "TDSP='True'"
SM(2) = view.ToTable().Rows.Count

... and so on. This will result in fewer hits to your database.
0

Featured Post

The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now