title: How to call c# function from js function inside asp.net?
body: <p>I am developing my first website in vs using asp.net and c#.
  How can I call c# function from js function inside asp.net? I want to insert form data into sql database.</p>
  
  <p><strong>C# (add_project.aspx.cs):</strong></p>
  
  <pre><code>using System.Web.Services;
  public partial class add_project : System.Web.UI.Page
  {
  [WebMethod]    
  public static void InsertIntoDatabase(string projectname, string piname)
      {
          Console.WriteLine("hello");// this doesn't appear which mean it doesn't enter this function
          SqlConnection conn = new SqlConnection("Data Source=servername;Initial Catalog=databasename;Integrated Security=SSPI");
          string sql = "INSERT INTO Projects (project_name, principal_investigator) VALUES (@project_name,@pi_name)";
  
          try
          {
              conn.Open();
              SqlCommand cmd = new SqlCommand(sql, conn);
              SqlParameter[] param = new SqlParameter[2];
              param[0] = new SqlParameter("@project_name", System.Data.SqlDbType.VarChar, 50);
              param[1] = new SqlParameter("@principal_investigator", System.Data.SqlDbType.VarChar, 50);
          param[0].Value = projectname;
              param[1].Value = piname;
              for (int i = 0; i &lt; param.Length; i++)
              {
                  cmd.Parameters.Add(param[i]);
              }
  
              cmd.ExecuteNonQuery();
          }
          catch (System.Data.SqlClient.SqlException ex)
          {
              string msg = "Insert Error:";
              msg += ex.Message;
              throw new Exception(msg);
          }
          finally
          {
              conn.Close();
          }
  
      }
  }
  </code></pre>
  
  <p><strong>js in the aspx file (add_project.aspx):</strong></p>
  
  <pre><code>&lt;script&gt;
    function doSomething() {
  
        var $projectname = $("#project_name").val();
        var $piname = $("#pi_name").val();
        alert($piname);    // This alert appear
        $.ajax({
            type: "POST",
            url: 'add_project.aspx/InsertIntoDatabase',
            data: "{'projectname':'" + $projectname + "', 'piname':'" + $piname + "' }", //Pass the parameter names and values
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: function (msg) {
                alert('success');
            },
            error: function (e) {
                alert('error');       // this msg appear
            }
        });
           }
  </code></pre>
  
  <p></p>
  
  <p><strong>form in the aspx file (add_project.aspx):</strong></p>
  
  <pre><code>    &lt;form class="form-horizontal form-label-left" id="add_project_form" runat="server" onsubmit="doSomething();"&gt;
      &lt;div id="wizard" class="form_wizard wizard_horizontal"&gt;
  &lt;!-- Project Name --&gt;
                            &lt;div class="form-group"&gt;
                              &lt;label class="control-label col-md-3 col-sm-3 col-xs-12" for="project_name"&gt;Project Name &lt;span class="required"&gt;*&lt;/span&gt;
                              &lt;/label&gt;
                              &lt;div class="col-md-6 col-sm-6 col-xs-12"&gt;
                                &lt;asp:TextBox id="project_name" required="required" class="form-control col-md-7 col-xs-12" runat="server"&gt;&lt;/asp:TextBox&gt;
                              &lt;/div&gt;
                            &lt;/div&gt;
  &lt;!-- PI name --&gt;
                            &lt;div class="form-group"&gt;
                              &lt;label class="control-label col-md-3 col-sm-3 col-xs-12" for="pi_name"&gt;PI Name &lt;span class="required"&gt;*&lt;/span&gt;
                              &lt;/label&gt;
                              &lt;div class="col-md-6 col-sm-6 col-xs-12"&gt;
                                &lt;asp:TextBox id="pi_name" required="required" class="form-control col-md-7 col-xs-12" runat="server"&gt;&lt;/asp:TextBox&gt;
                              &lt;/div&gt;
                            &lt;/div&gt;
      &lt;/div&gt;
  &lt;/form&gt;
  </code></pre>
  
  <p>I don't have access to the submit button of the form (because I use bootstrap template) .</p>
  
  <p>Thanks</p>
  
  <p><strong>Solution:</strong>
  I solve the problem by put [WebMethod] before the c# function
  Also, I were have error syntax in the insert statement.
  Thanks @Pawan and all for the help.</p>