locked
problem with update in datalist RRS feed

  • Question

  • User1829979718 posted

    I am currently using a datalist to update the database with a textbox control.
    The problem is that when I am pressing "Edit" and entering values in the textbox and when trying to update the database, the database is not getting updated with the entered value.

    Could someone please let me know the exact procedure to update the database with the value from the textbox?

    <asp:DataList ID="DataList1" runat="server" DataKeyField="memberID"
    oneditcommand="DataList1_EditCommand"
    onupdatecommand="DataList1_UpdateCommand">
    private void Load(){
    dal db = new dal();
    DataTable dt = new DataTable();
    dt = db.run_dr("SELECT [memberID], [Name], [Family], [Tell], [Email]");
    DataList1.DataSource = dt;
    DataList1.DataBind();
    protected void DataList1_UpdateCommand(object source, DataListCommandEventArgs e)
    {
    TextBox txtName = (TextBox)e.Item.FindControl("TextName");
    TextBox txtFamily = (TextBox)e.Item.FindControl("TextFamily");
    TextBox txtTell = (TextBox)e.Item.FindControl("TextTell");
    if (Page.IsValid)
    {
    db.run_dr("update members set Name=N'" + txtName.Text +
    "',Family=N'" + txtFamily.Text +
    "',Tell='" + txtTell.Text.Trim() +
    "' where memberID="+ DataList1.DataKeys[e.Item.ItemIndex].ToString());
    DataList1.DataBind();
    DataList1.EditItemIndex = -1;
    }
    }

    Problem is the altered value in the textbox is not getting reflected

        protected void DataList1_EditCommand(object source, DataListCommandEventArgs e)
        {
        DataList1.EditItemIndex = e.Item.ItemIndex;
        Load();
        }





    Friday, September 21, 2012 3:02 AM

Answers

  • User-1360095595 posted
    Use parameterized queries; also, don't call Databind after the update, instead call Load().
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, September 21, 2012 3:08 AM