How to display data from a database in a text field and update it

I can display my data in my text box in the drop-down list after retrieving the data from the sql database, but when I continue my update button, the information changed in the text box or drop-down list will not be updated in the database. I tried to remove the code in the page loading and re-enter all the data manually, it could be an update. All I want to do is get the data from the database and display it in the text box, and it can be an update in the database after making some changes from the text box. Can someone help me with this? Thanks.

using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using System.Data.SqlClient; using System.Configuration; using System.Data; public partial class UpdateCustomerDetails : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { SqlConnection con1 = new SqlConnection("Data Source=USER-PC;Initial Catalog=webservice_database;Integrated Security=True"); DataTable dt = new DataTable(); con1.Open(); SqlDataReader myReader = null; SqlCommand myCommand = new SqlCommand("select * from customer_registration where username='" + Session["username"] + "'", con1); myReader = myCommand.ExecuteReader(); while (myReader.Read()) { TextBoxPassword.Text = (myReader["password"].ToString()); TextBoxRPassword.Text = (myReader["retypepassword"].ToString()); DropDownListGender.SelectedItem.Text = (myReader["gender"].ToString()); DropDownListMonth.Text = (myReader["birth"].ToString()); DropDownListYear.Text = (myReader["birth"].ToString()); TextBoxAddress.Text = (myReader["address"].ToString()); TextBoxCity.Text = (myReader["city"].ToString()); DropDownListCountry.SelectedItem.Text = (myReader["country"].ToString()); TextBoxPostcode.Text = (myReader["postcode"].ToString()); TextBoxEmail.Text = (myReader["email"].ToString()); TextBoxCarno.Text = (myReader["carno"].ToString()); } con1.Close(); } protected void Button1_Click(object sender, EventArgs e) { SqlConnection con = new SqlConnection("Data Source=USER-PC;Initial Catalog=webservice_database;Integrated Security=True"); SqlCommand cmd = new SqlCommand("UPDATE customer_registration SET password = @password, retypepassword = @retypepassword, gender = @gender, birth = @birth, address = @address, city = @city, country = @country, postcode = @postcode, email = @email, carno = @carno where username='" + Session["username"] + "'", con); con.Open(); cmd.Parameters.AddWithValue("@password", TextBoxPassword.Text); cmd.Parameters.AddWithValue("@retypepassword", TextBoxRPassword.Text); cmd.Parameters.AddWithValue("@gender", DropDownListGender.Text); cmd.Parameters.AddWithValue("@birth", DropDownListDay.Text + DropDownListMonth.Text + DropDownListYear.Text); cmd.Parameters.AddWithValue("@address", TextBoxAddress.Text); cmd.Parameters.AddWithValue("@city", TextBoxCity.Text); cmd.Parameters.AddWithValue("@country", DropDownListCountry.Text); cmd.Parameters.AddWithValue("@postcode", TextBoxPostcode.Text); cmd.Parameters.AddWithValue("@email", TextBoxEmail.Text); cmd.Parameters.AddWithValue("@carno", TextBoxCarno.Text); cmd.ExecuteNonQuery(); con.Close(); if (IsPostBack) { Response.Redirect("UpdateSuccess.aspx"); } } 
+4
source share
4 answers

Wrap all statements in !IsPostBack when the page loads.

 protected void Page_Load(object sender, EventArgs e) { if(!IsPostBack) { // all statements } } 

This will fix your problem.

+4
source
 protected void Page_Load(object sender, EventArgs e) { DropDownTitle(); } protected void DropDownTitle() { if (!Page.IsPostBack) { string connection = System.Configuration.ConfigurationManager.ConnectionStrings["AuzineConnection"].ConnectionString; string selectSQL = "select DISTINCT ForumTitlesID,ForumTitles from ForumTtitle"; SqlConnection con = new SqlConnection(connection); SqlCommand cmd = new SqlCommand(selectSQL, con); SqlDataReader reader; try { ListItem newItem = new ListItem(); newItem.Text = "Select"; newItem.Value = "0"; ForumTitleList.Items.Add(newItem); con.Open(); reader = cmd.ExecuteReader(); while (reader.Read()) { ListItem newItem1 = new ListItem(); newItem1.Text = reader["ForumTitles"].ToString(); newItem1.Value = reader["ForumTitlesID"].ToString(); ForumTitleList.Items.Add(newItem1); } reader.Close(); reader.Dispose(); con.Close(); con.Dispose(); cmd.Dispose(); } catch (Exception ex) { Response.Write(ex.Message); } } } 
+1
source

Answer .IsPostBack proposed by @Kundan Singh Chouhan. To add to it, move your code to a separate method from Page_Load

 private void PopulateFields() { using(SqlConnection con1 = new SqlConnection("Data Source=USER-PC;Initial Catalog=webservice_database;Integrated Security=True")) { DataTable dt = new DataTable(); con1.Open(); SqlDataReader myReader = null; SqlCommand myCommand = new SqlCommand("select * from customer_registration where username='" + Session["username"] + "'", con1); myReader = myCommand.ExecuteReader(); while (myReader.Read()) { TextBoxPassword.Text = (myReader["password"].ToString()); TextBoxRPassword.Text = (myReader["retypepassword"].ToString()); DropDownListGender.SelectedItem.Text = (myReader["gender"].ToString()); DropDownListMonth.Text = (myReader["birth"].ToString()); DropDownListYear.Text = (myReader["birth"].ToString()); TextBoxAddress.Text = (myReader["address"].ToString()); TextBoxCity.Text = (myReader["city"].ToString()); DropDownListCountry.SelectedItem.Text = (myReader["country"].ToString()); TextBoxPostcode.Text = (myReader["postcode"].ToString()); TextBoxEmail.Text = (myReader["email"].ToString()); TextBoxCarno.Text = (myReader["carno"].ToString()); } con1.Close(); }//end using } 

Then call it in your Page_Load

 protected void Page_Load(object sender, EventArgs e) { if(!Page.IsPostBack) { PopulateFields(); } } 
0
source

Fill text box values ​​in the Page Init event, rather than using Postback.

 protected void Page_Init(object sender, EventArgs e) { DropDownTitle(); } 
0
source

All Articles