TextBox控件中更改的数据未在表单提交代码中更新。 [英] The changed data from TextBox controls is not getting updated in the form submit code.
问题描述
更新表单数据用于更新数据库中的记录。单击窗体上的btnUpdate后,事件处理程序以调试/跟踪模式显示旧数据,因此旧数据将被存储,而新更改的数据则不会。
帮助程序类DBUtility位于单独的C#文件和namesapce,已正确导入。
帮助程序类:
The Update form data is used to update the record in data base. Upon clicking the 'btnUpdate' on form, the event handler shows the old data in debug/trace mode, and therefore old data gets stored and new changed data does not.
The helper class DBUtility is in a separate C# file and namesapce, which is properly imported.
Helper Class:
public class DBUtility {
public static Int32 ExecuteNonQuery(string cmdName, CommandType cmdType, params SqlParameter[] pars) {
using (SqlConnection conn = new SqlConnection(connString)) {
using (SqlCommand cmd = new SqlCommand(cmdName, conn)) {
cmd.CommandType = cmdType;
cmd.CommandText = cmdName;
if (pars.Length > 0)
cmd.Parameters.AddRange(pars);
conn.Open();
return cmd.ExecuteNonQuery();
}
}
}
}
问题代码:btnUpdate_Click方法
Problem Code: the btnUpdate_Click method
public partial class InsertStudent : System.Web.UI.Page {
private int studId;
protected void Page_Load(object sender, EventArgs e) {
studId = Int32.Parse(Request.Params.Get("id"));
DataTable table = DBUtility.ExecuteSelectTable("SELECT * FROM stud_master WHERE StudId = @ID", CommandType.Text, new SqlParameter[] { new SqlParameter("@ID", studId) });
txtID.Text = table.Rows[0].ItemArray[0].ToString();
txtFName.Text = table.Rows[0].ItemArray[1].ToString();
txtLName.Text = table.Rows[0].ItemArray[2].ToString();
txtEmail.Text = table.Rows[0].ItemArray[3].ToString();
txtCity.Text = table.Rows[0].ItemArray[4].ToString();
}
protected void btnUpdate_Click(object sender, EventArgs e) {
string updateCommand = "UPDATE stud_master SET firstName = @Fname, lastName = @Lname, emailId = @Email, city = @City WHERE StudID = @ID";
SqlParameter[] parameters = new SqlParameter[] {
new SqlParameter("@ID", txtID.Text),
new SqlParameter("@Fname", txtFName.Text),
new SqlParameter("@Lname", txtLName.Text),
new SqlParameter("@Email", txtEmail.Text),
new SqlParameter("@City", txtCity.Text)
};
DBUtility dbUtility = new DBUtility();
if (DBUtility.ExecuteNonQuery(updateCommand, CommandType.Text, parameters) > 0) {
lblStatus.Text = "Record Successfully Updated.";
}
else {
lblStatus.Text = "Cannot add new Record. Operation failed.";
}
}
}
当使用Insert表单中的类似代码时,它会被插入。
类似代码没有问题
When similar code in the Insert form is used it gets inserted.
Similar Code with no problem
public partial class InsertStudent : System.Web.UI.Page {
protected void btnInsert_Click(object sender, EventArgs e) {
string insertCommand = "INSERT INTO stud_master VALUES(@ID, @Fname, @Lname, @Email, @City)";
SqlParameter[] parameters = new SqlParameter[] {
new SqlParameter("@ID", txtID.Text),
new SqlParameter("@Fname", txtFName.Text),
new SqlParameter("@Lname", txtLName.Text),
new SqlParameter("@Email", txtEmail.Text),
new SqlParameter("@City", txtCity.Text)
};
Int32 rowsAffected = DBUtility.ExecuteNonQuery(insertCommand, CommandType.Text, parameters);
if (rowsAffected > 0) {
lblStatus.Text = "Record Successfully added.";
}
else {
lblStatus.Text = "Cannot add new Record. Operation failed.";
}
}
}
推荐答案
问题是,因为我发现页面加载事件在btnUpdate事件之前触发。在前者我放置代码来初始化我的文本字段WITHOUT CHECKING如果它是回发!当应用该检查时问题已解决。
感谢您的帮助和关注。
The problem was, as I discovered that the page load event was firing before the btnUpdate event. In the former I placed the code to initialize my text fields WITHOUT CHECKING if it is a postback! When that check was applied the problem resolved.
Thank you both for help and concern.
这篇关于TextBox控件中更改的数据未在表单提交代码中更新。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!