Using the UPDATE function provided by the gridview, the data is not updated.

Source: Internet
Author: User

The problem is described in the title. The Code is as follows:

Page code

<asp:GridView ID="gdvCustomers" runat="server"             Height="210px" OnRowEditing="gdvCustomers_RowEditing" Width="586px" OnRowDeleting="gdvCustomers_RowDeleting" OnRowUpdating="gdvCustomers_RowUpdating" OnRowCancelingEdit="gdvCustomers_RowCancelingEdit" AutoGenerateColumns="False" DataKeyNames="customerID">            <Columns>            <asp:BoundField DataField="customerID" HeaderText="customerID">            </asp:BoundField>            <asp:BoundField DataField="companyName" HeaderText="companyName">            </asp:BoundField>            <asp:BoundField DataField="address" HeaderText="address">            </asp:BoundField>            <asp:CommandField ShowEditButton="True" ShowDeleteButton="True" />            </Columns>        </asp:GridView>

Background code

Protected void gdvcustomers_rowupdating (Object sender, gridviewupdateeventargs e) {string id = (textbox) This. gdvcustomers. rows [E. rowindex]. cells [0]. controls [0]). text. tostring (); string name = (textbox) This. gdvcustomers. rows [E. rowindex]. cells [1]. controls [0]). text. tostring (); string address = (textbox) This. gdvcustomers. rows [E. rowindex]. cells [2]. controls [0]). text. tostring (); string SQL = "Update MERs set companyName = '" + name + "', address = '"+ address +" 'where mermerid =' "+ ID +" '"; sqlconnection con = new sqlconnection (" Integrated Security = true; server = .; database = northwind; "); sqlcommand cmd = new sqlcommand (SQL, con); con. open (); cmd. executenonquery (); con. close (); gdvcustomers. editindex =-1; // cancel the editing status dataload ();}

 

Check the gdvcustomers_rowupdating event of the gridview. The code in the event is correct. After tracking the SQL statement, it is found that the read value is not the modified value, but the original value.

Finally, we found that the key to the problem was not in gdvcustomers_rowupdating, but in page_load. If we didn't determine whether it was a callback, we put the code bound to the gridview data into the ispostback judgment statement, and the problem was solved. The Code is as follows:

if (!this.IsPostBack){     DataLoad();}

I haven't gotten a webform for a long time, so I have forgotten such basic knowledge!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.