title: How to increment a value within a database column by button click
body: <p>As part of the project that I'm working on, users can create a post and then other users are able to click on a "like" or "dislike" button.</p>
  
  <p>The code below is the Post.cs class responsible for adding the tables to the database.</p>
  
  <pre><code>public class Post
  {
      //The post ID
      [Key]
      [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
      public int postId { get; set; }
      // Foreign key to customer
  
      public string Id { get; set; }
      public string Email { get; set; }
      public string postTitle { get; set; }
      public string postBody { get; set; }
      public string postDepartment { get; set; }
      public string postCategory { get; set; }
      public bool postAnonymous { get; set; }
      public int postLikes { get; set; }
      public int postDislikes { get; set; }
      public DateTime postDate { get; set; }
  }
  </code></pre>
  
  <p>The following code is the back-end C# code that's linked to either buttons.</p>
  
  <pre><code>protected void btnLike_Click(object sender, EventArgs e)
  {
  
  }
  
  protected void btnDislike_Click(object sender, EventArgs e)
  {
  
  }
  </code></pre>
  
  <p>I am trying to get the buttons to increase the integer value by +1 on the database per each click and the users should only be able to click on either one without being able to like // dislike more than once.</p>
  
  <p>How would I go about trying to do this successfully using the asp.net webforms.</p>
  
  <p>&lt;------------------EDIT-------------------------></p>
  
  <pre><code>protected void btnLike_Click(object sender, EventArgs e)
  {
      var postIDforLike = // add logic to get the id of the post to increment the likes
      using (var _dbContext = new ApplicationDbContext()) 
      {
          var addLikeSql = "update post set postLikes = postLikes + 1 where postID = @id";
          var paramID = new SqlParameter("@id", postIDforLike);
          _dbContext.Database.ExecuteSqlCommand(addLikeSql, paramID);
      }
  }
  </code></pre>
  
  <p>&lt;--------------------------EDIT2-----------------------></p>
  
  <pre><code>&lt;asp:Button ID="btnLike" class="btn btn-primary" runat="server" Text=" Like" Width="99.99px" OnClick="btnLike_Click" /&gt;&amp;nbsp &lt;asp:Button ID="btnDislike" Width="99.99px" class="btn btn-primary" runat="server" Text="Dislike " OnClick="btnDislike_Click"  /&gt;
  
          &lt;/div&gt;
      &lt;br /&gt;
           &lt;%--------------------------------------
            Inserting Comment Information
            --------------------------------------%&gt;
   &lt;div class="col-md-12"&gt;
              &lt;div class="panel panel-primary"&gt;
                    &lt;div class="panel-heading"&gt;
      &lt;h3 class="panel-title text-center"&gt;Add Comment&lt;/h3&gt;
    &lt;/div&gt;
  
    &lt;div class="panel-body"&gt;
  
  
        &lt;fieldset&gt;
        &lt;table class="nav-justified"&gt;
            &lt;tr&gt;
                &lt;td class="modal-sm" style="width: 237px; height: 21px;"&gt;
        &lt;label for="commentBody" class="col-lg-2 control-label"&gt;Comment:&lt;/label&gt;&lt;/td&gt;
                &lt;td style="width: 434px; height: 21px;"&gt;    
           &lt;asp:TextBox Width="400px" style="resize:none;" class="form-control" ID="commentBody" runat="server" placeholder="Body" TextMode="MultiLine"&gt;&lt;/asp:TextBox&gt;
                &lt;/td&gt;
                &lt;td style="height: 21px"&gt;    
                    &lt;asp:RequiredFieldValidator controltovalidate="commentBody" ID="commentBodyValidator" runat="server" ErrorMessage="*Comment is required" ForeColor="Red"&gt;&lt;/asp:RequiredFieldValidator&gt;    
                &lt;/td&gt;
            &lt;/tr&gt;
            &lt;/table&gt;
        &lt;br /&gt;
        &lt;table class="nav-justified"&gt;
            &lt;tr&gt;
                &lt;td style="height: 21px; width: 511px"&gt;    
        &lt;label for="commentAnonymous" class="col-lg-2 control-label" style="left: 0px; top: 0px; width: 538px"&gt;Would you like this comment to be submitted anonymously?:&lt;/label&gt;&lt;/td&gt;
                &lt;td style="height: 21px; width: 104px"&gt;    
        &lt;asp:RadioButtonList ID="commentAnonymous" runat="server" BorderStyle="None" CellPadding="0" CellSpacing="0"&gt;
          &lt;asp:ListItem Value="1" Text="Yes"&gt;Yes&lt;/asp:ListItem&gt;
          &lt;asp:ListItem Value="0" Text="No"&gt;No&lt;/asp:ListItem&gt;
      &lt;/asp:RadioButtonList&gt;  
                &lt;/td&gt;
                &lt;td style="height: 21px"&gt;&lt;asp:RequiredFieldValidator controltovalidate="commentAnonymous" ID="commentAnonymousValidator" runat="server" ErrorMessage="*Please select an option" ForeColor="Red"&gt;&lt;/asp:RequiredFieldValidator&gt;    
                &lt;/td&gt;
            &lt;/tr&gt;
        &lt;/table&gt;
        &lt;br /&gt;
        &lt;table class="nav-justified"&gt;
            &lt;tr&gt;
                &lt;td class="modal-sm" style="width: 408px"&gt;&amp;nbsp;&lt;/td&gt;
                &lt;td&gt;
          &lt;button type="reset" class="btn btn-default"&gt;Cancel&lt;/button&gt;
            &lt;asp:Button  class="btn btn-default" ID="commentSubmitBtn" runat="server" autopostback="false" onclick="AddComment" Text="Submit" /&gt;
                &lt;/td&gt;
                &lt;td&gt;&amp;nbsp;&lt;/td&gt;
            &lt;/tr&gt;
        &lt;/table&gt;
            &lt;/fieldset&gt;
                        &lt;hr&gt;
  
        &lt;table class="display" id="commentsTable"&gt;
          &lt;thead&gt;
              &lt;tr&gt;
                  &lt;th&gt;Comment&lt;/th&gt;
                  &lt;th&gt;User&lt;/th&gt;
                  &lt;th&gt;Date&lt;/th&gt;
  
              &lt;/tr&gt;
          &lt;/thead&gt;
          &lt;tbody&gt;
  
          &lt;/tbody&gt;
      &lt;/table&gt;
  </code></pre>