2013-10-14 31 views
-2

請幫助,我有一個問題,用asp.net網頁語法更新位數據類型。這裏是我的代碼無法更新位數據類型sql服務器的網頁

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 [email protected]"; 

     db.Execute(updateCommand1, idAdmin2); 

     var updateCommand2 = "UPDATE UserProfile SET onDuty= 1 WHERE [email protected]"; 
     db.Execute(updateCommand2, idAdmin); 
     Response.Redirect("~/SuperAdmin/diklat"); 


     } 

模塊想做到這一點:編輯聯繫的bit數據類型的狀態(值班與否)。我遇到的情況是一旦管理員更新爲新的,舊數據類型的onDuty不會更新爲0(從1開始),但新的成功更新爲1(從0開始)。對不起,長代碼,問題很簡單,但我只是想確保這個問題不是來自我的代碼中的任何其他地方。希望它足夠清楚

注意:根本沒有錯誤公告!該頁仍然有效,只有數據庫中的數據未更新

+0

-1發佈這麼多的代碼。你不能縮小你的問題,只發布相關的代碼? –

+0

好吧,我已經編輯它,希望它縮小它PAL @JohnSaunders –

回答

1

當表單發佈時,idAdmin2的值是默認的空字符串。這不是別的。所以只有那些沒有UserId值的行纔會在第一個UPDATE命令中被更新(可能不是你想要的)。在if(IsPost)部分中爲idAdmin2指定一個值,以便它與要影響的行匹配。

+0

我已經按照你的建議修改,但它仍然無法正常工作 '如果(IsPost){ idAdmin =的Request.Form [ 「idAdmin」]; if(idAdmin!= idAdmin2){var updateCommand1 =「UPDATE UserProfile SET onDuty = 0 WHERE UserId = @ 0」; db.Execute(updateCommand1,idAdmin2); }' –

+0

您仍然沒有爲admin2設置一個值。它仍然是一個空字符串。 –

+0

但我想我已經將它分配給if(!isPost)條件中的row.Id_Admin值 –