【问题标题】:Unable to update Bit datatype sql server webpages无法更新位数据类型 sql server 网页
【发布时间】:2013-10-14 12:59:45
【问题描述】:

请帮忙,我在使用 asp.net 网页语法更新位数据类型时遇到问题。这是我的代码

var db = Database.Open("StarterSite");
var idAdmin = ""; // Here's the ID of the new Admin
var idAdmin2 = ""; // Here's The ID of the old Admin




if(!IsPost){
    if(!Request.QueryString["id"].IsEmpty() && Request.QueryString["id"].IsInt()) {
        diklatid = Request.QueryString["id"];
        var dbCommand = "SELECT * FROM diklat  WHERE ID_diklat = @0";
        var row = db.QuerySingle(dbCommand, diklatid);

        if(row != null) { 
            string rowidadmin = row.ID_Admin.ToString();  


         idAdmin2 = rowidadmin;  // Inserted the ID value of old admin to this variable 

   }
        else{
            Validation.AddFormError("No data choosen");

        }
    }
    else{
        Validation.AddFormError("No data choosen");

    }
}

if(IsPost){
   idAdmin = Request.Form["idAdmin"]; //The ID value of new admin comes from the Form

    if (idAdmin == idAdmin2){ 

   //Check if the new admin equals to old admin
   -------------------//do something------
        Response.Redirect("~/SuperAdmin/diklat"); 
        } 
        else
         {
        //-----------------------------The Main Problem------------------------  

       // If different it suppose to change "onDuty" variable of the new one to 1 (true) and the old one to 0 (false)

        var updateCommand1 = "UPDATE UserProfile  SET onDuty= 0 WHERE UserId=@0";

        db.Execute(updateCommand1, idAdmin2);

        var updateCommand2 = "UPDATE UserProfile  SET onDuty= 1 WHERE UserId=@0";
        db.Execute(updateCommand2, idAdmin);
        Response.Redirect("~/SuperAdmin/diklat"); 


        }

模块假设这样做:以位数据类型编辑管理员的状态(值班与否)。我遇到的情况是,一旦管理员更新为新管理员,作为旧的位数据类型的 onDuty 不会更新为 0(从 1),但新的成功更新为 1(从 0)。抱歉,代码很长,问题很简单,但我只想确保这个问题不是来自我的代码中的其他任何地方。希望够清楚

注意:根本没有错误公告!页面还可以,只是数据库中的数据没有更新

【问题讨论】:

  • -1 用于发布这么多代码。不能缩小问题范围,只贴相关代码吗?
  • 好的,我已经编辑过了,希望能缩小范围,朋友@JohnSaunders

标签: sql-server asp.net-webpages


【解决方案1】:

提交表单时,idAdmin2 的值是默认的空字符串。它没有设置为其他任何东西。因此,只有那些没有 UserId 值的行才会在第一个 UPDATE 命令中更新(可能不是您想要的)。在if(IsPost) 部分中为idAdmin2 分配一个值,使其与您想要影响的行匹配。

【讨论】:

  • 我按照你的建议修改了,还是不行if(IsPost){ idAdmin = Request.Form["idAdmin"]; if (idAdmin != idAdmin2){ var updateCommand1 = "UPDATE UserProfile SET onDuty= 0 WHERE UserId=@0"; db.Execute(updateCommand1, idAdmin2); }
  • 您还没有为 admin2 设置值。它仍然是一个空字符串。
  • 但我以为我已经将它分配给 if(!isPost) 条件中的 row.Id_Admin 值
  • 只有在未发布表单时才会执行该代码。所以根据定义,如果表单被发布,则不会分配值。
  • 那是有道理的。那么我到底应该在哪里分配价值呢?对不起,我有点困惑
猜你喜欢
  • 2011-09-01
  • 2021-01-29
  • 1970-01-01
  • 2012-04-10
  • 1970-01-01
  • 2013-09-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多