473,327 Members | 2,103 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,327 software developers and data experts.

Detailsview updates only once when tied to gridview

2
Here's a problem that has me scratching my head. I have a gridview and a detailsview tied together, so that the user selects an item in the gridview, and then edits it in the detailsview. I run a databind on the gridview in the itemUpdated event handler for the detailsview.

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
  1.  
  2. <%@ Page Language="VB" AutoEventWireup="false" CodeFile="updateProblem.aspx.vb" Inherits="updateProblem" %>
  3.  
  4. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  5.  
  6. <html xmlns="http://www.w3.org/1999/xhtml">
  7. <head runat="server">
  8.     <title></title>
  9. </head>
  10.  
  11. <body>
  12.     <form id="form1" runat="server">
  13.     <div>
  14.  
  15.     <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
  16.  
  17.         <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" 
  18.             DataKeyNames="ID" DataSourceID="AccessDataSource1" 
  19.             EmptyDataText="There are no data records to display.">
  20.             <Columns>
  21.                 <asp:CommandField ButtonType="Button" ShowSelectButton="True" />
  22.                 <asp:BoundField DataField="ID" HeaderText="ID" ReadOnly="True" 
  23.                     SortExpression="ID" />
  24.                 <asp:BoundField DataField="fullName" HeaderText="fullName" 
  25.                     SortExpression="fullName" />
  26.                 <asp:BoundField DataField="age" HeaderText="age" SortExpression="age" />
  27.             </Columns>
  28.         </asp:GridView>
  29.         <asp:DetailsView ID="DetailsView1" runat="server" AutoGenerateRows="False" 
  30.             DataKeyNames="ID" DataSourceID="AccessDataSource2" DefaultMode="Edit" 
  31.             Height="50px" Width="125px">
  32.             <Fields>
  33.                 <asp:BoundField DataField="ID" HeaderText="ID" InsertVisible="False" 
  34.                     ReadOnly="True" SortExpression="ID" />
  35.                 <asp:BoundField DataField="fullName" HeaderText="fullName" 
  36.                     SortExpression="fullName" />
  37.                 <asp:BoundField DataField="age" HeaderText="age" SortExpression="age" />
  38.                 <asp:CommandField ShowEditButton="True" />
  39.             </Fields>
  40.         </asp:DetailsView>
  41.         <asp:AccessDataSource ID="AccessDataSource2" runat="server" 
  42.             DataFile="~/App_Data/Database1.accdb" 
  43.             DeleteCommand="DELETE FROM [Table1] WHERE [ID] = ?" 
  44.             InsertCommand="INSERT INTO [Table1] ([ID], [fullName], [age]) VALUES (?, ?, ?)" 
  45.             SelectCommand="SELECT * FROM [Table1] WHERE ([ID] = ?)" 
  46.             UpdateCommand="UPDATE [Table1] SET [fullName] = ?, [age] = ? WHERE [ID] = ?">
  47.             <DeleteParameters>
  48.                 <asp:Parameter Name="ID" Type="Int32" />
  49.             </DeleteParameters>
  50.             <InsertParameters>
  51.                 <asp:Parameter Name="ID" Type="Int32" />
  52.                 <asp:Parameter Name="fullName" Type="String" />
  53.                 <asp:Parameter Name="age" Type="Int32" />
  54.             </InsertParameters>
  55.             <SelectParameters>
  56.                 <asp:ControlParameter ControlID="GridView1" Name="ID" 
  57.                     PropertyName="SelectedValue" Type="Int32" />
  58.             </SelectParameters>
  59.             <UpdateParameters>
  60.                 <asp:Parameter Name="fullName" Type="String" />
  61.                 <asp:Parameter Name="age" Type="Int32" />
  62.                 <asp:Parameter Name="ID" Type="Int32" />
  63.             </UpdateParameters>
  64.         </asp:AccessDataSource>
  65.         <asp:AccessDataSource ID="AccessDataSource1" runat="server" 
  66.             DataFile="App_Data\Database1.accdb" 
  67.             DeleteCommand="DELETE FROM `Table1` WHERE `ID` = ?" 
  68.             InsertCommand="INSERT INTO `Table1` (`ID`, `fullName`, `age`) VALUES (?, ?, ?)" 
  69.             SelectCommand="SELECT `ID`, `fullName`, `age` FROM `Table1`" 
  70.             UpdateCommand="UPDATE `Table1` SET `fullName` = ?, `age` = ? WHERE `ID` = ?">
  71.             <DeleteParameters>
  72.                 <asp:Parameter Name="ID" Type="Int32" />
  73.             </DeleteParameters>
  74.             <InsertParameters>
  75.                 <asp:Parameter Name="ID" Type="Int32" />
  76.                 <asp:Parameter Name="fullName" Type="String" />
  77.                 <asp:Parameter Name="age" Type="Int32" />
  78.             </InsertParameters>
  79.             <UpdateParameters>
  80.                 <asp:Parameter Name="fullName" Type="String" />
  81.                 <asp:Parameter Name="age" Type="Int32" />
  82.                 <asp:Parameter Name="ID" Type="Int32" />
  83.             </UpdateParameters>
  84.         </asp:AccessDataSource>
  85.     </div>
  86.     </form>
  87. </body>
  88. </html>
  89.  
  90. Partial Class updateProblem
  91.     Inherits System.Web.UI.Page
  92.  
  93.     Protected Sub Page_Load(sender As Object, e As System.EventArgs) Handles Me.Load
  94.         If Not IsPostBack Then
  95.             DetailsView1.Visible = False
  96.         End If
  97.     End Sub
  98.  
  99.     Protected Sub DetailsView1_ItemUpdated(sender As Object, e As System.Web.UI.WebControls.DetailsViewUpdatedEventArgs) Handles DetailsView1.ItemUpdated
  100.         If e.Exception IsNot Nothing Then
  101.             Label1.Text = e.Exception.Message.ToString
  102.         Else
  103.             Label1.Text = "no problems w db update"
  104.             Try
  105.                 GridView1.DataBind()
  106.                 GridView1.Visible = True
  107.                 DetailsView1.Visible = False
  108.  
  109.             Catch ex As Exception
  110.                 Label1.Text = ex.Message.ToString
  111.             End Try
  112.  
  113.         End If
  114.     End Sub
  115.  
  116.     Protected Sub DetailsView1_ModeChanged(sender As Object, e As System.EventArgs) Handles DetailsView1.ModeChanged
  117.         GridView1.Visible = True
  118.         DetailsView1.Visible = False
  119.     End Sub
  120.  
  121.     Protected Sub GridView1_SelectedIndexChanged(sender As Object, e As System.EventArgs) Handles GridView1.SelectedIndexChanged
  122.         GridView1.Visible = False
  123.         DetailsView1.Visible = True
  124.     End Sub
  125.  
  126. End Class
  127.  
  128.  
  129.  
