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
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

Saravanan GanesanPosted Aug 6, 2023, 6:05 PM
Hi Marius ,
It looks like you are using a
DataListcontrol to display a list of items and allow editing. TheDataListcontrol is part of ASP.NET Web Forms, and updating the values in edit mode should work as expected. However, since you mentioned that the values are not getting updated, there could be several reasons for this behavior.ViewState: Make sure that ViewState is enabled for the page and control. Ensure that the
EnableViewStateproperty is set totrueat both the page level and theDataListcontrol level.PostBack Handling: Ensure that you are handling postbacks correctly in your code-behind. If you are rebinding the
DataListon each postback, it could cause the values not to persist during the update.Editing Mode: Verify that you are entering edit mode correctly when clicking the "Edit" button. The
EditItemTemplateshould be displayed when you click the "Edit" button and switch theDataListto edit mode.Check Event Handlers: Double-check your event handlers in the code-behind to ensure that the
DataList1_UpdateCommandmethod is indeed called when the "Update" button is clicked.Binding Data: When calling
LoadData()to bind data to theDataList, make sure you are calling it within a!IsPostBackblock to avoid resetting the control state on postbacks.Debugging: Place breakpoints in the
DataList1_UpdateCommandmethod and examine the values ofidandtxtIntrebare.Textto ensure they have the correct values during the update. If you have tried all the above steps and the issue persists, you may want to share more details about theLoadData()method, the event handling, and how theDataListcontrol is being populated with data with more context.Marius VasilePosted Aug 7, 2023, 4:20 PM
I found the problem, it seems that the session is causing the update failure. So I probably should do the post back within session page load
Marius VasilePosted Aug 7, 2023, 4:06 PM
Ok, I've checked all items in your post Saravanan and for:
6. Debugging: Place breakpoints in the
DataList1_UpdateCommandmethod and examine the values ofidandtxtIntrebare.Textto ensure they have the correct values during the update.the id is correct but the textbox is not modified, it is showing the old value not the updated one
Marius VasilePosted Aug 6, 2023, 6:13 PM
It is really frustating because I followed an old project I did few years ago which is working even now without issues. This one though...is not. My code behind is below
I am using a Session to load data, no other Load_Data(). Edit mode and Update mode are as required, at least from my point of view
Marius VasilePosted Aug 6, 2023, 5:48 PM
Hi Saravanan, thank you for replying. I removed all visible="false" from all divs in the page. my entire View is below. still not working
Saravanan GanesanPosted Aug 6, 2023, 5:37 PM
Hi Marius ,
It appears that the issue might be related to the ViewState in ASP.NET Web Forms. When you set
Visible="false"on theDivIntrebarediv element containing theDataList, the content of theDataListis not preserved in the ViewState, which is causing the problem.To fix this issue, you can try the following steps:
Remove the
Visible="false"attribute from theDivIntrebarediv element. This ensures that the content of theDataListis present in the ViewState.Instead of using the
Visibleattribute to control the visibility of theDivIntrebarediv, you can use CSS to hide or show the element. For example, you can add the following CSS style to hide the div:DivIntrebarediv:Visibleproperty of theDivIntrebarediv instead of directly setting theDisplayproperty in the CSS class:With these changes, your
DataListcontent should now be preserved in the ViewState, and you should be able to update thetxtIntrebareTextBox correctly when the UpdateCommand is triggered.