gpt4 book ai didi

c# - 如何将数据库中的数据显示到文本框中,并更新它

转载 作者:太空狗 更新时间:2023-10-30 00:54:04 25 4
gpt4 key购买 nike

从 sql 数据库检索数据后,我可以在我的文本框和下拉列表中显示我的数据,但是当我继续我的更新按钮时,在文本框或下拉列表中编辑的信息不会更新到数据库中。我试图删除页面加载中的代码,并手动重新键入所有数据,它可以更新。我想要的只是从数据库中检索数据并将其显示在文本框中,并且可以在对文本框进行一些更改后将其更新到数据库中。有人可以帮我吗?谢谢。

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");
}
}

最佳答案

在页面加载时将所有语句包装在 !IsPostBack 条件中。

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

这将解决您的问题。

关于c# - 如何将数据库中的数据显示到文本框中,并更新它,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14113025/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com