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();
}
}
私が間違っていることと、それを正しくする方法は? これが前に尋ねられたら申し訳ありませんが、答えが見つかりませんでした。ポストバックに何かがあると思いますよね?
さらに情報が必要な場合は、必要なものを書いてください。ありがとう(コマンド文字列のいくつかの単語を編集します)