1

我在 Page_Load 上有一个页面,我用从数据库中获取的数据填充了一些文本框和下拉列表,我得到了正确的数据,但是当我单击按钮保存新数据时,它采用旧数据而不是新数据进行更新.

我把我的代码:Page_load:

string id = Request.QueryString["id"];

        OleDbConnection conn = new OleDbConnection(@"Provider = Microsoft.Jet.OLEDB.4.0; Data Source =" + Server.MapPath("path to DB"));
        OleDbCommand comm = new OleDbCommand("select * from table where ID=" + id, conn);
        OleDbDataReader reader;

        conn.Open();
        reader = comm.ExecuteReader();
        reader.Read();


        ddlType.SelectedIndex = (Convert.ToInt32(reader["PersonType"]) - 1); // -1 to fix position, list is zero based. type 1(0) = PP type 2(1) = DC

        txtFirstName.Text = reader["FirstName"].ToString();
        txtLastName.Text = reader["LastName"].ToString();
        txtAddress.Text = reader["Address"].ToString();
        txtCountry.Text = reader["Country"].ToString();
        txtPhone.Text = reader["Phone"].ToString();
        txtFax.Text = reader["Fax"].ToString();
        txtClinic.Text = reader["Clinic"].ToString();
        txtReferredBy.Text = reader["ReferingFactor"].ToString();
        txtWebSite.Text = reader["WebSite"].ToString();
        txtReceiptNumber.Text = reader["Receipt"].ToString();
        txtDeviceType.Text = reader["DeviceType"].ToString();
        txtDeviceSerialNumber.Text = reader["DeviceSerialNumber"].ToString();
        txtPaymentType.Text = reader["PaymentType"].ToString();
        txtDevicePrice.Text = reader["DevicePrice"].ToString();
        txtClientUserName.Text = reader["PersonUserName"].ToString();
        txtClientPassword.Text = reader["PersonPassword"].ToString();
        txtComments.Text = reader["Comment"].ToString();

保存按钮:

string personType = ddlType.SelectedValue;
        string firstName = txtFirstName.Text;
        string lastName = txtLastName.Text;
        string address = txtAddress.Text;
        string country = txtCountry.Text;
        string phone = txtPhone.Text;
        string fax = txtFax.Text;
        string clinic = txtClinic.Text;
        string referredBy = txtReferredBy.Text;
        string website = txtWebSite.Text;
        string receiptNumber = txtReceiptNumber.Text;
        string deviceType = txtDeviceType.Text;
        string deviceSerialNumber = txtDeviceSerialNumber.Text;
        string paymentType = txtPaymentType.Text;
        string devicePrice = txtDevicePrice.Text;
        string clientUserName = txtClientUserName.Text;
        string clientPassword = txtClientPassword.Text;
        string comments = txtComments.Text;



        string id = Request.QueryString["id"];

        OleDbConnection conn = new OleDbConnection(@"Provider = Microsoft.Jet.OLEDB.4.0; Data Source =" + Server.MapPath("path to DB"));
        string str = "update table set PersonType = ?, FirstName = ?, LastName = ?, Address = ?, Country = ?, Phone = ?, Fax = ?, Clinic = ?, ReferingFactor = ?, WebSite = ?, Receipt = ?, DeviceType = ?, DeviceSerialNumber = ?, PaymentType = ?, DevicePrice = ?,PersonUserName = ?, PersonPassword = ?, Comment = ? where ID = ?";
        using (conn)
        {
            using (OleDbCommand cmd = new OleDbCommand(str, conn))
            {
                cmd.CommandType = CommandType.Text;
                cmd.Parameters.AddWithValue("PersonType", personType);
                cmd.Parameters.AddWithValue("FirstName", firstName);
                cmd.Parameters.AddWithValue("LastName", lastName);
                cmd.Parameters.AddWithValue("Address", address);
                cmd.Parameters.AddWithValue("Country", country);
                cmd.Parameters.AddWithValue("Phone", phone);
                cmd.Parameters.AddWithValue("Fax", fax);
                cmd.Parameters.AddWithValue("Clinic", clinic);
                cmd.Parameters.AddWithValue("ReferingFactor", referredBy);
                cmd.Parameters.AddWithValue("WebSite", website);
                cmd.Parameters.AddWithValue("Receipt", receiptNumber);
                cmd.Parameters.AddWithValue("DeviceType", deviceType);
                cmd.Parameters.AddWithValue("DeviceSerialNumber", deviceSerialNumber);
                cmd.Parameters.AddWithValue("PaymentType", paymentType);
                cmd.Parameters.AddWithValue("DevicePrice", devicePrice);
                cmd.Parameters.AddWithValue("PersonUserName", clientUserName);
                cmd.Parameters.AddWithValue("PersonPassword", clientPassword);
                cmd.Parameters.AddWithValue("Comment", comments);
                cmd.Parameters.AddWithValue("PersonID", id);

                conn.Open();
                cmd.ExecuteNonQuery();
                conn.Close();
            }
        }

