存储过程无法对绑定到gridview的行进行批量更新

时间:2016-08-03 20:32:26

标签: asp.net stored-procedures gridview

我正在使用网格视图同时更新网格中的所有行。此代码正在完美地处理另一页。现在的区别是我正在调用存储过程,因为我需要更新3个表。我没有收到任何错误,但表中没有任何更新。我相信这些参数没有得到正确的传递。

<asp:GridView ID="GridView1" runat="server" BackColor="White" BorderColor="#999999" BorderStyle="Solid" BorderWidth="1px" CellPadding="3" 
           DataSourceID="SqlDataSource1" ForeColor="Black" GridLines="Vertical" AutoGenerateColumns="False" DataKeyNames="CUST_ORDER_ID">
        <AlternatingRowStyle BackColor="#CCCCCC" />
        <Columns>

            <asp:BoundField DataField="CUST_ORDER_ID" HeaderText="ORDER_ID" SortExpression="CUST_ORDER_ID">
            <ItemStyle Width="50px"></ItemStyle>
            </asp:BoundField>
            <asp:BoundField DataField="NAME" HeaderText="NAME" ReadOnly="True" SortExpression="NAME">
            <ItemStyle Width="400px"></ItemStyle>
            </asp:BoundField>
            <asp:BoundField DataField="ORDERDATE" HeaderText="ORDER DATE" DataFormatString="{0:d}" ReadOnly="True" SortExpression="ORDERDATE">
            <ItemStyle Width="100px"></ItemStyle>
            </asp:BoundField>               
            <asp:BoundField DataField="DESIREDSHIPDATE" HeaderText="DESIRED SHIP DATE" SortExpression="DESIREDSHIPDATE" DataFormatString="{0:d}" ReadOnly="True" ItemStyle-Width="100" >
            <ItemStyle Width="100px"></ItemStyle>
            </asp:BoundField>
            <asp:BoundField DataField="PROMISEDATE" HeaderText="PROMISE DATE" SortExpression="PROMISEDATE" DataFormatString="{0:d}" ReadOnly="True" ItemStyle-Width="100">
            <ItemStyle Width="100px"></ItemStyle>
            </asp:BoundField>
            <asp:BoundField DataField="PROMISEDELDATE" HeaderText="DELIVERY DATE" SortExpression="PROMISEDELDATE" DataFormatString="{0:d}" ReadOnly="True" ItemStyle-Width="100" >
            <ItemStyle Width="100px"></ItemStyle>
            </asp:BoundField>
            <asp:TemplateField HeaderText="TRIP_ORDER" SortExpression="TRIP_ORDER" ItemStyle-Width="500">
                    <EditItemTemplate>
                        <asp:textbox ID="TextBox3" runat="server" Text='<%# Bind("TRIP_ORDER") %>'></asp:textbox>
                    </EditItemTemplate>
                    <ItemTemplate>
                        <asp:TextBox ID="TripOrderTextBox" runat="server" Text='<%# Bind("TRIP_ORDER") %>' Width="25px"></asp:TextBox>
                    </ItemTemplate>
                <ItemStyle Width="25px"></ItemStyle>
                </asp:TemplateField>

            <asp:BoundField DataField="ORDER_LINE_SUMMARY" HeaderText="ORDER LINE SUMMARY" SortExpression="ORDER_LINE_SUMMARY">
            <ItemStyle Width="50px"></ItemStyle>
            </asp:BoundField>

            <asp:TemplateField HeaderText="JOB_DEL_NOTES" SortExpression="JOB_DEL_NOTES" ItemStyle-Width="500">
                    <EditItemTemplate>
                        <asp:textbox ID="TextBox1" runat="server" Text='<%# Bind("JOB_DEL_NOTES") %>'></asp:textbox>
                    </EditItemTemplate>
                    <ItemTemplate>
                        <asp:TextBox ID="JobDelNotesTextBox" runat="server" Text='<%# Bind("JOB_DEL_NOTES") %>' Rows="3" TextMode="MultiLine" Width="500px" onkeypress="return this.value.length<=79" MaxLength="80"></asp:TextBox>
                    </ItemTemplate>
                <ItemStyle Width="500px"></ItemStyle>
                </asp:TemplateField>
            <asp:TemplateField HeaderText="ORDER_DEL_NOTES" SortExpression="ORDER_DEL_NOTES" ItemStyle-Width="500">
                    <EditItemTemplate>
                        <asp:textbox ID="TextBox2" runat="server" Text='<%# Bind("ORDER_DEL_NOTES") %>'></asp:textbox>
                    </EditItemTemplate>
                    <ItemTemplate>
                        <asp:TextBox ID="OrderDelNotesTextBox" runat="server" Text='<%# Bind("ORDER_DEL_NOTES") %>' Rows="3" TextMode="MultiLine" Width="500px" onkeypress="return this.value.length<=79" MaxLength="80"></asp:TextBox>
                    </ItemTemplate>
                <ItemStyle Width="500px"></ItemStyle>
                </asp:TemplateField>

        </Columns>
        <FooterStyle BackColor="#CCCCCC" />
        <HeaderStyle BackColor="Black" Font-Bold="True" ForeColor="White" />
        <PagerStyle BackColor="#999999" ForeColor="Black" HorizontalAlign="Center" />
        <SelectedRowStyle BackColor="#000099" Font-Bold="True" ForeColor="White" />
        <SortedAscendingCellStyle BackColor="#F1F1F1" />
        <SortedAscendingHeaderStyle BackColor="#808080" />
        <SortedDescendingCellStyle BackColor="#CAC9C9" />
        <SortedDescendingHeaderStyle BackColor="#383838" />
    </asp:GridView>


        <asp:SqlDataSource ID="SqlDataSource1" runat="server" CancelSelectOnNullParameter="false" ConnectionString="<%$ ConnectionStrings:ConnectionStringTest %>" 
            ProviderName="<%$ ConnectionStrings:ConnectionStringTest.ProviderName %>" 
            SelectCommandType="StoredProcedure" SelectCommand="CM_GET_SCHEDULED_ORDERS" 
            UpdateCommandType="StoredProcedure" UpdateCommand="CM_SP_UPDATE_TRIP">

            <SelectParameters>
                <asp:QueryStringParameter Name="DELIVERY_DATE" DbType="DateTime" Direction="Input" QueryStringField="PROMISE_DEL_DATE" DefaultValue="" />
                <asp:Parameter Name="UPDATE_TRIP" DefaultValue="YES" Type="String" />
            </SelectParameters>
            <UpdateParameters>
                <asp:Parameter Name="CUST_ORDER_ID" DefaultValue="" Type="String" Direction="Input" />
                <asp:Parameter Name="ORDER_LINE_SUMMARY" Type="String" Direction="Input" />
                <asp:Parameter Name="TRIP_ORDER" DefaultValue="" Type="String" Direction="Input"/>
                <asp:Parameter Name="PROMISEDELDATE" Defaultvalue="" Type="datetime" Direction="Input"/>
                <asp:Parameter Name="JOB_DEL_NOTES" Type="String" Direction="Input"/>
                <asp:Parameter Name="ORDER_DEL_NOTES" Type="String" Direction="Input"/>
            </UpdateParameters>

        </asp:SqlDataSource>

