简体   繁体   中英

ASP.NET GridView doesn't save update in database

SELECT works, Edit button works, but after editing, when I press "Save" the Grid is back to before changes and there is no change in the database. SELECT is using 3 tables, UPDATE is supposed to update only the main one, but still nothing happens.

 <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>

I've read multiple examples and tutorials and everything should be fine. I've "manually" written the SELECT and UPDATE commands in SqlDataSource, maybe that's the problem?

Thanks in advance for any kind of advice.

maybe that's the problem?

After the last note, now I see that you have set ReadOnly="True" on the values that you try to update. Change that to false (or remove it), and they will be update.

See this below Example and try to change the code as follows:

<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>

You can mark a GridView column as non-editable by settings its ReadOnly property to True. Try to delete this property. Also, set the type of the updateparameters - that is DateTime, string and int32 in your case.

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

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

I used code-behind solution in the end, manually coding:

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

Which might actually give me more control over data.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM