You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Dropdownlist5选中值时SQL更新失效,按钮仍执行新增操作问题

问题分析与修复方案

核心问题排查

  1. DropdownList5选中状态判断错误
    DropDownList未选中时,SelectedValue通常返回空字符串("")而非null,原代码中if (DropDownList5.SelectedValue != null)的判断逻辑无法准确识别选中状态,导致本该执行更新的场景错误进入插入分支。

  2. Update语句筛选条件不合理
    原Update用Name作为WHERE筛选条件,但如果更新时修改了DropDownList1的选中值(即Name字段),会导致找不到原记录,更新操作实际未生效。

  3. 必填项检查未终止后续逻辑
    第一个检查必填项的if仅设置提示文本,未终止后续代码执行,存在必填项为空仍执行数据库操作的逻辑漏洞。

  4. 严重SQL注入风险
    直接拼接SQL字符串会导致恶意注入攻击,同时可能因特殊字符引发语法错误。


修复后的完整代码

protected void Button1_Click(object sender, EventArgs e)
{
    // 检查必填项,为空则提示并终止逻辑
    if (string.IsNullOrEmpty(DropDownList1.SelectedValue) || 
        string.IsNullOrEmpty(DropDownList2.SelectedValue) || 
        string.IsNullOrEmpty(TextBox1.Text))
    {
        Label1.Text = "请填写所有必填字段";
        return;
    }

    // 使用using自动释放数据库资源,避免泄漏
    using (SqlConnection con = new SqlConnection("Data Source=ucpdapps2;Initial Catalog=OnCallWeb;Integrated Security=True"))
    {
        con.Open();
        if (!string.IsNullOrEmpty(DropDownList5.SelectedValue))
        {
            // 更新逻辑:改用唯一主键作为筛选条件(替换为你表中的实际主键字段,比如ID)
            string updateSql = @"UPDATE Dispatcher_Roles 
                                SET Name = @Name, Position = @Position, Roles = @Roles, 
                                    Status = @Status, DispatcherCovering = @DispatcherCovering
                                WHERE Id = @TargetId";
            using (SqlCommand comm = new SqlCommand(updateSql, con))
            {
                // 添加参数,杜绝SQL注入
                comm.Parameters.AddWithValue("@Name", DropDownList1.SelectedValue);
                comm.Parameters.AddWithValue("@Position", DropDownList2.SelectedValue);
                comm.Parameters.AddWithValue("@Roles", TextBox1.Text);
                comm.Parameters.AddWithValue("@Status", DropDownList3.SelectedValue);
                comm.Parameters.AddWithValue("@DispatcherCovering", DropDownList4.SelectedValue);
                comm.Parameters.AddWithValue("@TargetId", DropDownList5.SelectedValue);

                int affectedRows = comm.ExecuteNonQuery();
                // 根据影响行数反馈更新结果
                string alertMsg = affectedRows > 0 ? "更新成功" : "未找到对应记录,更新失败";
                ScriptManager.RegisterStartupScript(this, this.GetType(), "script", $"alert('{alertMsg}');", true);
            }
        }
        else
        {
            // 插入逻辑:参数化查询
            string insertSql = @"INSERT INTO Dispatcher_Roles (Name, Position, Roles, Status, DispatcherCovering)
                                VALUES (@Name, @Position, @Roles, @Status, @DispatcherCovering)";
            using (SqlCommand comm = new SqlCommand(insertSql, con))
            {
                comm.Parameters.AddWithValue("@Name", DropDownList1.SelectedValue);
                comm.Parameters.AddWithValue("@Position", DropDownList2.SelectedValue);
                comm.Parameters.AddWithValue("@Roles", TextBox1.Text);
                comm.Parameters.AddWithValue("@Status", DropDownList3.SelectedValue);
                comm.Parameters.AddWithValue("@DispatcherCovering", DropDownList4.SelectedValue);

                comm.ExecuteNonQuery();
                ScriptManager.RegisterStartupScript(this, this.GetType(), "script", "alert('添加成功');", true);
            }
        }
        ClearAllData();
        Label1.Text = "";
    }
}

关键修复说明

  • 修正选中判断逻辑:用!string.IsNullOrEmpty(DropDownList5.SelectedValue)准确识别DropDownList5的选中状态。
  • 优化Update筛选条件:使用表的唯一主键(如Id)替代Name作为WHERE条件,确保能精准定位要更新的记录。
  • 终止错误逻辑:必填项检查通过return终止后续代码,避免无效数据库操作。
  • 参数化查询:彻底消除SQL注入风险,同时避免字符串拼接引发的语法错误。
  • 操作结果反馈:通过ExecuteNonQuery()返回的影响行数,给用户更准确的操作结果提示。
  • 资源自动释放:用using语句管理数据库连接和命令对象,避免资源泄漏。

内容的提问来源于stack exchange,提问作者Walter G

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.18 05:17:31