数据库更新仅在某些时候有效

发布于 2024-11-19 23:06:59 字数 2277 浏览 2 评论 0原文

可能的重复:
数据未写入数据库

我正在尝试更新中的位字段我的数据库关于复选框的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

锦上情书 2024-11-26 23:07:00

我建议在 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.

放手` 2024-11-26 23:07:00

无论我是否喜欢该模式, !IsPostBack 都不是您的问题。然而,回发事件处理程序是一团糟。

  1. 通过连接字符串而不是使用参数,您已经留下了潜在的 SQL 注入漏洞。即使您必须编写存储过程并传入逗号分隔的列表,您也比现有的代码更好。
  2. 无论值设置为 1 还是 0,您的决策点都是 1,但您在决定运行两个字符串中的哪一个的点上分支代码(始终会创建一个字符串,消耗周期,但永远不会运行)。
  3. 您正在创建两个命令对象,其中之一永远不会被使用。

您可以通过使用各种测试用例逐步执行代码来确定哪里出了问题。最终你会遇到触发你所遇到的问题的那个。

更好的选择是将 SQL 更新代码分离到其自己的例程中并发送参数。这将减少移动部件的数量。主事件处理程序中唯一应该做的事情(这可以争论)是从网格中获取变量。

如果是我,我也会考虑使用更新语句的键值,以免数据库中有两个 James Smith,现在都已处理。

至于候选人为什么有时会更新而不是其他的?很可能由于某种原因您最终选择了错误的行。由于我没有您的代码副本来查看所有渗透,因此我只能猜测。

The !IsPostBack is not your issue, whether or not I like the pattern. The postback event handler is a mess, however.

  1. You have left open a potential SQL injection hole by concatenating strings rather than using parameters. Even if you have to write a sproc and pass in a comma separted list, you are better off than the code you have.
  2. Your decision point is one whether or not a value is set to 1 or 0, yet you branch code on the point of deciding which of two strings to run (one will always be created, consuming cycles, yet NEVER run).
  3. You are creating two command objects, one of which will NEVER be used.

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文