locked
DataGrid Update Not Working RRS feed

  • Question

  • User-1194755429 posted

    I have a datagrid that will not update using the update command. The delete command works great, but the update command returns the original value. I have checked that the DataKeyName matches the key in the database.

    I have a dropdown on the page that is used to select a WorkOrder, which then displays the records in the datagrid. My guess is that it has something to do with a postback that forces the datagrid to the original value from the dropdown instead of the update information.

    Any help would be appreciated. Thanks.

    <%@ Page Title="Blah - Work Orders" Language="VB" MasterPageFile="~/MasterPages/Frontend.master" AutoEventWireup="false" CodeFile="WorkOrderDetail.aspx.vb" Inherits="WorkOrders_Default" %>
    
    <asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server">
    </asp:Content>
    <asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
        <section class="container">
        <h1>Work Order Display</h1>
        <asp:DropDownList ID="WorkOrder" runat="server" AutoPostBack="True" DataSourceID="SqlDataSource1" DataTextField="WorkOrder" DataValueField="WorkOrder" CssClass="pull-right" Width="89px">
          <asp:ListItem Selected="True">Work Orders</asp:ListItem>
        </asp:DropDownList>
        
        <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:fpimsConnectionString %>" ProviderName="<%$ ConnectionStrings:fpimsConnectionString.ProviderName %>" 
            SelectCommand="SELECT [ID], [WorkOrder] FROM [WorkOrders]"></asp:SqlDataSource>
    
    
            <asp:Label ID="WordOrderLabel" runat="server" AssociatedControlID="WorkOrder" Text="Work Order"></asp:Label>
    
            <br />
            <asp:FormView ID="FormView1" runat="server" DataKeyNames="WorkOrder" DataSourceID="SqlDataSource3">
                <EditItemTemplate>
                    ID:
                    <asp:Label ID="IDLabel1" runat="server" Text='<%# Eval("ID") %>' />
                    <br />
                    WorkOrder:
                    <asp:Label ID="WorkOrderLabel1" runat="server" Text='<%# Eval("WorkOrder") %>' />
                    <br />
                    CustomerNumber:
                    <asp:TextBox ID="CustomerNumberTextBox" runat="server" Text='<%# Bind("CustomerNumber") %>' />
                    <br />
                    Customer_Name:
                    <asp:TextBox ID="Customer_NameTextBox" runat="server" Text='<%# Bind("Customer_Name") %>' />
                    <br />
                    OrderDate:
                    <asp:TextBox ID="OrderDateTextBox" runat="server" Text='<%# Bind("OrderDate") %>' />
                    <br />
                    OrderShipDate:
                    <asp:TextBox ID="OrderShipDateTextBox" runat="server" Text='<%# Bind("OrderShipDate") %>' />
                    <br />
                    OrderStatus:
                    <asp:TextBox ID="OrderStatusTextBox" runat="server" Text='<%# Bind("OrderStatus") %>' />
                    <br />
                    OrderNotes:
                    <asp:TextBox ID="OrderNotesTextBox" runat="server" Text='<%# Bind("OrderNotes") %>' />
                    <br />
                    OrderFiles:
                    <asp:TextBox ID="OrderFilesTextBox" runat="server" Text='<%# Bind("OrderFiles") %>' />
                    <br />
                    <asp:LinkButton ID="UpdateButton" runat="server" CausesValidation="True" CommandName="Update" Text="Update" />
                    &nbsp;<asp:LinkButton ID="UpdateCancelButton" runat="server" CausesValidation="False" CommandName="Cancel" Text="Cancel" />
                </EditItemTemplate>
                <InsertItemTemplate>
                    WorkOrder:
                    <asp:TextBox ID="WorkOrderTextBox" runat="server" Text='<%# Bind("WorkOrder") %>' />
                    <br />
                    CustomerNumber:
                    <asp:TextBox ID="CustomerNumberTextBox" runat="server" Text='<%# Bind("CustomerNumber") %>' />
                    <br />
                    Customer_Name:
                    <asp:TextBox ID="Customer_NameTextBox" runat="server" Text='<%# Bind("Customer_Name") %>' />
                    <br />
                    OrderDate:
                    <asp:TextBox ID="OrderDateTextBox" runat="server" Text='<%# Bind("OrderDate") %>' />
                    <br />
                    OrderShipDate:
                    <asp:TextBox ID="OrderShipDateTextBox" runat="server" Text='<%# Bind("OrderShipDate") %>' />
                    <br />
                    OrderStatus:
                    <asp:TextBox ID="OrderStatusTextBox" runat="server" Text='<%# Bind("OrderStatus") %>' />
                    <br />
                    OrderNotes:
                    <asp:TextBox ID="OrderNotesTextBox" runat="server" Text='<%# Bind("OrderNotes") %>' />
                    <br />
                    OrderFiles:
                    <asp:TextBox ID="OrderFilesTextBox" runat="server" Text='<%# Bind("OrderFiles") %>' />
                    <br />
                    <asp:LinkButton ID="InsertButton" runat="server" CausesValidation="True" CommandName="Insert" Text="Insert" />
                    &nbsp;<asp:LinkButton ID="InsertCancelButton" runat="server" CausesValidation="False" CommandName="Cancel" Text="Cancel" />
                </InsertItemTemplate>
                <ItemTemplate>
                    Work Order:
                    <asp:Label ID="WorkOrderLabel" runat="server" Text='<%# Eval("WorkOrder") %>' />
                    <br />
                    Customer Number:
                    <asp:Label ID="CustomerNumberLabel" runat="server" Text='<%# Bind("CustomerNumber") %>' />
                    <br />
                    Customer Name:
                    <asp:Label ID="Customer_NameLabel" runat="server" Text='<%# Bind("Customer_Name") %>' />
                    <br />
                    Order Date:
                    <asp:Label ID="OrderDateLabel" runat="server" Text='<%# Bind("OrderDate") %>' />
                    <br />
                    Order Ship Date:
                    <asp:Label ID="OrderShipDateLabel" runat="server" Text='<%# Bind("OrderShipDate") %>' />
                    <br />
                    Order Status:
                    <asp:Label ID="OrderStatusLabel" runat="server" Text='<%# Bind("OrderStatus") %>' />
                    <br />
                    Order Notes:
                    <asp:Label ID="OrderNotesLabel" runat="server" Text='<%# Bind("OrderNotes") %>' />
                    <br />
                    Order Files:
                    <asp:Label ID="OrderFilesLabel" runat="server" Text='<%# Bind("OrderFiles") %>' />
                    <br />
    
                </ItemTemplate>
            </asp:FormView>
    
            <br />
        <asp:GridView ID="GridView1" runat="server" AllowPaging="True" AllowSorting="True" AutoGenerateColumns="False" DataSourceID="SqlDataSource2" DataKeyNames="ID" EmptyDataText="No parts selected. Add a part below">
            <Columns>
                <asp:CommandField ShowDeleteButton="True" ShowEditButton="True" />
                <asp:BoundField DataField="ID" HeaderText="ID" InsertVisible="False" ReadOnly="True" SortExpression="ID" />
                <asp:BoundField DataField="WorkOrder" HeaderText="WorkOrder" SortExpression="WorkOrder" />
                <asp:BoundField DataField="PartNumber" HeaderText="PartNumber" SortExpression="PartNumber" />
                <asp:BoundField DataField="Description" HeaderText="Description" SortExpression="Description" />
                <asp:BoundField DataField="Quantity" HeaderText="Quantity" SortExpression="Quantity" />
            </Columns>
        </asp:GridView>
            <asp:SqlDataSource ID="SqlDataSource2" runat="server" ConnectionString="<%$ ConnectionStrings:fpimsConnectionString %>" 
                DeleteCommand="DELETE FROM [WorkOrdersItems] WHERE [ID] = @ID" 
                InsertCommand="INSERT INTO [WorkOrdersItems] ([ID], [WorkOrder], [PartNumber], [Description], [Quantity]) VALUES (?, ?, ?, ?, ?)" 
                ProviderName="<%$ ConnectionStrings:fpimsConnectionString.ProviderName %>" 
                SelectCommand="SELECT [ID], [WorkOrder], [PartNumber], [Description], [Quantity] FROM [WorkOrdersItems] WHERE ([WorkOrder] = ?)" 
                UpdateCommand="UPDATE [WorkOrdersItems] SET [Quantity] = @Quantity WHERE [ID] = @ID">
                <DeleteParameters>
                    <asp:Parameter Name="ID" Type="Int32" />
                </DeleteParameters>
                <InsertParameters>
                    <asp:Parameter Name="ID" Type="Int32" />
                    <asp:Parameter Name="WorkOrder" Type="String" />
                    <asp:Parameter Name="PartNumber" Type="String" />
                    <asp:Parameter Name="Description" Type="String" />
                    <asp:Parameter Name="Quantity" Type="Decimal" />
                </InsertParameters>
                <SelectParameters>
                    <asp:ControlParameter ControlID="WorkOrder" Name="WorkOrder" PropertyName="SelectedValue" Type="String" />
                </SelectParameters>
                <UpdateParameters>
                    <asp:Parameter Name="WorkOrder" Type="String" />
                    <asp:Parameter Name="PartNumber" Type="String" />
                    <asp:Parameter Name="Description" Type="String" />
                    <asp:Parameter Name="Quantity" Type="Decimal" />
                    <asp:Parameter Name="ID" Type="Int32" />
                </UpdateParameters>
            </asp:SqlDataSource>
            <br />
    
    
        <asp:SqlDataSource ID="SqlDataSource3" runat="server" ConnectionString="<%$ ConnectionStrings:fpimsConnectionString %>" ProviderName="<%$ ConnectionStrings:fpimsConnectionString.ProviderName %>" SelectCommand="SELECT DISTINCT [ID], [WorkOrder], [CustomerNumber], [Customer Name] AS Customer_Name, [OrderDate], [OrderShipDate], [OrderStatus], [OrderNotes], [OrderFiles] FROM [WorkOrders] WHERE ([WorkOrder] = ?)">
            <SelectParameters>
                <asp:ControlParameter ControlID="WorkOrder" Name="WorkOrder" PropertyName="SelectedValue" Type="String" />
            </SelectParameters>
            </asp:SqlDataSource>
    
    
      </section>
    </asp:Content>
    
    

    Partial Class WorkOrders_Default
        Inherits System.Web.UI.Page
    
        
    
        Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
    
            If Not IsPostBack Then
                WorkOrder.ClearSelection()
                WorkOrder.SelectedValue = Request.QueryString("WorkOrder")
    
    
            End If
    
        End Sub
    
    
        Protected Sub id_SelectedIndexChanged(sender As Object, e As EventArgs) Handles WorkOrder.SelectedIndexChanged
            WordOrderLabel.Text = WorkOrder.SelectedItem.Text.ToString()
        End Sub
    
    
        Protected Sub GridView1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles GridView1.SelectedIndexChanged
    
        End Sub
    End Class
    

    Sunday, January 22, 2017 1:51 AM

All replies

  • User-1716253493 posted

    How about updating quantity? seem like update command will update quatity only.

    Sunday, January 22, 2017 7:30 AM
  • User-1194755429 posted

    Quantity is the only thing that I want it to update.  It doesn't work. I change the quantity number and it reverts back to the original number.

    Sunday, January 22, 2017 11:01 PM
  • User-1716253493 posted

    Try modify this

               <UpdateParameters>
                    <asp:Parameter Name="WorkOrder" Type="String" />
                    <asp:Parameter Name="PartNumber" Type="String" />
                    <asp:Parameter Name="Description" Type="String" />
                    <asp:Parameter Name="Quantity" Type="Decimal" />
                    <asp:Parameter Name="ID" Type="Int32" />
                </UpdateParameters>
               <UpdateParameters>
                    <asp:Parameter Name="Quantity" Type="Decimal" />
                    <asp:Parameter Name="ID" Type="Int32" />
                </UpdateParameters>

    For sql server it will not effect, but i don't know the database you use

    Monday, January 23, 2017 1:00 AM