Solved

Gridview Edit Doesn't Work

Posted on 2008-10-04
5
1,117 Views
Last Modified: 2013-11-08
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
Comment
Question by:wrwiii12
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
5 Comments
 
LVL 15

Expert Comment

by:NazoUK
ID: 22643707
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
 
LVL 4

Author Comment

by:wrwiii12
ID: 22647139
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
 
LVL 15

Expert Comment

by:NazoUK
ID: 22647554
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
 
LVL 4

Author Comment

by:wrwiii12
ID: 22656045
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
 
LVL 15

Accepted Solution

by:
NazoUK earned 500 total points
ID: 22657547
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

How Do You Stack Up Against Your Peers?

With today’s modern enterprise so dependent on digital infrastructures, the impact of major incidents has increased dramatically. Grab the report now to gain insight into how your organization ranks against your peers and learn best-in-class strategies to resolve incidents.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

756 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question