Hi!
I have a problem. I cannot update the row in GridView control.

<asp:Label ID="Label1" runat="server" Text="Username: "></asp:Label><asp:TextBox
                                ID="unTextBox" runat="server"></asp:TextBox><%--<asp:RequiredFieldValidator ID="usernameRequiredFieldValidator"
                                    runat="server" ErrorMessage="*" ControlToValidate="unTextBox"></asp:RequiredFieldValidator>--%><asp:Button ID="unSearchButton" runat="server"
                                    Text="Search"/><br /><br />
                            <asp:GridView ID="GridView1" runat="server" AllowPaging="True" AllowSorting="True" AutoGenerateColumns="False" DataSourceID="MySqlDataSource" CellPadding="4" ForeColor="#333333" Font-Size="Small">
                                <Columns>
                                    <asp:CommandField ShowEditButton="True" />
                                    <asp:BoundField DataField="Name" HeaderText="Name" SortExpression="Name" ReadOnly="true" />
                                    <asp:BoundField DataField="Last_Name" HeaderText="Last Name" SortExpression="Last_Name" ReadOnly="true" />
                                    <asp:BoundField DataField="Username" HeaderText="Username" SortExpression="Username" ReadOnly="true" />
                                    <asp:BoundField DataField="Email" HeaderText="E-mail" SortExpression="Email" ReadOnly="true" />
                                    <asp:CheckBoxField DataField="IsApproved" HeaderText="User Approved?" SortExpression="IsApproved" />
                                </Columns>
                                <RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
                                <FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
                                <PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
                                <SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
                                <HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
                                <EditRowStyle BackColor="#999999" />
                                <AlternatingRowStyle BackColor="White" ForeColor="#284775" />
                            </asp:GridView>
                            <asp:SqlDataSource ID="MySqlDataSource" runat="server" ConnectionString="server=servername;user id=userid;Password=password;database=dbname;persist security info=True" ProviderName="MySql.Data.MySqlClient" SelectCommand="SELECT p.Name, p.Last_Name, u.Username, u.Email, u.IsApproved FROM users u INNER JOIN person p ON u.PERSON_REF = p.ID WHERE u.IsApproved=0" UpdateCommand="UPDATE users SET IsApproved=?IsApproved WHERE Username=?Username " >
                            <%--<SelectParameters><asp:ControlParameter ControlID="unTextBox" Name="Username" PropertyName="Text" /></SelectParameters>--%>
                            <UpdateParameters><asp:Parameter Type="Int16" Name="IsApproved"/><asp:Parameter Type="String" Name="Username" /></UpdateParameters></asp:SqlDataSource>

When I try edit and check the checkbox in User Approved field and then click the Update it doesn't not update the field. What am I doing wrong?

Dani AI

Generated

Quick practical checklist to get the GridView update working.

Summary of likely causes (tying back to the thread): is right to suggest using an edit template, and 's question about a primary key is exactly the right line of thinking. The usual reasons an edit appears to do nothing are: the row key is not being sent to the Update, the edited control is not two-way bound, or the SqlDataSource parameters are the wrong type/name for the provider.

Minimal fixes to apply now:

  1. Give the GridView a DataKeyNames value that contains the table key (prefer the numeric PK; Username is OK only if it is guaranteed unique). This ensures the key value is available during update.

  2. Make the checkbox editable with a TemplateField and use Bind(...) in the EditItemTemplate so the new value is posted back (Eval is one-way). Example:

    <asp:GridView ID="GridView1" runat="server" DataKeyNames="Username">
      <Columns>
        <asp:CommandField ShowEditButton="True" />
        <asp:TemplateField HeaderText="User Approved?">
          <ItemTemplate>
            <asp:CheckBox ID="chkView" runat="server" Checked='<%# Eval("IsApproved") %>' Enabled="false" />
          </ItemTemplate>
          <EditItemTemplate>
            <asp:CheckBox ID="chkEdit" runat="server" Checked='<%# Bind("IsApproved") %>' />
          </EditItemTemplate>
        </asp:TemplateField>
      </Columns>
    </asp:GridView>
  3. Match the SqlDataSource parameters to the actual types: if IsApproved is stored as boolean/tinyint use Type="Boolean" (or convert to 0/1 and use Int32). Make sure the UpdateCommand parameter names match what the provider expects.

If it still fails: attach a handler to RowUpdating and log/inspect e.NewValues and e.Keys (or handle SqlDataSource.Updating) to see which values are present. Also verify the actual SQL executed (MySQL general_log or a query logger) to confirm parameters were bound. Final note: prefer a numeric primary key for updates to avoid ambiguity and concurrency issues.

Recommended Answers

All 2 Replies

Hi Donish,

I don't know if you solved the problem, but here is my suggestion.
I don't see any code to update the GridView. I mean, there is an UpdateCommand in the DataSource, but in the GridView there's nothing saying to actually perform the update. Do you have any code in the code behind? If so, please post. =)
Where are you getting the value of the CheckBox? How are you editing the row?
What I suggest for now is to add an <EditItemTemplate> to your GridView. I think it's a easy way to edit the gridView.

If you have any questions (or if you solved the problem already) let us know.

Ana

do u have any primary key in ur table...?

if yes then get back 2 me...asap..

i think i have the solution..

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.