Marius Vasile

Marius Vasile

  • 599
  • 1.9k
  • 142.8k

SQL server update query not working

Aug 6 2023 2:55 PM

I want to edit a Datalist values and update but I can't see why the update procedure does not modify the text. What I have is

Page view

<asp:Panel ID="Panel1" runat="server">
    <div id="DivIntrebare" runat="server" Visible="false">
        <div class="row no-gutters mb-4">
            <asp:DataList 
                ID="DataList1" 
                runat="server" 
                style="width:100%"
                DataKeyField="IdIntrebare"
                OnEditCommand="DataList1_EditCommand"
                OnUpdateCommand="DataList1_UpdateCommand"
                OnCancelCommand="DataList1_CancelCommand"
                OnDeleteCommand="DataList1_DeleteCommand">  
                <HeaderTemplate> 
                    <div class="row no-gutters">
                        <div class="col-1">
                            <asp:Label Text="Sectiune" class="form-control form-control-sm lfc-green" runat="server" />
                        </div>
                        <div class="col-4">
                            <asp:Label Text="Procedura" class="form-control form-control-sm lfc-green" runat="server" />
                        </div>
                        <div class="col-7">
                            <asp:Label Text="Intrebare" class="form-control form-control-sm lfc-green" runat="server" />
                        </div>
                    </div>
                </HeaderTemplate>
                <ItemTemplate>
                    <div class="row no-gutters">
                        <div class="col-1">
                            <asp:TextBox ID="txtSectiune" ReadOnly="true" class="form-control form-control-sm bg-white" runat="server" Text='<%#Eval("Sectiune")%>' />
                            <asp:Button ID="btnEdit" Text="Edit" runat="server" CommandName="Edit" CausesValidation="false" UseSubmitBehavior="False" class="btn lfc-green col-12 m-0 p-0" style="font-size:14px; height:22px" />
                            <asp:Button ID="btnDelete" Text="Delete" runat="server" CommandName="Delete" CausesValidation="false" UseSubmitBehavior="False" class="btn lfc-red col-12 m-0 p-0" style="font-size:14px; height:22px" />
                        </div>
                        <div class="col-4">
                            <asp:TextBox ID="txtCodProcedura" ReadOnly="true" class="form-control form-control-sm bg-white" runat="server" Text='<%#Eval("CodProcedura")%>' />
                            <asp:TextBox ID="txtProcedura" ReadOnly="true" class="form-control form-control-sm bg-white text-uppercase" runat="server" TextMode="MultiLine" Rows="2" style="resize:none;" Text='<%#Eval("Procedura")%>' />
                        </div>
                        <div class="col-7">
                            <asp:TextBox ID="txtIntrebare" ReadOnly="true" class="form-control form-control-sm bg-white text-uppercase" runat="server" TextMode="MultiLine" Rows="4" style="resize:none; height:83px;" Text='<%#Eval("Intrebare")%>' />
                            <asp:TextBox ID="txtID" runat="server" Text='<%#DataBinder.Eval(Container.DataItem,"IdIntrebare")%>' Visible="false"/>
                        </div>
                    </div>
                    <hr />
                </ItemTemplate>
                <EditItemTemplate>
                    <div class="row no-gutters">
                        <div class="col-1">
                            <asp:TextBox ID="txtSectiune" ReadOnly="true" class="form-control form-control-sm bg-white" runat="server" Text='<%#Eval("Sectiune")%>' />
                            <asp:Button ID="btnUpdate" Text="Update" runat="server" CommandName="Update" CausesValidation="false" UseSubmitBehavior="False" class="btn bg-primary col-12 m-0 p-0" style="font-size:14px; height:22px" />
                            <asp:Button ID="btnCancel" Text="Cancel" runat="server" CommandName="Cancel" CausesValidation="false" UseSubmitBehavior="False" class="btn bg-warning col-12 m-0 p-0" style="font-size:14px; height:22px" />
                        </div>
                        <div class="col-4">
                            <asp:TextBox ID="txtCodProcedura" ReadOnly="true" class="form-control form-control-sm bg-white" runat="server" Text='<%#Eval("CodProcedura")%>' />
                            <asp:TextBox ID="txtProcedura" ReadOnly="true" class="form-control form-control-sm text-uppercase bg-white" runat="server" TextMode="MultiLine" Rows="2" style="resize:none;" Text='<%#Eval("Procedura")%>' />
                        </div>
                        <div class="col-7">
                            <asp:TextBox ID="txtIntrebare" class="form-control form-control-sm text-uppercase" runat="server" TextMode="MultiLine" Rows="4" style="resize:none; height:83px;" Text='<%#Eval("Intrebare")%>' />
                            <asp:TextBox ID="txtID" runat="server" Text='<%#DataBinder.Eval(Container.DataItem,"IdIntrebare")%>' Visible="false"/>
                            <asp:TextBox ID="txtIDP" runat="server" Text='<%#DataBinder.Eval(Container.DataItem,"IdProcedura")%>' Visible="false"/>
                        </div>
                    </div>
                </EditItemTemplate>
            </asp:DataList>
        </div>
    </div>
</asp:Panel>

code behind

protected void DataList1_UpdateCommand(object source, DataListCommandEventArgs e)
{
    using (SqlConnection conn = new SqlConnection(connString))
    {
        int id = Convert.ToInt32(DataList1.DataKeys[e.Item.ItemIndex].ToString());
        TextBox txtIntrebare = (TextBox)e.Item.FindControl("txtIntrebare");

        string sqlQuery = "UPDATE tblIntrebare SET Intrebare=@Intrebare WHERE IdIntrebare=@id";
        using (SqlCommand cmd = new SqlCommand(sqlQuery, conn))
        {
            cmd.CommandType = CommandType.Text;
            cmd.Parameters.Add("@id", SqlDbType.Int).Value = id;
            cmd.Parameters.Add("@Intrebare", SqlDbType.NVarChar).Value = txtIntrebare.Text;

            conn.Open();
            cmd.ExecuteNonQuery();
            conn.Close();
        }
        DataList1.EditItemIndex = -1;
        LoadData();
    }
}

So, on update the int id is correct but txtIntrebare is not modified at all


Answers (6)