Web 应用程序中的更新和删除操作 ASP.NET 问题

Issues with Update and Delete Operations in ASP.NET Web Application

提问人:meraki_1 提问时间:5/18/2023 最后编辑:meraki_1 更新时间:5/19/2023 访问量:54

问:

问题描述:

我在 ASP.NET Web 应用程序中遇到更新和删除操作的问题。我有一个 GridView 控件,用于显示 SQL Server 数据库中的数据。使用 SqlDataSource 控件提取数据,并且我定义了更新和删除操作的存储过程。但是,当我尝试执行这些操作时,我遇到以下错误:

“/”应用程序中的服务器错误。 过程或函数 UpdateNarudzbaTable/DeleteNarudzba 指定的参数过多。

我想了解为什么会抛出此错误以及如何解决它。此外,我将不胜感激地了解为什么更新和删除操作无法按预期工作。

HTML 代码 (Narudzbe.aspx):

    <%@ Page Language="vb" AutoEventWireup="false" CodeBehind="Narudzbe.aspx.vb" Inherits="Mišković_OMIS_projekt.Narudzbe" %>
    
    <!DOCTYPE html>
    
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title>Narudžbe</title>
        <style>
            <!-- Styles -->
        </style>
    </head>
    <body>
<div class="search">
            <input type="text" id="txtSearch" placeholder="Pretraži po broju narudžbe"/>
            <input type="button" id="btnSearch" value="Pretraži" onclick="btnSearch_Click"/>
            <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="ID narudžbe" DataSourceID="SqlDataSource1" Height="282px" Width="1124px" style="margin-top: 20px" AllowPaging="True" AllowSorting="True">
                <Columns>
                    <asp:BoundField DataField="ID narudžbe" HeaderText="Broj narudžbe" ReadOnly="True" SortExpression="ID narudžbe" />
                    <asp:BoundField DataField="Ime" HeaderText="Kupac" ReadOnly="True" SortExpression="Ime" />
                    <asp:BoundField DataField="Datum primitka" DataFormatString="{0:d}" HeaderText="Datum primitka" ReadOnly="True" SortExpression="Datum primitka" />
                    <asp:BoundField DataField="Datum isporuke" DataFormatString="{0:d}" HeaderText="Datum isporuke" SortExpression="Datum isporuke" />
                    <asp:BoundField DataField="Naziv statusa" HeaderText="Status" SortExpression="Naziv statusa" />
                    <asp:CommandField ShowDeleteButton="True" DeleteText="Obriši"  ShowEditButton="True" CancelText="Poništi" EditText="Uredi" UpdateText="Spremi"/>
                </Columns>
            </asp:GridView>
                <asp:HyperLink ID="HyperLink1" runat="server" BorderColor="#CCCCCC" BorderStyle="Solid" BorderWidth="20px" CssClass="glavni izbornik" NavigateUrl="~/LandingPage.aspx">Povratak na glavni izbornik</asp:HyperLink>
                <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:MyDatabaseConnection %>" SelectCommand="PodaciZaTablicuNarudzbe" SelectCommandType="StoredProcedure"
                    UpdateCommand="UpdateNarudzbaTable" UpdateCommandType="StoredProcedure"
                    DeleteCommand="DeleteNarudzba" DeleteCommandType="StoredProcedure">
                    <UpdateParameters>
                        <asp:Parameter Name="BrojNarudzbe" Type="Int32" />
                        <asp:Parameter Name="DatumPrimitka" Type="DateTime" />
                        <asp:Parameter Name="DatumIsporuke" Type="DateTime" />
                        <asp:Parameter Name="Status" Type="String" />
                    </UpdateParameters>
                    <DeleteParameters>
                        <asp:Parameter Name="BrojNarudzbe" Type="Int32" />
                    </DeleteParameters>
                </asp:SqlDataSource>
        </div>
        <!-- Form and other elements -->
    </body>
    </html>

存储过程 (DeleteNarudzba):

ALTER PROCEDURE [dbo].[DeleteNarudzba]
    @BrojNarudzbe INT
AS
BEGIN
    -- Check if the order exists
    IF EXISTS (SELECT 1 FROM Narudžba WHERE [ID narudžbe] = @BrojNarudzbe)
    BEGIN
        -- Delete the order
        DELETE FROM Narudžba WHERE [ID narudžbe] = @BrojNarudzbe
    END
    ELSE
    BEGIN
        -- Order does not exist, raise an error
        RAISERROR('Narudžba sa Broj narudžbe = %d ne postoji.', 16, 1, @BrojNarudzbe)
    END
END

存储过程 (UpdateNarudzbaTable):

ALTER PROCEDURE [dbo].[UpdateNarudzbaTable]
    @BrojNarudzbe INT,
    @DatumPrimitka DATE,
    @DatumIsporuke DATE,
    @Status VARCHAR(50)
