Last post Feb 21, 2017 07:21 AM by Chris Zhao
Feb 15, 2017 09:18 PM|mavila|LINK
I have a database that is bound to an SQLDataSource control and I'm using it to update a single field. I don't get any errors, but it doesn't update the database. I've tried many things and cannot figure this out. I've used the RowUpdating event to confirm
that the e.OldValue and e.NewValue are correct. Not sure where I'm going wrong. Any help is appreciated -thanks!
Here's the code:
<asp:GridView ID="gvChrono" runat="server" AllowPaging="True" AllowSorting="True" AutoGenerateColumns="False" BackColor="White" BorderColor="#999999" BorderStyle="None" BorderWidth="1px" CellPadding="3" DataKeyName="Op, Chrono" DataSourceID="sdsGvChrono" GridLines="Vertical">
<AlternatingRowStyle BackColor="#DCDCDC" />
<asp:CommandField AccessibleHeaderText="Edit:" ButtonType="Button" ShowEditButton="True" />
<asp:BoundField DataField="Op" HeaderText="Operation Name:" SortExpression="Op" ReadOnly="True" />
<asp:BoundField DataField="Chrono" HeaderText="Chronology Value:" SortExpression="Chrono" />
<FooterStyle BackColor="#CCCCCC" ForeColor="Black" />
<HeaderStyle BackColor="#000084" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#999999" ForeColor="Black" HorizontalAlign="Center" />
<RowStyle BackColor="#EEEEEE" ForeColor="Black" />
<SelectedRowStyle BackColor="#008A8C" Font-Bold="True" ForeColor="White" />
<SortedAscendingCellStyle BackColor="#F1F1F1" />
<SortedAscendingHeaderStyle BackColor="#0000A9" />
<SortedDescendingCellStyle BackColor="#CAC9C9" />
<SortedDescendingHeaderStyle BackColor="#000065" />
<asp:SqlDataSource ID="sdsGvChrono" runat="server" ConnectionString="<%$ ConnectionStrings:MyConnectionString %>" SelectCommand="SELECT DISTINCT [Op], [Chrono] FROM [AHA_Main]" UpdateCommand="UPDATE [AHA_Main] SET [Chrono] = @Chrono WHERE [Op] = @Op">
<asp:Parameter Name="Op" Type="String" />
<asp:Parameter Name="Chrono" Type="Int32" />
Feb 16, 2017 06:00 AM|Chris Zhao|LINK
Include both the primary key, and your other key field like this:
Feb 16, 2017 05:20 PM|mavila|LINK
Hi Chris, thank you for your response. I'm not using the primary key in this instance. The primary key is the ID column which is the indexable identifier column that is entered automatically when a new record is inserted into the database. I'm not using
it because I want to SELECT DISTINCT Op/Chrono items and if I use the primary key, ID, it negates, in effect, the DISTINCT part of the SELECT statement and returns all items in the database which is not desirable.
Do I have to use the primary key? If so, is there a way to select the Op and Chrono fields as DISTINCT but not the ID field?
Incidentally, I did make the change you suggested - adding an "s" to DataKeyNames but it still doesn't work unfortunately.
Thanks again for your help.
Feb 16, 2017 06:17 PM|PatriceSc|LINK
Been a long time but I would try
Don't you have something in e.Exception? To me, it shouldn't fail silently (or at least without an exception is exposed somewhere). It should be easier to fix once the issue is known.
Your intent is to update multiple rows at the same time? I wonder if it couldn't be that by design it expects to change a single row using a pk and maybe it could throw an exception (even if not seen for now).
Feb 16, 2017 07:36 PM|mavila|LINK
Thanks, again. I followed the link and used the code in the RowUpdated event, launched the web page, made the edit and there was no exception - it responded that the row was updated successfully. But, it wasn't actually updated in the database. The old
value remained in the database. Weird.
Yes, the Gridview shows DISTINCT items from the database (so they aren't unnecessarily repeated in the Gridview). The user then selects an item to edit/update a numerical value associated with the user-selected item. But, there are multiple instances of
that user-selected item in the database and so, yes, all of them (multiple rows) need to be changed in accordance with the user's edit.
Feb 21, 2017 07:21 AM|Chris Zhao|LINK
Use the DataKeyNames property to specify the field or fields that represent the primary key of the data source. You should only set this property to the field or fields that are required to uniquely identify