I have an Access database and I want to fill some text boxes with data from a table. I'm using the following method for an example:

Me.txtCompany.Text = dr.GetString(1)

That works great. I have that in the PageLoad event.

Then I want a user to be able to change that value and submit it and have it update the database field.

So to do that, I have something like this:

strSQLCompany = "UPDATE Customers SET CompanyName='" & txtCompany.Text.Replace("'", "_") & "'"

This, however, doesn't work. It updates the database with the data that's entered into the text boxes on PageLoad instead of what's typed. I have the above code in my button click event.

I've tested my button click event by taking out the fill code in the PageLoad (making empty text boxes) and filling them in and submitting it and the correct information gets updated in the database, so I know that part of the code is correct.

Is there a reason why the button click event is taking the text from PageLoad instead of what's actually typed in the text box?

Dani AI

Generated

Short summary and likely cause

The symptom described by (textbox filled from a DataReader on load, but the UPDATE uses the PageLoad value rather than the typed value) is almost always caused by the page re-populating the control during the postback lifecycle. 's advice to limit initial population to the first load is the right direction; since that was tried with no fix, further troubleshooting should look for other places that re-bind or re-create the control so the user's typed text is overwritten before the click handler runs.

Checklist of concrete checks (order matters)

  • Put breakpoints or Trace/Debug.WriteLine at the start and end of Page_Load (and in PreRender) and inside the button click. Compare the textbox value at each point to see exactly when it changes.
  • Search for any calls to DataBind(), Page.DataBind(), or assignment of a DataSource (templated controls, FormView/GridView) that might run on every request and overwrite posted values.
  • Confirm the page has a single <form runat="server"> and the textbox is a server control (runat="server"). If the control is created dynamically, confirm it is recreated on every request during Page_Init with the same ID.
  • If using an UpdatePanel, verify partial-postback triggers and boundaries.
  • Make sure any DataReader used to populate controls is closed before running the UPDATE (or use a separate connection).

Safer update pattern for Access (VB.NET, OleDb)

Parameterized queries avoid brittle string manipulation and SQL injection. Example pattern:

Using cn As New OleDbConnection(connString)
  cn.Open()
  Using cmd As New OleDbCommand("UPDATE Customers SET CompanyName = ? WHERE CustomerID = ?", cn)
    cmd.Parameters.AddWithValue("?", txtCompany.Text)
    cmd.Parameters.AddWithValue("?", customerId)
    cmd.ExecuteNonQuery()
  End Using
End Using

Notes and cautions

Always include a WHERE clause to avoid updating every row. The most common remaining cause when an IsPostBack guard appears present is a late rebind (DataBind or templated control) that runs after the posted value is applied; the breakpoint trace will show exactly where the textbox value is replaced.

Recommended Answers

All 3 Replies

*bump* anyone?

Have you written your code in IsPostBack method ?

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Not IsPostBack Then
   Me.txtCompany.Text = dr.GetString(1)
End If

Protected Sub CmdSave_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles CmdSave.Click
Dim strSQLCompany  as String
strSQLCompany = "UPDATE Customers SET CompanyName='" & txtCompany.Text.Replace("'", "_") & "'"    
End Sub

Thanks,

Kusno.

I'm working on another site and I'm running into this same problem again (I never got it to work before either).

In the Page_Load event, I tried doing your If Not IsPostBack suggestion for filling the text boxes and I still get the same result.

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.