AS
BEGIN
    -- Check if the status exists in the 'Status' table
    IF EXISTS (SELECT 1 FROM [Status] WHERE [Naziv statusa] = @Status)
    BEGIN
        -- Get the ID of the status from the 'Status' table
        DECLARE @StatusID INT
        SELECT @StatusID = [ID statusa] FROM [Status] WHERE [Naziv statusa] = @Status

        -- Update the 'Narudžba' table
        UPDATE Narudžba
        SET [Datum isporuke] = @DatumIsporuke,
            [ID statusa] = @StatusID
        WHERE [ID narudžbe] = @BrojNarudzbe
    END
    ELSE
    BEGIN
        -- Return an error if the status doesn't exist
        THROW 50000, 'Status ne postoji', 1
    END

    -- Return an error if 'Datum isporuke' is not greater than or equal to 'Datum primitka'
    IF @DatumIsporuke < @DatumPrimitka
    BEGIN
        THROW 50001, 'Datum isporuke mora biti veći ili jednak datumu primitka', 1
    END
END

感谢您的帮助!

编辑:尝试将 DefaultValue 属性添加到我的代码隐藏中的参数:

Protected Sub BindGridView()
        ' Bind the GridView to the SqlDataSource control
        GridView1.DataSource = SqlDataSource1
        GridView1.DataBind()
    End Sub

    Protected Sub GridView1_RowEditing(sender As Object, e As GridViewEditEventArgs)
        GridView1.EditIndex = e.NewEditIndex
        BindGridView() ' Rebind the GridView to show the editable row
    End Sub

    Protected Sub GridView1_RowUpdating(sender As Object, e As GridViewUpdateEventArgs)
        ' Get the updated values from the GridView's edit row
        Dim updatedValues As Dictionary(Of String, Object) = New Dictionary(Of String, Object)()

        For i As Integer = 0 To Math.Min(e.NewValues.Count - 1, e.Keys.Count - 1)
            Dim field As String = e.Keys(i).ToString()
            Dim value As Object = e.NewValues(i)
            updatedValues.Add(field, value)
        Next

        ' Set the DefaultValue for parameters
        SqlDataSource1.UpdateParameters("@BrojNarudzbe").DefaultValue = updatedValues("BrojNarudzbe").ToString()
        SqlDataSource1.UpdateParameters("@DatumPrimitka").DefaultValue = DateTime.Parse(updatedValues("DatumPrimitka").ToString())
        SqlDataSource1.UpdateParameters("@DatumIsporuke").DefaultValue = DateTime.Parse(updatedValues("DatumIsporuke").ToString())
        SqlDataSource1.UpdateParameters("@Status").DefaultValue = updatedValues("Status").ToString()

        ' Continue with the update operation
        GridView1.UpdateRow(e.RowIndex, False)
        GridView1.EditIndex = -1
    End Sub


    Protected Sub GridView1_RowCancelingEdit(sender As Object, e As GridViewCancelEditEventArgs)
        GridView1.EditIndex = -1
        BindGridView() ' Rebind the GridView to cancel the edit mode
    End Sub

    Protected Sub GridView1_RowDeleting(sender As Object, e As GridViewDeleteEventArgs)
        ' Get the values from the GridView's delete row
        Dim deletedValues As Dictionary(Of String, Object) = New Dictionary(Of String, Object)()
        For i As Integer = 0 To e.Keys.Count - 1
            Dim field As String = e.Keys(i).ToString()
            Dim value As Object = e.Keys(i)
            deletedValues.Add(field, value)
        Next

        ' Set the DefaultValue for parameters
        SqlDataSource1.DeleteParameters("BrojNarudzbe").DefaultValue = deletedValues("BrojNarudzbe").ToString()
        SqlDataSource1.DeleteParameters("DatumPrimitka").DefaultValue = deletedValues("DatumPrimitka").ToString()
        SqlDataSource1.DeleteParameters("DatumIsporuke").DefaultValue = deletedValues("DatumIsporuke").ToString()
        SqlDataSource1.DeleteParameters("Status").DefaultValue = deletedValues("Status").ToString()

        ' Continue with the delete operation
        GridView1.DeleteRow(e.RowIndex)
        BindGridView() ' Rebind the GridView to show the updated data after deletion
    End Sub
HTML asp.net vb.net visual-studio-2019

评论

0赞 Michael Foster 5/19/2023
参数看起来正确,但我没有看到 DefaultValue 属性。数据应该如何到达存储过程?到目前为止,我还没有对我的数据使用 GridView,而只使用 RadGrid,我在代码隐藏中编辑了 DefaultValue。
0赞 meraki_1 5/19/2023
@MichaelFoster我尝试添加DefaultValue属性(代码已添加到线程中),但我不断收到此错误:System.Collections.Generic.KeyNotFoundException:“字典中不存在给定的键。
0赞 Michael Foster 5/19/2023
例如,这意味着您的收藏中没有。根据 learn.microsoft.com/en-us/dotnet/api/,这些应该来自您的 GridView......BrojNarudzbeKeysDataKeyNames

答: 暂无答案