title: Call asp.net function from javascript function
body: <p>I want to call asp.net function from a javascript function passing string value.</p>
  
  <p>this is the asp.net function :</p>
  
  <pre><code>[System.Web.Services.WebMethod]     
  public static string InsertData(string ID)
  {
      string source = "Data Source=(LocalDB)\v11.0;Integrated Security=True;Connect Timeout=30";
      using(SqlConnection con = new SqlConnection(source))
      {
          using (SqlCommand cmd = new SqlCommand("Insert into Book (Name) values(@Name)", con))       {
              con.Open();
              cmd.Parameters.AddWithValue("@Name", ID);
              cmd.ExecuteNonQuery();
              return "True";
          }
      }
  }
  </code></pre>
  
  <p>So i want to call this function from javascript function which passes the string value "ID" to the the asp.net function.</p>
  
  <p>this is the javascript function i use</p>
  
  <pre><code> function CallMethod() {
              PageMethods.InsertData("hello", CallSuccess, CallError);
  
          }
  
          function CallSuccess(res) {
              alert(res);
          }
  
          function CallError() {
              alert('Errorrr');
          }
  </code></pre>
  
  <p>and i call it from here </p>
  
  <pre><code>&lt;body&gt;
          &lt;header&gt;        
          &lt;/header&gt;       
          &lt;div class="table"  id="div1" &gt; &lt;/div&gt;                      
          &lt;form id="Form1" runat="server"&gt;
              &lt;asp:Button id="b1" Text="Submit" runat="server" onclientclick="CallMethod();return false;"/&gt;
              &lt;asp:ScriptManager enablepagemethods="true" id="ScriptManager1" runat="server"&gt;&lt;/asp:ScriptManager&gt;
          &lt;/form&gt; 
  
      &lt;/body&gt;
  </code></pre>
  
  <p>so i have a button and onClick i want to add "Hello" Row to my table but nothing happens and CallError function calls</p>