我的代码背后:

Imports Microsoft.VisualBasic
Imports System.Data
Imports System.Configuration
Imports System.Data.SqlClient

Public Class UpdateTrip
Inherits System.Web.UI.Page

Private tableCopied As Boolean = False
Private originalDataTable As System.Data.DataTable

Protected Sub Page_load(sender As Object, e As EventArgs) Handles Me.Load
    If Not IsPostBack Then
        SetDateTextBox.Text = Request.QueryString("PROMISE_DEL_DATE")
    End If

End Sub
Protected Sub btnRedirect_Click(sender As Object, e As EventArgs)
    Response.Redirect("~/UpdateTrip.aspx?PROMISE_DEL_DATE=" + SetDateTextBox.Text)
End Sub

Protected Sub GridView1_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GridView1.RowDataBound
    If e.Row.RowType = DataControlRowType.DataRow Then
        If Not tableCopied Then
            originalDataTable = CType(e.Row.DataItem, System.Data.DataRowView).Row.Table.Copy()
            ViewState("originalValuesDataTable") = originalDataTable
            tableCopied = True
        End If
    End If
End Sub

Protected Sub UpdateButton_Click(ByVal sender As Object, ByVal e As EventArgs) Handles UpdateTrip.Click
    originalDataTable = CType(ViewState("originalValuesDataTable"), System.Data.DataTable)

    For Each r As GridViewRow In GridView1.Rows
        'If IsRowModified(r) Then GridView1.UpdateRow(r.RowIndex, False)
        GridView1.UpdateRow(r.RowIndex, False)
    Next

    ' Rebind the Grid to repopulate the original values table.
    'tableCopied = False
    'GridView1.DataBind()
    Response.Redirect("~/UpdateTrip.aspx?PROMISE_DEL_DATE=" + SetDateTextBox.Text)

