更新访问数据库

时间:2013-04-29 06:20:42

标签: c# asp.net database ms-access

我有页面在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();
            }
        }

我做错了什么以及我是如何做对的? 对不起,如果此问,但我没有找到答案。 我认为它与回发有关,我是对的吗?

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

3 个答案:

答案 0 :(得分: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();
            }

        }

希望它有效..

答案 1 :(得分: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();

答案 2 :(得分: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();
    }