具有访问数据源的Gridview不会更新或删除asp c#

时间:2013-03-27 21:19:36

标签: c# asp.net gridview


我看了一遍,研究了书籍,教程视频和大量文章,我无法解决这个问题。在visual studio 2010中插入gridview,我将数据源指定为访问数据库。在规范中,我包括插入,更新和删除记录的选项。 gridview出现在屏幕上,当我测试网页时,它会加载相关按钮以进行更新和插入等。 在测试页面时,我单击更新按钮,更改行,然后单击更新。该页面可以追溯到之前的情况,记录保持不变。我刷新页面以确定它没有更新,它肯定没有。 我完全遵循了教程,并且在使用此方法时无法获取要更新或删除的字段。我正在使用的数据库是一个访问者,一个mdb文件。

我确定我在这里遗漏了一些非常简单的东西,但我不确定是什么。如果有人能提供一些帮助,我将非常感激!谢谢

这是我的表单代码。

 <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" 
        DataKeyNames="Book Id" DataSourceID="SqlDataSource1">
        <Columns>
            <asp:CommandField ShowDeleteButton="True" ShowEditButton="True" 
                ShowSelectButton="True" />
            <asp:BoundField DataField="Book Id" HeaderText="Book Id" InsertVisible="False" 
                ReadOnly="True" SortExpression="Book Id" />
            <asp:BoundField DataField="ISBN Number" HeaderText="ISBN Number" 
                SortExpression="ISBN Number" />
            <asp:BoundField DataField="Title" HeaderText="Title" SortExpression="Title" />
            <asp:BoundField DataField="Author" HeaderText="Author" 
                SortExpression="Author" />
            <asp:BoundField DataField="Publisher" HeaderText="Publisher" 
                SortExpression="Publisher" />
            <asp:BoundField DataField="Date Published" HeaderText="Date Published" 
                SortExpression="Date Published" />
            <asp:CheckBoxField DataField="Availability" HeaderText="Availability" 
                SortExpression="Availability" />
            <asp:BoundField DataField="Price" HeaderText="Price" SortExpression="Price" />
        </Columns>
    </asp:GridView>
    <asp:SqlDataSource ID="SqlDataSource1" runat="server" 
        ConflictDetection="CompareAllValues" 
        ConnectionString="<%$ ConnectionStrings:booksellerConnectionString %>" 
        DeleteCommand="DELETE FROM [tblBooks] WHERE [Book Id] = ? AND (([ISBN Number] = ?) OR ([ISBN Number] IS NULL AND ? IS NULL)) AND (([Title] = ?) OR ([Title] IS NULL AND ? IS NULL)) AND (([Author] = ?) OR ([Author] IS NULL AND ? IS NULL)) AND (([Publisher] = ?) OR ([Publisher] IS NULL AND ? IS NULL)) AND (([Date Published] = ?) OR ([Date Published] IS NULL AND ? IS NULL)) AND [Availability] = ? AND (([Price] = ?) OR ([Price] IS NULL AND ? IS NULL))" 
        InsertCommand="INSERT INTO [tblBooks] ([Book Id], [ISBN Number], [Title], [Author], [Publisher], [Date Published], [Availability], [Price]) VALUES (?, ?, ?, ?, ?, ?, ?, ?)" 
        OldValuesParameterFormatString="original_{0}" 
        ProviderName="<%$ ConnectionStrings:booksellerConnectionString.ProviderName %>" 
        SelectCommand="SELECT * FROM [tblBooks]" 
        UpdateCommand="UPDATE [tblBooks] SET [ISBN Number] = ?, [Title] = ?, [Author] = ?, [Publisher] = ?, [Date Published] = ?, [Availability] = ?, [Price] = ? WHERE [Book Id] = ? AND (([ISBN Number] = ?) OR ([ISBN Number] IS NULL AND ? IS NULL)) AND (([Title] = ?) OR ([Title] IS NULL AND ? IS NULL)) AND (([Author] = ?) OR ([Author] IS NULL AND ? IS NULL)) AND (([Publisher] = ?) OR ([Publisher] IS NULL AND ? IS NULL)) AND (([Date Published] = ?) OR ([Date Published] IS NULL AND ? IS NULL)) AND [Availability] = ? AND (([Price] = ?) OR ([Price] IS NULL AND ? IS NULL))">
        <DeleteParameters>
            <asp:Parameter Name="original_Book_Id" Type="Int32" />
            <asp:Parameter Name="original_ISBN_Number" Type="String" />
            <asp:Parameter Name="original_ISBN_Number" Type="String" />
            <asp:Parameter Name="original_Title" Type="String" />
            <asp:Parameter Name="original_Title" Type="String" />
            <asp:Parameter Name="original_Author" Type="String" />
            <asp:Parameter Name="original_Author" Type="String" />
            <asp:Parameter Name="original_Publisher" Type="String" />
            <asp:Parameter Name="original_Publisher" Type="String" />
            <asp:Parameter Name="original_Date_Published" Type="String" />
            <asp:Parameter Name="original_Date_Published" Type="String" />
            <asp:Parameter Name="original_Availability" Type="Boolean" />
            <asp:Parameter Name="original_Price" Type="Decimal" />
            <asp:Parameter Name="original_Price" Type="Decimal" />
        </DeleteParameters>
        <InsertParameters>
            <asp:Parameter Name="Book_Id" Type="Int32" />
            <asp:Parameter Name="ISBN_Number" Type="String" />
            <asp:Parameter Name="Title" Type="String" />
            <asp:Parameter Name="Author" Type="String" />
            <asp:Parameter Name="Publisher" Type="String" />
            <asp:Parameter Name="Date_Published" Type="String" />
            <asp:Parameter Name="Availability" Type="Boolean" />
            <asp:Parameter Name="Price" Type="Decimal" />
        </InsertParameters>
        <UpdateParameters>
            <asp:Parameter Name="ISBN_Number" Type="String" />
            <asp:Parameter Name="Title" Type="String" />
            <asp:Parameter Name="Author" Type="String" />
            <asp:Parameter Name="Publisher" Type="String" />
            <asp:Parameter Name="Date_Published" Type="String" />
            <asp:Parameter Name="Availability" Type="Boolean" />
            <asp:Parameter Name="Price" Type="Decimal" />
            <asp:Parameter Name="original_Book_Id" Type="Int32" />
            <asp:Parameter Name="original_ISBN_Number" Type="String" />
            <asp:Parameter Name="original_ISBN_Number" Type="String" />
            <asp:Parameter Name="original_Title" Type="String" />
            <asp:Parameter Name="original_Title" Type="String" />
            <asp:Parameter Name="original_Author" Type="String" />
            <asp:Parameter Name="original_Author" Type="String" />
            <asp:Parameter Name="original_Publisher" Type="String" />
            <asp:Parameter Name="original_Publisher" Type="String" />
            <asp:Parameter Name="original_Date_Published" Type="String" />
            <asp:Parameter Name="original_Date_Published" Type="String" />
            <asp:Parameter Name="original_Availability" Type="Boolean" />
            <asp:Parameter Name="original_Price" Type="Decimal" />
            <asp:Parameter Name="original_Price" Type="Decimal" />
        </UpdateParameters>
    </asp:SqlDataSource>

