You have to do something like this
Java Script:
$(document).ready(function () {
$('#btnsubmit').click(function () {
$.ajax({
type: 'POST',
contentType: "application/json; charset=utf-8",
url: 'Test.aspx/AddRecord',
data: {'name': $('#txtname').val()},
async: false,
success: function (response) {
alert("Record saved successfully in database");
},
error: function () {
alert("some problem in saving data");
}
});
});
});
}
});
HTML
Test.aspx
<html>
<body>
<form id="form1" runat="server">
<div>
<table>
<tr>
<td>
Name
</td>
<td>
<asp:TextBox ID="txtname" runat="server"></asp:TextBox>
</td>
<td>
</td>
</tr>
<tr>
<td>
</td>
<td>
<input type="button" id="btnsubmit" value="Submit" />
</td>
<td>
</td>
</tr>
</table>
</div>
</form>
</body>
</html>
Code Behind:
Test.aspx.cs
public static string AddRecord(string name)
{
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["connection"].ToString());
try
{
SqlCommand cmd = new SqlCommand("Insert into Test values("+name+")", con);
con.Open();
cmd.ExecuteNonQuery();
con.Close();
return "Success";
}
catch (Exception ex)
{
return "failure";
}
}