title: save data to sql server with asp.net using ajax
body: <p>After I entered the data, my ajax function did not run. That makes my code not work in "WebService2.asmx/ThemTaiKhoan" which makes my data unable to save to the database. Can someone help me, please? Thank you so much.Here is my code.  WebService2.asmx</p>
  
  <pre><code>public string ThemTaiKhoan(string username, string password, string fullname, string email, string dienthoai, string status, string admin)
      {
  
          string query = "insert into login values (" + username + ",'" + password + "'," + fullname + ",'" + email + "','" + dienthoai + "'," + status + ",'" + admin + "')";
  
          string CS = ConfigurationManager.ConnectionStrings["Appconnection"].ConnectionString;
          using (SqlConnection con = new SqlConnection(CS))
          {
              SqlCommand cmd = new SqlCommand(query, con);
              con.Open();
              cmd.ExecuteNonQuery();
          }
          return query;
      }
  </code></pre>
  
  <p>Here is JS code on ThemMoi.aspx</p>
  
  <pre><code>&lt;script&gt;
      function ThemTaiKhoan() {
          var username = $("#username").val();
          var password = $("#password").val();
          var fullname = $("#fullname").val();
          var email = $("#email").val();
          var phone = $("#phone").val();
          var status = $("#status").val();
          var admin = $("#admin").val();
  
          $.ajax({
              type: "POST",
              url: "WebService2.asmx/ThemTaiKhoan",
              data: "{username:" + username + ",password:'" + password + "',fullname:" + fullname + ",email:'" + email + "',phone:'" + phone + "',status:" + status + ",admin:'" + admin + "'}",
              contentType: "application/json; charset=utf-8",
              success: function (ms) {
  
                  location.reload();
  
              },
              dataType: "json",
          })
      }
  &lt;/script&gt;
  </code></pre>
  
  <p>Here is HTML code on ThemMoi.aspx</p>
  
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-html lang-html prettyprint-override"><code>&lt;body&gt;
      &lt;form id="form1" runat="server"&gt;
          &lt;div class="container"&gt;
              &lt;h2&gt;Nhập thông tin tài khoản&lt;/h2&gt;
              &lt;div class="form-group"&gt;
                  &lt;label for="username"&gt;Tên đăng nhập:&lt;/label&gt;
                  &lt;input type="text" class="form-control" id="username" placeholder="Username" /&gt;
              &lt;/div&gt;
              &lt;div class="form-group"&gt;
                  &lt;label for="password"&gt;Mật khẩu:&lt;/label&gt;
                  &lt;input type="password" class="form-control" id="password" placeholder="Password" /&gt;
              &lt;/div&gt;
              &lt;div class="form-group"&gt;
                  &lt;label for="fullname"&gt;Tên đầy đủ:&lt;/label&gt;
                  &lt;input type="text" class="form-control" id="fullname" placeholder="Fullname" /&gt;
              &lt;/div&gt;
              &lt;div class="form-group"&gt;
                  &lt;label for="email"&gt;Email:&lt;/label&gt;
                  &lt;input type="text" class="form-control" id="email" placeholder="Email" /&gt;
              &lt;/div&gt;
              &lt;div class="form-group"&gt;
                  &lt;label for="phone"&gt;Điện thoại:&lt;/label&gt;
                  &lt;input type="text" class="form-control" id="phone" placeholder="Phone" /&gt;
              &lt;/div&gt;
              &lt;div class="form-group"&gt;
                  &lt;label for="status"&gt;Trạng thái:&lt;/label&gt;
                  &lt;input type="text" class="form-control" id="status" placeholder="Status" /&gt;
              &lt;/div&gt;
              &lt;div class="form-group"&gt;
                  &lt;label for="admin"&gt;Admin:&lt;/label&gt;
                  &lt;input type="text" class="form-control" id="admin" placeholder="Admin" /&gt;
              &lt;/div&gt;
  
              &lt;div&gt;
                  &lt;input type="submit" value="Thêm" onclick="ThemTaiKhoan()" /&gt;
              &lt;/div&gt;
          &lt;/div&gt;
          &lt;div class="container"&gt;
              &lt;asp:HyperLink ID="HyperLink1" runat="server" NavigateUrl="TrangChu.aspx"&gt;Trang chủ&lt;/asp:HyperLink&gt;
          &lt;/div&gt;
      &lt;/form&gt;
  &lt;/body&gt;</code></pre>
  </div>
  </div>
  </p>
  
  <p>And this is the result when I debug in chrome, the code doesn't work inside the ajax function. What happened? :(
  <a href="https://i.stack.imgur.com/Ae0Lk.png" rel="nofollow noreferrer">enter image description here</a></p>