Update a row in GridView ASP NET doesn't work

1k Views Asked by At

I'm new ASP.NET developer and trying to update a row in a GridView. I have try many solution but it don't work. When I update a row, not error message but the update is not taken into account.

<asp:GridView ID="grd_quest" Visible = "False" runat="server" CellPadding="4" 
                  DataSourceID="ERP_questionn" ForeColor="#333333" GridLines="None" 
                  AutoGenerateColumns="False" DataKeyNames="QUE_id">
                <AlternatingRowStyle BackColor="White" ForeColor="#284775" />
    <Columns>
        <asp:CommandField ShowEditButton="true" />  

        <asp:BoundField DataField="QUE_id" HeaderText="ID" ReadOnly="True" 
            SortExpression="QUE_id" ItemStyle-Width="10%" />
        <asp:BoundField DataField="QUE_libelle" HeaderText="Libelle" 
            SortExpression="QUE_libelle" />

    </Columns>
    <EditRowStyle BackColor="#999999" />
    <FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
    <HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
    <PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
    <RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
    <SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
    <SortedAscendingCellStyle BackColor="#E9E7E2" />
    <SortedAscendingHeaderStyle BackColor="#506C8C" />
    <SortedDescendingCellStyle BackColor="#FFFDF8" />
    <SortedDescendingHeaderStyle BackColor="#6F8DAE" />
</asp:GridView>

<asp:SqlDataSource ID="ERP_questionn" runat="server" 
      ConnectionString="<%$ ConnectionStrings:ERPConnectionString %>" 
      SelectCommand="" 
      UpdateCommand=
      "UPDATE [TR_QUESTION] SET [QUE_libelle] = @QUE_libelle WHERE [QUE_id] = @QUE_id">
    <UpdateParameters>
        <asp:Parameter Name="QUE_libelle" Type="String" />
        <asp:Parameter Name="QUE_id" Type="Int32" />
    </UpdateParameters>
</asp:SqlDataSource>

The select command is not written here because it changes in function of others list so I write the SelectCommand in C#. I hope it's clear for you

[EDIT]

I had try an other solution with this in C# :

protected void grd_quest_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        SqlConnection sqlConnection1 = new SqlConnection(connectionString);
        int id = Convert.ToInt32(grd_quest.DataKeys[e.RowIndex].Value.ToString());
        GridViewRow row = (GridViewRow)grd_quest.Rows[e.RowIndex];

        //TextBox txtname=(TextBox)gr.cell[].control[];  

        TextBox textadd = (TextBox)row.Cells[2].Controls[0];

        //TextBox textadd = (TextBox)row.FindControl("txtadd");  
        //TextBox textc = (TextBox)row.FindControl("txtc");  
        grd_quest.EditIndex = -1;
        sqlConnection1.Open();
        //SqlCommand cmd = new SqlCommand("SELECT * FROM detail", conn);  
        SqlCommand cmd = new SqlCommand("UPDATE TR_QUESTION SET QUE_libelle = @p_libelle where QUE_id = @p_id ", sqlConnection1);
        cmd.Parameters.AddWithValue("@p_id", id);
        cmd.Parameters.AddWithValue("@p_libelle", textadd.Text);         
        cmd.ExecuteNonQuery();
        sqlConnection1.Close();
        grd_quest.DataBind();

    }

But it does not work too, I have an error message who say that :

The data source 'ERP_questionn' does not support updating unless UpdateCommand is specified. I have try : UpdateCommand = "" but it does not work too

[EDIT new try ]

    protected void grd_quest_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {

        string id = grd_quest.DataKeys[e.RowIndex].Value.ToString();
        string libelle = ((TextBox)grd_quest.Rows[e.RowIndex].Cells[2].Controls[0]).Text;
        ERP_questionn.UpdateParameters["QUE_id"].DefaultValue = id;
        ERP_questionn.UpdateParameters["QUE_libelle"].DefaultValue = libelle;
        ERP_questionn.Update();
        //ERP_questionn.SelectCommand = DropDownList1.SelectedValue;

    }

When I update a row, not error message but the update is not taken into account.

[EDIT]

Maybe that can help,to display my data in my gridView I did this :

protected void lst_facteur_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (lst_pos.SelectedValue == "1")
        {
            ERP_questionn.SelectCommand = "select * FROM TR_QUESTION WHERE FAC_id =" + lst_facteur.SelectedValue + " AND QUE_ordreUsine IS NOT NULL";

        }
        else if (lst_pos.SelectedValue == "2")
        {
            ERP_questionn.SelectCommand = "select * FROM TR_QUESTION WHERE FAC_id =" + lst_facteur.SelectedValue + " AND QUE_ordreBureau IS NOT NULL";
        }

        grd_quest.DataBind();
        grd_quest.Visible = true;
        txt_question.Visible = true;
        btn_add_question.Visible = true;

I have always the update problem...

2

There are 2 best solutions below

3
On

You have to set SelectCommand = "SELECT * FROM [TR_QUESTION]" in HTML Code also:

<asp:GridView ID="grd_quest" runat="server" AutoGenerateColumns="False" DataKeyNames="QUE_id" DataSourceID="ERP_questionn">
    <Columns>
        <asp:CommandField ShowEditButton="True" />
        <asp:BoundField DataField="QUE_id" HeaderText="ID" InsertVisible="False" ReadOnly="True" SortExpression="QUE_id" />
        <asp:BoundField DataField="QUE_libelle" HeaderText="Libelle" SortExpression="QUE_libelle" />
    </Columns>
</asp:GridView>

<asp:SqlDataSource ID="ERP_questionn" runat="server" 
      ConnectionString="<%$ ConnectionStrings:ERPConnectionString %>" 
      SelectCommand = "SELECT * FROM [TR_QUESTION]" 
      UpdateCommand = 
      "UPDATE [TR_QUESTION] SET [QUE_libelle] = @QUE_libelle WHERE [QUE_id] = @QUE_id">
</asp:SqlDataSource>
0
On
"UPDATE [TR_QUESTION] SET [QUE_libelle] =? WHERE [QUE_id] =?"

As well as this, you have not set an update command event on the gridview.

onrowupdated="CustomersGridView_RowUpdated"