Attached Files
File Type: zip updateProblem.zip (13.8 KB, 94 views)
Apr 28 '12 #1
1 2565
tsako
2
I figured it out. If you select the same record twice in a row from a gridview, then the detailsview doesn't make a new request, so it's working with stale data. The solution is to add a detailsview.databind() to the gridview selectedItemIndexChanged event handler.
May 2 '12 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: Thomas Johansen | last post by:
Hi I have an managed C++ dll, thats uses an API to handling some HW. This DLL is then called from an C# dll. But I need only once instance of the managed C++ dll, no matter how many user...
2
by: Randall Parker | last post by:
Is there an event that will run once when either we tell IIS that we've changed the source code for the site or when the server reboots? What I'm trying to do: I want to have a file that tells my...
9
by: J055 | last post by:
Hi I'm trying to get an instance of UserLists to persist after it's been used by the GridView. I understand from the documentation that The OnObjectCreated event allows access to the instance. I...
2
by: VBLearner | last post by:
How to close the parent and all child forms together at once when click on parent window's close button X?
2
by: Ivan Jericevich | last post by:
I have a form with Textboxes and 2 datepickers, on the update everything updates to the database just fine bar the 2 Datepickers, they updated to database once the first time only and won't change...
0
by: =?Utf-8?B?Vmlua2k=?= | last post by:
Hello Everyone, I have a gridview. I calculated some values in the gridview for the footer of the gridview so basically I am adding all the values that are displayed in the gridview for each...
2
by: Joey | last post by:
This is causing us a lot of pain right now. Does anyone know why this happens and, more importantly, how to fix it? TIA JP
3
by: Nick Bennett | last post by:
I've got a page with a DetailsView. It uses a SqlDataSource which itself uses stored procedures for Select and Update. I don't want the user to see some of the columns, but if I don't bind them...
1
by: Cirene | last post by:
I had a non-ajaxed page with a gridview. I wanted to ajax it. I added the scriptmanager at the top. I added an updatepanel below. I dragged the gridview into it. In my design view I got...
8
by: Nick | last post by:
Hi there, I have a GridView in an UpdatePanel, each time the UpdatePanels Load event fires I set the DataSource and call DataBind of the grid view. This works great once, I add an item to the...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.