ASP.NET GridView不会在数据库中保存更新

时间:2012-03-23 09:42:08

标签: c# asp.net sql gridview

SELECT工作,编辑按钮工作,但编辑后,当我按“保存”时,网格返回到更改之前,数据库没有变化。 SELECT使用3个表,UPDATE应该只更新主表,但仍然没有任何反应。

 <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="idw" AllowSorting="True" DataSourceID="SqlDataSourceGrid">        
   <Columns>
     <asp:CommandField ShowEditButton="True" />
     <asp:BoundField DataField="idw" HeaderText="idw" InsertVisible="False" ReadOnly="True" SortExpression="idw" />
     <asp:BoundField DataField="name" HeaderText="name" ReadOnly="True" SortExpression="name" />
     <asp:BoundField DataField="surname" HeaderText="surname" ReadOnly="True" SortExpression="surname" />
     <asp:BoundField DataField="item_name" HeaderText="item_name" ReadOnly="True" SortExpression="item_name" />
     <asp:BoundField DataField="item_serial" HeaderText="item_serial" ReadOnly="True" SortExpression="item_serial" />
     <asp:BoundField DataField="date_taken" HeaderText="date_taken" SortExpression="date_taken" />
     <asp:BoundField DataField="notes" HeaderText="notes" SortExpression="notes" />
   </Columns>
 </asp:GridView>
 <asp:SqlDataSource ID="SqlDataSourceGrid" runat="server" ConnectionString="<%$ ConnectionStrings:WarehouseConnectionString %>" 
 SelectCommand="SELECT idw, name, surname, item_name, item_serial, date_taken, notes FROM Warehouse w JOIN Users u ON w.idu=u.idu JOIN Items i ON i.ids=i.ids" 
 UpdateCommand="UPDATE Warehouse SET date_taken=@date_taken, notes=@notes WHERE idw=@idw">
 <UpdateParameters>
   <asp:Parameter Name="date_taken" />
   <asp:Parameter Name="notes" />
   <asp:Parameter Name="idw" />
 </UpdateParameters>

我已经阅读了多个示例和教程,一切都应该没问题。我在SqlDataSource中“手动”编写了SELECT和UPDATE命令,也许这就是问题?

提前感谢任何建议。

5 个答案:

答案 0 :(得分:0)

maybe that's the problem?

在最后一个注释之后,现在我看到您已对尝试更新的值设置了ReadOnly="True"。将其更改为false(或删除它),它们将被更新。

答案 1 :(得分:0)

请参阅下面的示例并尝试按如下方式更改代码:

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" 
        DataKeyNames="EmployeeId" DataSourceID="SqlDataSource1">
        <Columns>
            <asp:CommandField ShowDeleteButton="True" ShowEditButton="True" 
                ShowSelectButton="True" />
            <asp:BoundField DataField="EmployeeId" HeaderText="EmployeeId" 
                InsertVisible="False" ReadOnly="True" SortExpression="EmployeeId" />
            <asp:BoundField DataField="Name" HeaderText="Name" SortExpression="Name" />
            <asp:BoundField DataField="Salary" HeaderText="Salary" 
                SortExpression="Salary" />
        </Columns>
    </asp:GridView>
    <asp:SqlDataSource ID="SqlDataSource1" runat="server" 
        ConnectionString="<%$ ConnectionStrings:ConnectionString %>" 
        DeleteCommand="DELETE FROM [Employee] WHERE [EmployeeId] = @EmployeeId" 
        InsertCommand="INSERT INTO [Employee] ([Name], [Salary]) VALUES (@Name, @Salary)" 
        SelectCommand="SELECT * FROM [Employee]" 
        UpdateCommand="UPDATE [Employee] SET [Name] = @Name, [Salary] = @Salary WHERE [EmployeeId] = @EmployeeId">
        <DeleteParameters>
            <asp:Parameter Name="EmployeeId" Type="Int64" />
        </DeleteParameters>
        <InsertParameters>
            <asp:Parameter Name="Name" Type="String" />
            <asp:Parameter Name="Salary" Type="String" />
        </InsertParameters>
        <UpdateParameters>
            <asp:Parameter Name="Name" Type="String" />
            <asp:Parameter Name="Salary" Type="String" />
            <asp:Parameter Name="EmployeeId" Type="Int64" />
        </UpdateParameters>
    </asp:SqlDataSource>

答案 2 :(得分:0)

通过将其ReadOnly属性设置为True,可以将GridView列标记为不可编辑。尝试删除此属性。另外,设置更新参数的类型 - 在您的情况下是DateTime,string和int32。

答案 3 :(得分:0)

您必须从后面的代码中手动编写:

SqlDataSourceGrid.UpdateCommand = "UPDATE Warehouse SET date_taken=date_taken1, notes=note1 WHERE idw=1";

答案 4 :(得分:0)

我最后使用了代码隐藏解决方案,手动编码:

protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
    string id = ...
    string notes = ...    
    UpdateDB(id, notes);    
    GridView1.EditIndex = -1;
    bind_grid();
}

这实际上可以让我更好地控制数据。