first you need to create webmethod in webservice like below
[WebMethod]
public string Inserttblval(int CountryId, string UserName, int IsActive)
{
int rowsInserted;
SqlConnection conn = new SqlConnection(your connectionstring);
string sql = string.Format(@"INSERT INTO [TestDB].[dbo].[Table_1] ([UserName],[CountryId],[IsActive]) VALUES ({0},'{1}',{2})", UserName, CountryId, IsActive);
SqlCommand cmd = new SqlCommand();
cmd.Connection = conn;
cmd.CommandText = sql;
cmd.CommandType = CommandType.Text;
conn.Open();
rowsInserted = cmd.ExecuteNonQuery();
conn.Close();
cmd = null;
return string.Format("Thank you, {0} number of rows inserted!", rowsInserted);
}
next in aspx page add below code
<head runat="server">
<title></title>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.1/jquery-ui.min.js"></script>
<script type="text/javascript">
$(function () {
$("[id*=Button1]").click(function () {
var obj = {};
obj.countryid= $.trim($("[id*=TextBox2]").val());
obj.username= $.trim($("[id*=TextBox1]").val());
obj.isactive= $.trim($("[id*=TextBox3]").val());
$.ajax({
type: "POST",
url: "Default.aspx/GetValues",
data: JSON.stringify(obj),
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (r) {
alert(r.d);
document.getElementById('Label1').value = r.d;
}
});
return false;
});
});
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<table align="center" class="auto-style1">
<tr>
<td>UserName</td>
<td>
<asp:TextBox ID="TextBox1" runat="server">
</td>
<td> </td>
</tr>
<tr>
<td>CountryId </td>
<td>
<asp:TextBox ID="TextBox2" runat="server">
</td>
<td> </td>
</tr>
<tr>
<td class="auto-style2">IsActive</td>
<td class="auto-style2">
<asp:TextBox ID="TextBox3" runat="server">
</td>
<td class="auto-style2"></td>
</tr>
<tr>
<td> </td>
<td>
<asp:Button ID="Button1" runat="server" Text="Button" />
</td>
<td> </td>
</tr>
<tr>
<td>Result:</td>
<td>
<asp:Label ID="Label1" runat="server" Text="">
</td>
<td> </td>
</tr>
</table>
</div>
</form>
</body>
add below code in CS Page
using System.Web.Services;
public partial class Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
[WebMethod]
public static string GetValues(int countryid,string username,int isactive)
{
WebService ws = new WebService();
return ws.Inserttblval(countryid, username, isactive);
}
}
accept solution if its working,