数据库更新仅在某些时候有效
可能的重复:
数据未写入数据库
我正在尝试更新中的位字段我的数据库关于复选框的 checkchanged 事件。当它被选中时,它发送一个 1。当它未被选中时,它发送一个 0。现在我不确定为什么,但这些更改只在某些时候被保存。这与“!IsPostBack”有什么关系吗?
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
using (SqlConnection connection = new SqlConnection(connectionString.ToString()))
{
connection.Open();
dataAdapter = new SqlDataAdapter("SELECT * FROM SecureOrders", connection);
dataSet = new DataSet();
dataAdapter.Fill(dataSet, "SecureOrders");
DataView source = new DataView(dataSet.Tables[0]);
DefaultGrid.DataSource = source;
DefaultGrid.DataBind();
connection.Close();
}
}
}
protected void CheckBoxProcess_CheckedChanged(object sender, EventArgs e)
{
bool update;
string checkedString = "UPDATE SecureOrders SET processed = 1 WHERE fName LIKE '%" + DefaultGrid.SelectedRow.Cells[2].Text + "%' AND lName LIKE '% " + DefaultGrid.SelectedRow.Cells[3].Text + "%'";
string uncheckedString = "UPDATE SecureOrders SET processed = 0 WHERE fName LIKE '%" + DefaultGrid.SelectedRow.Cells[2].Text + "%' AND lName LIKE '% " + DefaultGrid.SelectedRow.Cells[3].Text + "%'";
CheckBox cb = (CheckBox)sender;
GridViewRow gvr = (GridViewRow)cb.Parent.Parent;
DefaultGrid.SelectedIndex = gvr.RowIndex;
update = Convert.ToBoolean(DefaultGrid.SelectedValue);
orderByString = orderByList.SelectedItem.Value;
fieldString = searchTextBox.Text;
connectionString = rootWebConfig.ConnectionStrings.ConnectionStrings["secureodb"];
using (SqlConnection connection = new SqlConnection(connectionString.ToString()))
{
connection.Open();
SqlCommand checkedCmd = new SqlCommand(checkedString, connection);
SqlCommand uncheckedCmd = new SqlCommand(uncheckedString, connection);
if (cb.Checked == true)
{
checkedCmd.ExecuteNonQuery();
}
else
{
uncheckedCmd.ExecuteNonQuery();
}
connection.Close();
}
Possible Duplicate:
Data not writing out to Database
I'm trying to update a bit field in my database on a checkbox's checkedchanged event. When it gets checked, it sends a 1. When it is unchecked, it sends a 0. Now I'm not sure why, but these changes only get saved SOME of the time. Would this have anything to do with the "!IsPostBack" ?
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
using (SqlConnection connection = new SqlConnection(connectionString.ToString()))
{
connection.Open();
dataAdapter = new SqlDataAdapter("SELECT * FROM SecureOrders", connection);
dataSet = new DataSet();
dataAdapter.Fill(dataSet, "SecureOrders");
DataView source = new DataView(dataSet.Tables[0]);
DefaultGrid.DataSource = source;
DefaultGrid.DataBind();
connection.Close();
}
}
}
protected void CheckBoxProcess_CheckedChanged(object sender, EventArgs e)
{
bool update;
string checkedString = "UPDATE SecureOrders SET processed = 1 WHERE fName LIKE '%" + DefaultGrid.SelectedRow.Cells[2].Text + "%' AND lName LIKE '% " + DefaultGrid.SelectedRow.Cells[3].Text + "%'";
string uncheckedString = "UPDATE SecureOrders SET processed = 0 WHERE fName LIKE '%" + DefaultGrid.SelectedRow.Cells[2].Text + "%' AND lName LIKE '% " + DefaultGrid.SelectedRow.Cells[3].Text + "%'";
CheckBox cb = (CheckBox)sender;
GridViewRow gvr = (GridViewRow)cb.Parent.Parent;
DefaultGrid.SelectedIndex = gvr.RowIndex;
update = Convert.ToBoolean(DefaultGrid.SelectedValue);
orderByString = orderByList.SelectedItem.Value;
fieldString = searchTextBox.Text;
connectionString = rootWebConfig.ConnectionStrings.ConnectionStrings["secureodb"];
using (SqlConnection connection = new SqlConnection(connectionString.ToString()))
{
connection.Open();
SqlCommand checkedCmd = new SqlCommand(checkedString, connection);
SqlCommand uncheckedCmd = new SqlCommand(uncheckedString, connection);
if (cb.Checked == true)
{
checkedCmd.ExecuteNonQuery();
}
else
{
uncheckedCmd.ExecuteNonQuery();
}
connection.Close();
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我建议在 DataView 上将“EnableViewState”设置为 false,然后将代码从
if (!IsPostBack)
部分移至页面的 Pre_Init 事件。我认为这会解决您的问题,并且可能还会帮助您的回发更快。I would recommend setting "EnableViewState" to false on your DataView, and then moving the code from the
if (!IsPostBack)
section to your page's Pre_Init event. I think that will solve your problem and possibly also help your postbacks go faster.无论我是否喜欢该模式, !IsPostBack 都不是您的问题。然而,回发事件处理程序是一团糟。
您可以通过使用各种测试用例逐步执行代码来确定哪里出了问题。最终你会遇到触发你所遇到的问题的那个。
更好的选择是将 SQL 更新代码分离到其自己的例程中并发送参数。这将减少移动部件的数量。主事件处理程序中唯一应该做的事情(这可以争论)是从网格中获取变量。
如果是我,我也会考虑使用更新语句的键值,以免数据库中有两个 James Smith,现在都已处理。
至于候选人为什么有时会更新而不是其他的?很可能由于某种原因您最终选择了错误的行。由于我没有您的代码副本来查看所有渗透,因此我只能猜测。
The !IsPostBack is not your issue, whether or not I like the pattern. The postback event handler is a mess, however.
You can determine where things are going wrong by stepping through the code with a wide variety of test cases. Eventually you will hit the one that triggers the issue you have.
A better option would be to separate out the SQL update code into its own routine and send the parameters in. This will reduce the number of moving parts. The only thing that should be in the main event handler (and this can be argued) is the grabbing of the variables from the Grid.
If it were me, I would also consider using the key value for my update statement, lest you have two James Smith's in the database, both which are now processed.
As for candidates why it updates some times and not others? Could very well be that you are ending up with the wrong selected row for some reason. As I don't have a copy of your code to see all the permeatations, I can only guess.