我做错了什么以及如何使它正确?抱歉,如果之前问过这个问题,但我没有找到答案。我认为它与回发有关,对吗?

如果您需要更多信息,请写信给我。谢谢(我编辑了命令字符串上的一些单词)

4

3 回答 3

2

您必须使用Page.IsPostback属性,例如

protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
             // Put your code inside this.
             string id = Request.QueryString["id"];
             OleDbConnection conn = new OleDbConnection(@"Provider = Microsoft.Jet.OLEDB.4.0; Data Source =" + Server.MapPath("path to DB"));
    OleDbCommand comm = new OleDbCommand("select * from table where ID=" + id, conn);
    OleDbDataReader reader;
    conn.Open();
    reader = comm.ExecuteReader();
    reader.Read();
    ddlType.SelectedIndex = (Convert.ToInt32(reader["PersonType"]) - 1); // -1 to fix position, list is zero based. type 1(0) = PP type 2(1) = DC

    txtFirstName.Text = reader["FirstName"].ToString();
    txtLastName.Text = reader["LastName"].ToString();
    txtAddress.Text = reader["Address"].ToString();
    txtCountry.Text = reader["Country"].ToString();
    txtPhone.Text = reader["Phone"].ToString();
    txtFax.Text = reader["Fax"].ToString();
    txtClinic.Text = reader["Clinic"].ToString();
    txtReferredBy.Text = reader["ReferingFactor"].ToString();
    txtWebSite.Text = reader["WebSite"].ToString();
    txtReceiptNumber.Text = reader["Receipt"].ToString();
    txtDeviceType.Text = reader["DeviceType"].ToString();
    txtDeviceSerialNumber.Text = reader["DeviceSerialNumber"].ToString();
    txtPaymentType.Text = reader["PaymentType"].ToString();
    txtDevicePrice.Text = reader["DevicePrice"].ToString();
    txtClientUserName.Text = reader["PersonUserName"].ToString();
    txtClientPassword.Text = reader["PersonPassword"].ToString();
    txtComments.Text = reader["Comment"].ToString();
            }

        }

希望它有效..

于 2013-04-29T06:25:14.323 回答
1

这是因为您将旧值传递给数据库。

在更新查询中传递文本框中的新值。

样本:

OleDbConnection conn = new OleDbConnection(@"Provider = Microsoft.Jet.OLEDB.4.0; Data Source =" + Server.MapPath("path to DB"));
        string str = "update table set PersonType = ?, FirstName = ?, LastName = ?, Address = ?, Country = ?, Phone = ?, Fax = ?, Clinic = ?, ReferingFactor = ?, WebSite = ?, Receipt = ?, DeviceType = ?, DeviceSerialNumber = ?, PaymentType = ?, DevicePrice = ?,PersonUserName = ?, PersonPassword = ?, Comment = ? where ID = ?";
        using (conn)
        {
            using (OleDbCommand cmd = new OleDbCommand(str, conn))
            {
                cmd.CommandType = CommandType.Text;

                cmd.Parameters.AddWithValue("FirstName", txtFirstName.Text);
                cmd.Parameters.AddWithValue("LastName", lastName);
                .
                .
                .
            }
            conn.Open();
            cmd.ExecuteNonQuery();
            conn.Close();
于 2013-04-29T06:25:38.690 回答
1

当页面第一次加载时,你应该将数据加载到控件

if (!IsPostBack)
            {
txtFirstName.Text = reader["FirstName"].ToString();
        txtLastName.Text = reader["LastName"].ToString();
        txtAddress.Text = reader["Address"].ToString();
        txtCountry.Text = reader["Country"].ToString();
        txtPhone.Text = reader["Phone"].ToString();
        txtFax.Text = reader["Fax"].ToString();
        txtClinic.Text = reader["Clinic"].ToString();
        txtReferredBy.Text = reader["ReferingFactor"].ToString();
        txtWebSite.Text = reader["WebSite"].ToString();
        txtReceiptNumber.Text = reader["Receipt"].ToString();
        txtDeviceType.Text = reader["DeviceType"].ToString();
        txtDeviceSerialNumber.Text = reader["DeviceSerialNumber"].ToString();
        txtPaymentType.Text = reader["PaymentType"].ToString();
        txtDevicePrice.Text = reader["DevicePrice"].ToString();
        txtClientUserName.Text = reader["PersonUserName"].ToString();
        txtClientPassword.Text = reader["PersonPassword"].ToString();
        txtComments.Text = reader["Comment"].ToString();
    }
于 2013-04-29T06:26:12.083 回答