Solved

Gridview Edit Doesn't Work

Posted on 2008-10-04
5
1,111 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
  • 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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Populating an array of locale_time_info 8 37
vb.net checkbox 7 41
Help with AsEnumerable(), LINQ 4 22
execute SQL Stored Procedure 1000 times from vb.net 3 22
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).

910 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

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now