End Sub

'Protected Function IsRowModified(ByVal r As GridViewRow) As Boolean
'    Dim currentID As String
'    Dim currentTripOrder As String
'    Dim currentJobDelNotes As String
'    Dim currentOrderDelNotes As String

'    currentID = Convert.ToInt32(GridView1.DataKeys(r.RowIndex).Value)

'    currentTripOrder = CType(r.FindControl("TripOrderTextBox"), TextBox).Text
'    currentJobDelNotes = CType(r.FindControl("JobDelNotesTextBox"), TextBox).Text
'    currentOrderDelNotes = CType(r.FindControl("OrderDelNotesTextBox"), TextBox).Text

'    Dim row As System.Data.DataRow =
'        originalDataTable.Select(String.Format("cust_order_id = {0}", currentID))(0)

'    If Not currentTripOrder.Equals(row("Trip_order").ToString()) Then Return True
'    If Not currentJobDelNotes.Equals(row("Job_Del_Notes").ToString()) Then Return True
'    If Not currentOrderDelNotes.Equals(row("Order_Del_Notes").ToString()) Then Return True
'    Return False
'End Function

Protected Sub Calendar1_SelectionChanged(sender As Object, e As EventArgs) Handles Calendar1.SelectionChanged
    SetDateTextBox_PopupControlExtender.Commit(Calendar1.SelectedDate)
End Sub

结束班

存储过程

SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO



-- =============================================
-- Author:      <Michael Mueller>
-- Create date: <8/3/2016>
-- Description: <update multiple tables for the trip order. customer_order. cust_order_line, cust_address>
-- =============================================
ALTER PROCEDURE [dbo].[CM_SP_UPDATE_TRIP] 
    -- Add the parameters for the stored procedure here
    @CUST_ORDER_ID varchar(15),
    @ORDER_LINE_SUMMARY VARCHAR(MAX),
    @TRIP_ORDER VARCHAR(3),
    @PROMISE_DEL_DATE DATETIME,
    @JOB_DEL_NOTES varchar(80),
    @ORDER_DEL_NOTES VARCHAR(80)

AS
BEGIN
    -- SET NOCOUNT ON added to prevent extra result sets from
    -- interfering with SELECT statements.
    SET NOCOUNT ON;

    -- update cust_adress with job delivery notes

    UPDATE CUST_ADDRESS 
    SET USER_4 = @JOB_DEL_NOTES
    FROM         dbo.CUSTOMER_ORDER INNER JOIN
                      dbo.CUST_ADDRESS ON dbo.CUSTOMER_ORDER.CUSTOMER_ID = dbo.CUST_ADDRESS.CUSTOMER_ID AND 
                      dbo.CUSTOMER_ORDER.SHIPTO_ID = dbo.CUST_ADDRESS.SHIPTO_ID
    WHERE     (dbo.CUSTOMER_ORDER.ID = @CUST_ORDER_ID)

    -- update customer_order with order delivery notes

    UPDATE CUSTOMER_ORDER
    SET USER_5 = @ORDER_DEL_NOTES 
    WHERE ID = @CUST_ORDER_ID 

    -- update cust_order_line with trip_order information

    IF @ORDER_LINE_SUMMARY = 'ALL LINES'
        BEGIN
        UPDATE CUST_ORDER_LINE
        SET USER_4 = @TRIP_ORDER 
        WHERE CUST_ORDER_ID = @CUST_ORDER_ID 
        END
    ELSE -- ONLY SPECIFIC LINES OF ORDER GET UPDATED
        BEGIN
        UPDATE CUST_ORDER_LINE
        SET USER_4 = @TRIP_ORDER
        WHERE CUST_ORDER_ID = @CUST_ORDER_ID AND PROMISE_DEL_DATE = @PROMISE_DEL_DATE
        END

1 个答案:

答案 0 :(得分:0)

我找出了主要问题。使用Sql Server Profile我可以看到只有绑定字段被传递给存储过程。其他值为null。我为剩下的参数创建了ItemTemplates。