Everything works fine ... until you try and edit the same record twice in sequence. At this point the data isn't written to the database. For example, I can edit record 1 and then record 2 and everything functions correctly. I can also edit record 1, then record 2, then come back and edit record 1 and everything works. But if I edit record 1 and then edit record 1 again the update doesn't happen.
As you can see I've added error checks for the database update, and nothing shows up. But I've also opened the database table and verified that the 2nd update never happens.
It doesn't happen if I just use a detailsview for editing. It only happens when the gridview and detailsview are tied together.
I've "fixed" this by forcing the page to reload using server.transfer() if the same record is edited twice in a row. But this isn't a very good solution and I was hoping someone could tell me what's causing this and provide a more graceful solution.
Here's the code. I've also zipped up a sample web site.
Thanks for any help you can provide!
Expand|Select|Wrap|Line Numbers
- <%@ Page Language="VB" AutoEventWireup="false" CodeFile="updateProblem.aspx.vb" Inherits="updateProblem" %>
- <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
- <html xmlns="http://www.w3.org/1999/xhtml">
- <head runat="server">
- <title></title>
- </head>
- <body>
- <form id="form1" runat="server">
- <div>
- <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
- <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
- DataKeyNames="ID" DataSourceID="AccessDataSource1"
- EmptyDataText="There are no data records to display.">
- <Columns>
- <asp:CommandField ButtonType="Button" ShowSelectButton="True" />
- <asp:BoundField DataField="ID" HeaderText="ID" ReadOnly="True"
- SortExpression="ID" />
- <asp:BoundField DataField="fullName" HeaderText="fullName"
- SortExpression="fullName" />
- <asp:BoundField DataField="age" HeaderText="age" SortExpression="age" />
- </Columns>
- </asp:GridView>
- <asp:DetailsView ID="DetailsView1" runat="server" AutoGenerateRows="False"
- DataKeyNames="ID" DataSourceID="AccessDataSource2" DefaultMode="Edit"
- Height="50px" Width="125px">
- <Fields>
- <asp:BoundField DataField="ID" HeaderText="ID" InsertVisible="False"
- ReadOnly="True" SortExpression="ID" />
- <asp:BoundField DataField="fullName" HeaderText="fullName"
- SortExpression="fullName" />
- <asp:BoundField DataField="age" HeaderText="age" SortExpression="age" />
- <asp:CommandField ShowEditButton="True" />
- </Fields>
- </asp:DetailsView>
- <asp:AccessDataSource ID="AccessDataSource2" runat="server"
- DataFile="~/App_Data/Database1.accdb"
- DeleteCommand="DELETE FROM [Table1] WHERE [ID] = ?"
- InsertCommand="INSERT INTO [Table1] ([ID], [fullName], [age]) VALUES (?, ?, ?)"
- SelectCommand="SELECT * FROM [Table1] WHERE ([ID] = ?)"
- UpdateCommand="UPDATE [Table1] SET [fullName] = ?, [age] = ? WHERE [ID] = ?">
- <DeleteParameters>
- <asp:Parameter Name="ID" Type="Int32" />
- </DeleteParameters>
- <InsertParameters>
- <asp:Parameter Name="ID" Type="Int32" />
- <asp:Parameter Name="fullName" Type="String" />
- <asp:Parameter Name="age" Type="Int32" />
- </InsertParameters>
- <SelectParameters>
- <asp:ControlParameter ControlID="GridView1" Name="ID"
- PropertyName="SelectedValue" Type="Int32" />
- </SelectParameters>
- <UpdateParameters>
- <asp:Parameter Name="fullName" Type="String" />
- <asp:Parameter Name="age" Type="Int32" />
- <asp:Parameter Name="ID" Type="Int32" />
- </UpdateParameters>
- </asp:AccessDataSource>
- <asp:AccessDataSource ID="AccessDataSource1" runat="server"
- DataFile="App_Data\Database1.accdb"
- DeleteCommand="DELETE FROM `Table1` WHERE `ID` = ?"
- InsertCommand="INSERT INTO `Table1` (`ID`, `fullName`, `age`) VALUES (?, ?, ?)"
- SelectCommand="SELECT `ID`, `fullName`, `age` FROM `Table1`"
- UpdateCommand="UPDATE `Table1` SET `fullName` = ?, `age` = ? WHERE `ID` = ?">
- <DeleteParameters>
- <asp:Parameter Name="ID" Type="Int32" />
- </DeleteParameters>
- <InsertParameters>
- <asp:Parameter Name="ID" Type="Int32" />
- <asp:Parameter Name="fullName" Type="String" />
- <asp:Parameter Name="age" Type="Int32" />
- </InsertParameters>
- <UpdateParameters>
- <asp:Parameter Name="fullName" Type="String" />
- <asp:Parameter Name="age" Type="Int32" />
- <asp:Parameter Name="ID" Type="Int32" />
- </UpdateParameters>
- </asp:AccessDataSource>
- </div>
- </form>
- </body>
- </html>
- Partial Class updateProblem
- Inherits System.Web.UI.Page
- Protected Sub Page_Load(sender As Object, e As System.EventArgs) Handles Me.Load
- If Not IsPostBack Then
- DetailsView1.Visible = False
- End If
- End Sub
- Protected Sub DetailsView1_ItemUpdated(sender As Object, e As System.Web.UI.WebControls.DetailsViewUpdatedEventArgs) Handles DetailsView1.ItemUpdated
- If e.Exception IsNot Nothing Then
- Label1.Text = e.Exception.Message.ToString
- Else
- Label1.Text = "no problems w db update"
- Try
- GridView1.DataBind()
- GridView1.Visible = True
- DetailsView1.Visible = False
- Catch ex As Exception
- Label1.Text = ex.Message.ToString
- End Try
- End If
- End Sub
- Protected Sub DetailsView1_ModeChanged(sender As Object, e As System.EventArgs) Handles DetailsView1.ModeChanged
- GridView1.Visible = True
- DetailsView1.Visible = False
- End Sub
- Protected Sub GridView1_SelectedIndexChanged(sender As Object, e As System.EventArgs) Handles GridView1.SelectedIndexChanged
- GridView1.Visible = False
- DetailsView1.Visible = True
- End Sub
- End Class