My problem is that I have a jqury function basically I want to add gridview row into data base .so that pupose I am doing that by accessing webmethod ,but the web method can’t call from that code .
My data can’t save into data base I will check it.value should be passed but problem is that this function can’t call webmethod I will check it doing debudg.
Please help me to solve that problem my function should be call webmethod
Following my code
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js" type="text/javascript"> </script>
<script type="text/javascript">
$(function () {
$("[id*=btnSubmit]").bind("click", function () {
$('[id*=GridView1]').find('tr:has(td)').each(function () {
var Customer = {};
Customer.Id = $(this).find("td:nth-child(2)").html();
Customer.Name = $(this).find("td:nth-child(3)").html();
Customer.Country = $(this).find("td:nth-child(4)").html();
$.ajax({
type: 'POST',
url: "WebForm1.aspx/SaveGridViewRowData",
// data: JSON.stringify(Customer),
// data: '{product: ' + JSON.stringify(product) + '}',
data: '{Customers: ' + JSON.stringify(Customer) + '}',
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (response) {
alert("User has been added successfully.");
//window.location.reload();
}
});
});
return false;
});
});
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<table border="0" cellpadding="0" cellspacing="0">
<tr>
<td>
Id
</td>
<td>
<asp:TextBox ID="txtId" runat="server" />
</td>
</tr>
<tr>
<td>
Name
</td>
<td>
<asp:TextBox ID="txtName" runat="server" />
</td>
</tr>
<tr>
<td>
Country
</td>
<td>
<asp:TextBox ID="txtCountry" runat="server" />
</td>
</tr>
</table>
<br />
<%-- <asp:Button ID="btnAddRow" Text="Add Row" runat="server" />--%> <%-- <asp:Button ID="btnUpdate" Text="Update Row" runat="server" />--%>
<input id="btnAddRow" type="button" value="Add Row" onclick="return btnAddRow_onclick()" />
<input id="btnUpdate" type="button" value="Update Row" />
<br />
<br />
<asp:GridView ID="GridView1" HeaderStyle-BackColor="#3AC0F2" HeaderStyle-ForeColor="White"
runat="server" AutoGenerateColumns="false"
onrowdatabound="GridView1_RowDataBound1">
<Columns>
<asp:BoundField HeaderText="RowIndex" ItemStyle-CssClass="RowIndex" ItemStyle-Width="30" />
<asp:BoundField DataField="Id" ItemStyle-CssClass="Id" HeaderText="Id" ItemStyle-Width="30" />
<asp:BoundField DataField="Name" ItemStyle-CssClass="Name" HeaderText=" Name" ItemStyle-Width="150" />
<asp:BoundField DataField="Country" ItemStyle-CssClass="Country" HeaderText="Country"
ItemStyle-Width="150" />
<asp:TemplateField>
<ItemTemplate>
<input id="Button1" type="button" value="-" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<asp:HiddenField ID="hdclick" runat="server" />
</div>
<asp:Button ID="btnSubmit" runat="server" Height="31px" Text="Submitt"
Width="77px" />
</form>
Webmethod in C#
public class customers
{
public string Id { get; set; }
public string Name { get; set; }
public string Country { get; set; }
}
[System.Web.Services.WebMethod]
public static string SaveGridViewRowData(customers Customer)
{
string str1 = "Data Source=.\\SQLEXPRESS;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|Database1.mdf;User Instance=true";
SqlConnection conn = new SqlConnection(str1);
try
{
using (SqlCommand cmd = new SqlCommand("INSERT INTO tbl_customer VALUES(@id, @name,@country)"))
{
//SqlCommand cmd1 = new SqlCommand("spinsertcustomer", conn);
//cmd1.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@id", Customer.Id);
cmd.Parameters.AddWithValue("@name", Customer.Name);
cmd.Parameters.AddWithValue("@country", Customer.Country);
////com.CommandType = CommandType.StoredProcedure;
////SqlCommand cmd1 = new SqlCommand()", conn);
conn.Open();
cmd.ExecuteNonQuery();
conn.Close();
return "Data Inserted";
}
}
catch (Exception ex)
{
return "Data Insert Failed";
}
}
please help me that i can call the webmethod