title: Using same submit to Add and Edit to database values in ASP.NET and C#
body: <p>I am using a same submit button to Add new row and also edit a row's values of a database table in and ASP.NET form. I use <code>CommandArgument</code> to change the method being applied.</p>
  
  <pre><code>protected void btnSave_Click(object sender, EventArgs e)
      {
         if (!CheckProfileComplete())
          {
              btnSave.CommandArgument = "new";
              btnSave.Text = "Save";
          }
          else
          {
              btnSave.CommandArgument = "update";
              btnSave.Text = "Update";
          }
      }
  protected void btnSave_Click(object sender, EventArgs e)
      {
          if (!Page.IsValid)
              return;
          string strCmd = this.btnSave.CommandArgument;
          if (strCmd.ToLower() == "new")
          {
               //Add new Record
          }
          else if(strCmd.ToLower() == "update")
          {
               //Update existing record.
          }
       }
  </code></pre>
  
  <p>In case of <code>Add New</code> a blank form is presented, while in <code>Update</code> a form with previously added data to the database table is presented, where the user can edit the values and then save it. The <code>CommandArgument</code> is updated at page load, after checking some other parameters.</p>
  
  <p>The <code>Add New</code> method runs fine, but I am unable to edit the values in case of <code>Update</code>. I mean the form posts the same values that were dynamically filled before presenting to the user for editing. No change is being shown after the Save (Update mode) button is clicked. It seems like the form takes its previous state as the current state even after values being changed and posted back.</p>
  
  <p>for example if <code>tbName.Text</code> had value <code>foo</code> when read from database initially.</p>
  
  <p>user edited it, now <code>tbName.Text</code> has <code>bar</code> before postback.</p>
  
  <p>But when the form is posted while in debug mode, the <code>tbName.Text</code> shows <code>foo</code> as the current value in place of <code>bar</code></p>