下面的asp.cs文件;

namespace Second
{
    public partial class _Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }
    }
}

2 个答案:

答案 0 :(得分:0)

我不一定得到直接答案,但有些建议:

可能是因为UPDATE

的复杂性
UPDATE [tblBooks] SET [ISBN Number] = ?, [Title] = ?, [Author] = ?, [Publisher] = ?, [Date Published] = ?, [Availability] = ?, [Price] = ? WHERE [Book Id] = ? AND (([ISBN Number] = ?) OR ([ISBN Number] IS NULL AND ? IS NULL)) AND (([Title] = ?) OR ([Title] IS NULL AND ? IS NULL)) AND (([Author] = ?) OR ([Author] IS NULL AND ? IS NULL)) AND (([Publisher] = ?) OR ([Publisher] IS NULL AND ? IS NULL)) AND (([Date Published] = ?) OR ([Date Published] IS NULL AND ? IS NULL)) AND [Availability] = ? AND (([Price] = ?) OR ([Price] IS NULL AND ? IS NULL))

因为你有一个非常重要的WHERE条款。如果你只是把它缩回到:

UPDATE [tblBooks] SET [ISBN Number] = ?, [Title] = ?, [Author] = ?, [Publisher] = ?, [Date Published] = ?, [Availability] = ?, [Price] = ? WHERE [Book Id] = ?

相应地修剪你的<UpdateParameters>,这有效吗?

另外(对我而言已经有一段时间了)我注意到你的参数名称(例如Book_Id)与数据字段绑定(例如Book Id)不完全匹配,因此可能导致问题。我相信他们需要为binding目的匹配(或者他们这样做很好),但在这种情况下他们不会。

可能值得修改你的数据库,以便列名称是[Book_Id],[ISBN_Number]等...我认为在数据库专栏中有空格是非常罕见的(根据我的经验,这很少见......)但我没那么经验!)

答案 1 :(得分:0)

只是为了踢,尝试删除线 ConflictDetection="CompareAllValues"。更新现在会发生吗?