I've got a form prepopulated from a row in a database (grabs the querystring from the URL to determine the row) and this works fine. I'm using the pageload function on the code-behind file to do this.

The button onclick has a function that is supposed to take the data from the form and put it back to the database. Here's that code:

using (SqlConnection updater = new SqlConnection(ConfigurationManager.ConnectionStrings["DB"].ToString()))
            {
                updater.Open();
                string up = "UPDATE roomtable SET ";
                up += "name='" + namein.Text.ToString() + "', country='" + countryin.Text.ToString() + "', state='" + statesin.Text.ToString() + "' ";
                up += "WHERE rid=" + curid;
                //curid is already declared and correct

                SqlCommand finalupdate = new SqlCommand(up);
                finalupdate.Connection = updater;
                finalupdate.ExecuteNonQuery();
                updater.Close();                
            }

However, when I replace the form.Text.ToString() with hardcoded data, it will work after TWO clicks of the button. The first click will refresh the page (and the data on all of form elements) and the second click will apply the hardcoded changes to the database.

Any ideas on what I am doing wrong?
Thank you!!

you have to rebind all the values...

i.e update works on first click but you dont update the row values.

so after update query fire the select query and rebind your controls.

it should work

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.