-2

Please help, I have a problem updating a bit datatype with asp.net webpages syntax. Here's my code

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"); 


        }

The modules suppose to do this : Editing the status of Admin (On duty or not) in bit datatype. The Situation I'm having is Once the admin is updated to the new one, The onDuty which is bit datatype of the old one won't update to 0 (from 1) but the new one successfully updated to 1 (from 0). Sorry for the long code, The problem is simple, but I just want to make sure this problem doesn't come from anywhere else from my code. Hope it's clear enough

Note : There's no error announcement at all! The page still works, only the data in the database is not updated

1 Answers1

1

When the form is posted, the value of idAdmin2 is the default empty string. It is not set to anything else. So only those rows that have no UserId value will be updated in the first UPDATE command (probably NOT what you intend). Assign a value to idAdmin2 in the if(IsPost) section so that it matches the row(s) you want to affect.

Mike Brind
  • 28,238
  • 6
  • 56
  • 88
  • I've edited it according to your suggestion but it still doesn't work `if(IsPost){ idAdmin = Request.Form["idAdmin"]; if (idAdmin != idAdmin2){ var updateCommand1 = "UPDATE UserProfile SET onDuty= 0 WHERE UserId=@0"; db.Execute(updateCommand1, idAdmin2); }` – Aditya Aufar Oct 14 '13 at 15:30
  • You still haven't set a value for admin2. It is still an empty string. – Mike Brind Oct 14 '13 at 18:53
  • But I thought I've assigned it to the row.Id_Admin value in the if(!isPost) condition – Aditya Aufar Oct 15 '13 at 22:44
  • 1
    That code will only execute if the form is NOT posted. So by definition, if the form is posted, the value is not assigned. – Mike Brind Oct 16 '13 at 04:42
  • Well that make sense. So Where Exactly do I suppose to assigned the value at? I'm sorry I'm a bit confuse – Aditya Aufar Oct 16 '13 at 18:11
  • I can't tell you where to assign it. I don't know what business rules you are working to. Your code, on the face of it, makes no sense at all to me. You appear to be adding errors to ModelState even though a form has not been posted. I would only expect to see that in an if(IsPost) section. I think you need to take a good look at the code you have and actually read it and understand what you have written. Then the location of the assignment should become obvious to you. – Mike Brind Oct 16 '13 at 18:43