I am trying to update a field from a Datalist but the value from Textbox is not changing. On Update command, the value is not changed
and code behind
if (e.CommandName == "Update")
{
TextBox Locatie = (TextBox)e.Item.FindControl("txtLocatieEdit");
using (SqlConnection conn = new SqlConnection(connString))
{
string sqlQuery = "UPDATE tblDefRap SET Locatie = @Locatie WHERE IdDR = @IdDR";
using (SqlCommand cmd = new SqlCommand(sqlQuery, conn))
{
cmd.CommandType = CommandType.Text;
cmd.Parameters.AddWithValue("@IdDR", iddr);
cmd.Parameters.AddWithValue("@Locatie", Locatie.Text);
conn.Open();
int result = cmd.ExecuteNonQuery();
if (result > 0)
{
ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "alertMessage", "alert('Record Inserted Successfully')", true);
}
DataList2.EditItemIndex = -1;
conn.Close();
}
GetWorkOrder();
}
}
The value entered in the database is the same as the one trying to change and I don't understand why

Marius VasilePosted May 4, 2025, 4:53 PM
Yeah, beginer mistake. I didn't used if (!IsPostBack) i Page Load for loading data. Is working fine now
Eliana BlakePosted May 4, 2025, 7:12 AM
From the details you provided regarding the issue with updating a field from a Datalist in ASP.NET where the value from the TextBox is not changing, it appears that the problem lies in how the data is being handled during the update process.
Looking at the code snippet you shared, specifically the C# code behind for handling the Update command, here are a few points to consider:
1. Data Binding: Ensure that the Datalist is being bound correctly, especially during postbacks. If the Datalist data binding is not properly handled, the changes made in the TextBox might not persist.
2. Update Logic: The logic for updating the database seems correct, where you are trying to update the "Locatie" field in the "tblDefRap" table based on the "IdDR". Double-check that the "iddr" value is being passed correctly and corresponds to the correct record for updating.
3. TextBox Identification: Validate that the TextBox "txtLocatieEdit" is being correctly identified in the Datalist during the update process. Make sure the FindControl method is targeting the right control within the Datalist.
4. Data Persistence: Check for any other code that might overwrite the value of the "Locatie" field after it has been updated. There could be a mechanism resetting it back to its original value somewhere else in the code.
If the above aspects seem in order, you might want to troubleshoot by:
- Debugging: Take advantage of breakpoints to step through the code and see if the TextBox value is being properly retrieved during the Update command.
- Directly Setting Text: Instead of relying on databinding with `<%# Eval("Locatie") %>`, try setting the Text property of the TextBox directly in the code-behind during the Datalist ItemDataBound or ItemCreated events.
By reviewing these points and performing some troubleshooting, you should be able to identify the root cause of why the TextBox value is not updating in your ASP.NET application and make the necessary adjustments to resolve the issue.