title: Insert data into database using ajax
body: <p>I am using ajax to insert data in database</p>
  
  <p>My default .aspx file is as below</p>
  
  <pre><code>&lt;%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %&gt;
  
  &lt;!DOCTYPE html&gt;
  
  &lt;html xmlns="http://www.w3.org/1999/xhtml"&gt;
  &lt;head runat="server"&gt;
      &lt;title&gt;Ajax Demo&lt;/title&gt;
  
      &lt;script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js"&gt;&lt;/script&gt;
  
      &lt;script type="text/javascript"&gt;
          $(document).ready(function () {
  
              $("#&lt;%= btn_Insert.ClientID %&gt;").click(function () {
  
                  //var name = $('#&lt;%=txt_name.ClientID%&gt;').val();
                  //var email=$('#&lt;%=txt_email.ClientID%&gt;').val()
  
                  var email = document.getElementById('txt_email').value;
                  var name = document.getElementById('txt_name').value;
  
                  //alert("{'Name':'" + name + "', 'Email':'" + email + "'}");
                  $.ajax({
  
                      type: 'POST',
  
                      contentType: "application/x-www-form-urlencoded; charset=utf-8",
  
                      data: "Name=" + name + "&amp;Email=" + email,
  
                     //data: $('#form1')
  
                      async: true,
  
                      success: function (response, data)
                      {
                          $('#txt_name').val('');
  
                          $('#txt_email').val('');
  
                          alert("Record Has been Saved in Database");
  
                          alert(response.data.name);
                      },
  
                      error: function () {
                          console.log('there is some error');
                      }
  
                  });
                  return false;
              });
          });
   &lt;/script&gt;
  &lt;/head&gt;
  &lt;body&gt;
      &lt;form id="form1" runat="server"&gt;
      &lt;div&gt;
          &lt;p&gt;
              Name : &lt;asp:TextBox ID="txt_name" runat="server" ClientIDMode="Static"&gt;&lt;/asp:TextBox&gt;
          &lt;/p&gt;
  
          &lt;p&gt;
              E-mail : &lt;asp:TextBox ID="txt_email" runat="server" ClientIDMode="Static"&gt;&lt;/asp:TextBox&gt;
          &lt;/p&gt;
  
          &lt;p&gt;
              &lt;asp:Button ID="btn_Insert" runat="server" Text="INSERT" OnClick="btn_Insert_Click"/&gt;
          &lt;/p&gt;
      &lt;/div&gt;
      &lt;/form&gt;
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>and my .cs file is as below</p>
  
  <pre><code>using System;
  using System.Collections.Generic;
  using System.Linq;
  using System.Web;
  using System.Web.Services;
  using System.Web.UI;
  using System.Web.UI.WebControls;
  using System.Data.SqlClient;
  using System.Configuration;
  using System.Data;
  using System.IO;
  
  public partial class _Default : System.Web.UI.Page
  {
      protected void Page_Load(object sender, EventArgs e)
      {
          string Name = Request["Name"];
          string Email = Request["Email"];
  
          InsertMethod(Name, Email);
      }
  
      SqlCommand cmd;
  
      public Boolean InsertMethod(string Name, string Email)
      {
         SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["connectionstring"].ConnectionString);
  
          cmd = new SqlCommand("insert into dbo.Demo_AJAX (Name,Email)values (@name,@email)", conn);
          cmd.CommandType =CommandType.Text;
          cmd.Parameters.AddWithValue("@name",Name);
          cmd.Parameters.AddWithValue("@email", Email);
          try
          {
              conn.Open();
              //cmd.ExecuteNonQuery();
              int affected = cmd.ExecuteNonQuery();
  
              if (affected == 1)
              {
                  //Response.Write("Bhargav");
                  return true;
              }
              else
              {
                  return false;
              }
          }
          catch (Exception ex)
          {
              //Response.Write(ex.Message);
              return false;
          }
          finally
          {
              conn.Close();
          }
      }
  
      protected void btn_Insert_Click(object sender, EventArgs e)
      {
      }
  }
  </code></pre>
  
  <p>When I run then it insert one blank data in database.</p>
  
  <p>I don't know how to solve it. </p>
  
  <p>Please any one help me. </p>