Hi all,

i had Web Page include DataList control , TextBox and Button ..


DataList1 : Getting Data from SQLDB

TextBox : for Editing Selected Field in DataList

Button : Submit Editing


when i Click Button it Editing Data Succefully in SQLDB , but in same Time DataList Control doesn't changed untill i refresh the Page !!

how could i make DataList changing Data or Reloading it after Submit Edit in same Page without need to Refresh the Page .. !!


i Hope u understand me , Sorry for My Weak English :(

Dani AI

Generated

As explained, the root cause is not that the database update fails but that the page never rebinds the DataList to fresh data after the update. ’s BindData() is the right place to centralize the select-and-bind logic; the important details are ordering (commit the update first), creating a fresh result set, and calling the bind method immediately afterward. For immediate, in-page feedback without a manual browser refresh, rebind on the server side or use a partial-AJAX update.

A solid server-side pattern (button click handler) is: persist the edit with a parameterized command inside using blocks, ensure the update returned an affected-rows count, then call the data-binding method that fetches a new DataTable/reader and calls DataList.DataBind().

protected void btnSave_Click(object sender, EventArgs e)
{
    using (var cn = new SqlConnection(connString))
    using (var cmd = new SqlCommand("UPDATE MyTable SET Title=@t WHERE Id=@id", cn))
    {
        cmd.Parameters.AddWithValue("@t", txtEdit.Text);
        cmd.Parameters.AddWithValue("@id", hiddenId.Value);
        cn.Open();
        int rows = cmd.ExecuteNonQuery();
    }

    BindData(); // re-run the select and call DataList.DataBind()
}

To avoid a full-page flicker, wrap the DataList and editing controls in an UpdatePanel (or perform a client-side AJAX call to a WebMethod/Web API and update the DOM). Example UpdatePanel markup:

<asp:UpdatePanel runat="server">
  <ContentTemplate>
    <asp:DataList ID="DataList1" runat="server" />
    <asp:TextBox ID="txtEdit" runat="server" />
    <asp:Button ID="btnSave" runat="server" OnClick="btnSave_Click" Text="Save" />
  </ContentTemplate>
</asp:UpdatePanel>

Troubleshooting checklist: confirm ExecuteNonQuery() returns >0, avoid re-binding too early in Page_Load (use if (!IsPostBack) for initial load), always fetch a new DataTable/reader (don’t reuse stale cached objects), disable any output/SQL caching that might hide changes, and use parameterized queries/using blocks to ensure correct commits and disposal.

Recommended Answers

All 3 Replies

When you say data is updated in the database, you mean you have handled the page postback correctly.
Also you are saying the datalist is filled correctly when page is refreshed, that means, the datalist filling code is there and working AND the datalist depends on your latest update.
All you have to do is, logically, after updating database, fill the datalist and bind again. One of the standard practices is: add private method that will fill datalist. Call it on page load when not postback as well as call after database update.
If this helps, please mark thread as solved.

Need more Explanation, please !!

sorry , i'm Still Beginner :|

Thanks alot (Padtes)

i did it :

void BindData()
    {
        DataTable dt2 = new DataTable();
        da2.Fill(dt2);
        DataList2.DataSource = dt2;
        DataList2.DataBind();
    }
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.