I am new to jquery. I often do my coding in c# and Now i have to work with jquery. I Know basic of jquery. Just few functions of jquery. But now I want to do some advance thing . I want to save update delete the data in sql server from my asp.net page using jquery. I tried a jquery code for this but it is not working. I am not able to get what is the error and where i am going wrong. Here is my code of jquery which i am using
<script type="text/javascript">
$(document).ready(function () {
$("#Button1").click(function () {
$.ajax({
type: 'POST',
contentType: "application/json;charset=utf-8",
url: 'Default2.aspx/InsertMethod',
data: "{'username':'" + document.getElementById('txtusername').value + "','password':'" + document.getElementById("txtpassword").value + "'}",
async: false,
success: function (response) {
$('#txtusername').val('');
$('#txtpassword').val('');
alert("record has been saved in database");
},
error: function () {
console.log('there is some error');
}
});
});
});
I create a web method in c# code where i write the code for save data in sql server and calling that web method in jquery. But this code is not working Here is my C# code
[WebMethod]
public static string InsertMethod(string username, string password)
{
SqlConnection con = new SqlConnection();
con.ConnectionString = ConfigurationManager.ConnectionStrings["con"].ConnectionString;
con.Open();
SqlCommand cmd = new SqlCommand("insert jquerydata values('" + username + "','" + password + "'", con);
cmd.CommandType = CommandType.Text;
cmd.ExecuteNonQuery();
cmd.Dispose();
con.Close();
return "true";
}
Please tell me how can I do it.