如何显示从数据库中的数据到文本框,并更新 [英] How to display data from database into textbox, and update it

查看:343
本文介绍了如何显示从数据库中的数据到文本框,并更新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以在我的文本框显示我的数据,DropDownList的检索后从SQL数据库中的数据,但是当我与我的更新按钮进行,在文本框中或DropDownList的编辑的信息将不会更新到数据库中。我试图手动删除页面负载code和重新键中的所有数据,也可以是更新。我想要的是从数据库中检索数据,并在文本框中显示它,并且它可以更新到数据库中做后,从文本框的一些变化。有人可以帮助我在这?谢谢你。

 使用系统;
使用System.Collections.Generic;
使用System.Linq的;
使用的System.Web;
使用System.Web.UI程序;
使用System.Web.UI.WebControls;
使用System.Data.SqlClient的;
使用System.Configuration;
使用System.Data这;
公共部分类UpdateCustomerDetails:System.Web.UI.Page
{保护无效的Page_Load(对象发件人,EventArgs的发送)
{
    SqlConnection的CON1 =新的SqlConnection(数据源= USER-PC;初始目录= webservice_database;集成安全性=真);
    DataTable的DT =新的DataTable();
    con1.Open();
    SqlDataReader的myReader = NULL;
    的SqlCommand myCommand =新的SqlCommand('+会话[用户名] +,CON1从customer_registration其中username =选择*');    myReader = myCommand.ExecuteReader();    而(myReader.Read())
    {
        TextBoxPassword.Text =(myReader [密码]的ToString());
        TextBoxRPassword.Text =(myReader [retypepassword]​​的ToString());
        DropDownListGender.SelectedItem.Text =(myReader [性别]的ToString());
        DropDownListMonth.Text =(myReader [诞生]的ToString());
        DropDownListYear.Text =(myReader [诞生]的ToString());
        TextBoxAddress.Text =(myReader [地址]的ToString());
        TextBoxCity.Text =(myReader [城市]的ToString());
        DropDownListCountry.SelectedItem.Text =(myReader [国]的ToString());
        TextBoxPost code.Text =(myReader [后code]的ToString());
        TextBoxEmail.Text =(myReader [电子邮件]的ToString());
        TextBoxCarno.Text =(myReader [卡尔诺(Carno)]的ToString());
    }
    con1.Close();
}
保护无效的button1_Click(对象发件人,EventArgs的发送)
{
    SqlConnection的CON =新的SqlConnection(数据源= USER-PC;初始目录= webservice_database;集成安全性=真);
    CMD的SqlCommand =新的SqlCommand(UPDATE customer_registration SET PASSWORD = @密码,retypepassword = @retypepassword,性别= @gender,出生= @birth,地址= @address,城市= @city,国家= @country,交code = @后code,电子邮件= @email,卡尔诺(Carno)= @carno其中username ='+会话[用户名] +',CON);
    con.Open();    cmd.Parameters.AddWithValue(@密码,TextBoxPassword.Text);
    cmd.Parameters.AddWithValue(@ retypepassword,TextBoxRPassword.Text);
    cmd.Parameters.AddWithValue(@性别,DropDownListGender.Text);
    cmd.Parameters.AddWithValue(@诞生,DropDownListDay.Text + DropDownListMonth.Text + DropDownListYear.Text);
    cmd.Parameters.AddWithValue(@地址,TextBoxAddress.Text);
    cmd.Parameters.AddWithValue(@城,TextBoxCity.Text);
    cmd.Parameters.AddWithValue(@国,DropDownListCountry.Text);
    cmd.Parameters.AddWithValue(@后code,TextBoxPost code.Text);
    cmd.Parameters.AddWithValue(@电子邮件,TextBoxEmail.Text);
    cmd.Parameters.AddWithValue(@卡尔诺(Carno),TextBoxCarno.Text);    cmd.ExecuteNonQuery();
    con.Close();
    如果(的IsPostBack)
    {
        的Response.Redirect(UpdateSuccess.aspx);
    }
}


解决方案

总结所有的语句!的IsPostBack 状态页面加载。

 保护无效的Page_Load(对象发件人,EventArgs的发送)
{
   如果(!的IsPostBack)
   {
      //所有声明
   }
}

这将解决您的问题。

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 dropdownlist wouldn't update into the database. I tried to remove the code in page load, and re-key in all the data manually, it can be update. All I want is retrieve data from database and display it in the textbox, and it can be update into the database after make some changes from the textbox. Can someone help me on 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");
    }
}

解决方案

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.

这篇关于如何显示从数据库中的数据到文本框,并更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