How to display data from database into textbox, and update it

后端 未结 4 1912
难免孤独
难免孤独 2020-12-15 02:08

I can display my data in my textbox, dropdownlist after retrieve data from sql database, but when i proceed with my update button, the information edited in the textbox or d

相关标签:
4条回答
  • 2020-12-15 02:24

    Populate the text box values in the Page Init event as opposed to using the Postback.

    protected void Page_Init(object sender, EventArgs e)
    {
        DropDownTitle();
    }
    
    0 讨论(0)
  • 2020-12-15 02:27

    The answer is .IsPostBack as suggested by @Kundan Singh Chouhan. Just to add to it, move your code into 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 讨论(0)
  • 2020-12-15 02:31

    Wrap your all statements in !IsPostBack condition on page load.

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

    This will fix your issue.

    0 讨论(0)
  • 2020-12-15 02:33
    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);
            }
    
        }
    }
    
    0 讨论(0)
提交回复
热议问题