title: I need to save record in asp.net using Ajax. I think saveData() is not execute in ajax
body: <p>I want to save record in asp.net c# using Ajax. I submit my two files named <code>addquestion.aspx</code> and <code>addquestion.aspx.cs</code>. There was no compilation error in code but record is not inserted in database.</p>
  
  <p><strong>addquestion.aspx</strong></p>
  
  <pre><code>    $(document).ready(function()
      {
          $("#submit").click(function()
          {
              alert("step1");
              var mcq = $("#question").val();
              var ans1 = $("#optiona").val();
              var ans2 = $("#optionb").val();
              var ans3 = $("#optionc").val();
              var ans4 = $("#optiond").val();
              var rightans = $("#rightans").val();
  
              $.ajax({
                  type : 'POST',
                  contentType: "application/json; charset=utf-8",
                  url : 'addquestion.aspx/saveData',
                  data :  "{'mcq':'" + mcq + "','ans1':'" + ans1 + "','ans2':'" + ans2 + "','ans3':'" + ans3 + "','ans4':'" + ans4 + "','rightans':'" + rightans + "'}",
                  success: function (response) {
                  alert(response);
                 }
  </code></pre>
  
  <p><strong>addquestion.aspx.cs</strong></p>
  
  <p>[System.Web.Services.WebMethod]</p>
  
  <pre><code>public static string saveData(string mcq, string ans1,string ans2, string ans3, string ans4, string rightans)
  {
     String connectionstring = ConfigurationManager.ConnectionStrings["constring"].ToString();
      SqlConnection con = new SqlConnection(connectionstring);
      con.Open();
      String query = "select count(*) as totalrecord from mcq";
      int count = 0;
      SqlCommand cmd = new SqlCommand(query, con);
      SqlDataReader dr = cmd.ExecuteReader();
      dr.Read();
      if ((int)dr["totalrecord"] == 0)
          count = 1;
      else
          count = (int)dr["totalrecord"] + 1;
      dr.Close();
      query = "insert into mcq values (" + count + ",'" + mcq + "','" + ans1 + "'," + ans2 + "'," + ans3 + "','" + ans4 + "','" + rightans + "')";
      cmd = new SqlCommand(query, con);
      dr = cmd.ExecuteReader();
      return "true";
  }        
           });
       });
     });
  </code></pre>