I am trying to save data using soap Jquery with C# WebMethod But can not save data in SQL Server please help me how can I save data using Soap jquery. I am using IDE Visual Studio 2015.
我试图用C#WebMethod使用SOAP Jquery保存数据但是无法在SQL Server中保存数据请帮我如何使用Soap jquery保存数据。我正在使用IDE Visual Studio 2015。
<script type="text/javascript">
function SavePersonRecord() {
var Name = $.trim($('#<%=txtName.ClientID %>').val());
var LName = $.trim($('#<%=txtlname.ClientID %>').val());
var Company = $.trim($('#<%=txtCompany.ClientID %>').val());
var Messege = "";
if (Name == '') {
Messege = "Can not Blank Name";
}
if (LName == '') {
Messege += "Can not Blank Last Name";
}
if (Company == '') {
Messege += " Company Name Can not Blank";
}
if (Messege.length == 0) {
$.ajax({
type: "POST",
dataType: "json",
contentType: "application/json; charset=utf-8",
url: "Soap-Service.aspx/InsertPersonRecord",
data: "{'FirstName':'" + Name + "', 'LastName':'" + LName + "','Company':'" + Company + "'}",
success: function (Record) {
$('#txtName').val();
$('#txtlName').val();
$('#txtCompany').val();
if (Record.d == true) {
$('#Result').text("Your Record insert");
}
else {
$('#Result').text("Your Record Not Insert");
}
},
Error: function (textMsg) {
$('#Result').text("Error: " + Error);
}
});
}
else {
$('#Result').html('');
$('#Result').html(Messege);
}
$('#Result').fadeIn();
}
</script>
<table border="0" cellpadding="5" cellspacing="5" style="border: solid 2px Green;">
<tr>
<td colspan="2" style="background-color: red; color: white; font-weight: bold; font-size: 12pt; text-align: center; font-family: Verdana;">Enter Employee Information</td>
</tr>
<tr>
<td>First Name:
</td>
<td>
<asp:TextBox ID="txtName" runat="server" Text="" />
</td>
</tr>
<tr>
<td>Last Name:
</td>
<td>
<asp:TextBox ID="txtlname" runat="server" />
</td>
</tr>
<tr>
<td>Company
</td>
<td>
<asp:TextBox ID="txtCompany" runat="server" />
</td>
</tr>
<tr>
<td></td>
<td>
<asp:Button ID="btnInsertRecord" Text="Save" runat="server" OnClientClick="SavePersonRecord(); return false" />
</td>
</tr>
</table>
3 个解决方案
#1
2
Vague question. However, do the following step:
模糊的问题。但是,请执行以下步骤:
- Are you getting any error in browser console which breaks your javascript code?
- 您是否在浏览器控制台中出现任何错误,从而破坏了您的javascript代码?
- Have you captured any error in "Error:" section?
- 您是否在“错误:”部分中捕获了任何错误?
- If above both looks okay, have you put dubugger in your webmethod to check whether it gets hit or not?
- 如果上面两个看起来都没问题,你有没有把dubugger放在你的web方法中来检查它是否被击中?
- After all this,is there in any server side exception (in your webmethod)?
- 在这之后,是否存在任何服务器端异常(在您的webmethod中)?
- Answer of 4) is yes, post the exception
- 回答4)是,发布例外
#2
0
Well as i can see you got problem with your ajax call. It should be:
好吧,我可以看到你的ajax调用有问题。它应该是:
$.ajax({
type: "POST",
dataType: "json",
contentType: "application/json; charset=utf-8",
url: "Soap-Service.aspx/SaveUser", //Here you got wrong method
//right here you should pass object (not string) with right property names (like you have in your web method)
data: { FName: Name, LNAme: LName, Company: Company},
success: function (Record) {
$('#txtName').val();
$('#txtlName').val();
$('#txtCompany').val();
if (Record.d == true) {
$('#Result').text("Your Record insert");
}
else {
$('#Result').text("Your Record Not Insert");
}
},
error: function (textMsg) {
$('#Result').text("Error: " + Error);
}
});
Above your webmethod you should add:
在您的网络方法之上,您应该添加:
[WebInvoke(Method="POST",ResponseFormat=WebMessageFormat.Json)]
#3
0
Finally, My code is running Well here is the Code There is some few Changes in web application
最后,我的代码运行良好这里是代码Web应用程序中有一些变化
<script type="text/javascript" src="http://code.jquery.com/jquery-1.8.2.js"></script>
<script type="text/javascript">
$(function () {
$('#btnSubmit').click(function () {
var Name = $('#txtname').val();
var Contact = $('#txtcontact').val();
//var body = $('#txtbody').val();
if (Name != '' && Contact != '') {
$.ajax({
type: "POST",
contentType: "application/json; charset=utf-8",
url: "Soap-Jquery.aspx/InsertData",
data: "{'EmpName':'" + Name + "','ConNo':'" + Contact + "'}",
dataType: "json",
success: function (data) {
var obj = data.d;
if (obj == 'true') {
$('#txtname').val('');
$('#txtcontact').val('');
//$('#txtbody').val('');
$('#lblmsg').html("Details Submitted Successfully");
window.location.reload();
}
},
//error: function (result) {
// alert("Error");
//}
});
}
else {
alert('Please enter all the fields')
return false;
}
})
});
</script>
<asp:Panel ID="pnl_add_payment" runat="server">
<asp:ScriptManager ID="ScriptManager1" runat="server"></asp:ScriptManager>
<section>
<div class="container">
<div class="row">
<div class="form-group">
<button type="button" class="btn btn-primary ribbon">Data Insert Using Jquery</button>
<asp:Label ID="lblmsg" runat="server" Text="" ForeColor="Red"></asp:Label>
<div class="col-md-12" style="margin-top: 15px;">
<div class="col-md-2">Enter Name</div>
<div class="col-md-4">
<asp:TextBox ID="txtname" runat="server" class="form-control" placeholder="Enter Name"></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ControlToValidate="txtname" ErrorMessage="*" ForeColor="Red" ValidationGroup="a"></asp:RequiredFieldValidator>
</div>
<div class="col-md-2">Enter Contact No</div>
<div class="col-md-4">
<asp:TextBox ID="txtcontact" runat="server" class="form-control" placeholder="Enter Contact No"></asp:TextBox>
<asp:RegularExpressionValidator ID="RegularExpressionValidator1" ControlToValidate="txtcontact" runat="server" ErrorMessage="Only Numbers allowed" ValidationExpression="\d+" ForeColor="Red" ValidationGroup="a"></asp:RegularExpressionValidator>
<asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server" ControlToValidate="txtcontact" ErrorMessage="*" ForeColor="Red" ValidationGroup="a"></asp:RequiredFieldValidator>
</div>
<div class="col-md-6">
<asp:Button ID="btnSubmit" runat="server" Text="Submit" value="Submit" ValidationGroup="a" />
</div>
</div>
</div>
<%--End Form Group--%>
</div>
<%--End Row--%>
</div>
<%--End Container--%>
</section>
<br /><br />
<section>
<div class="container">
<div class="row">
<div class="col-md-6">
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" BackColor="White" BorderColor="#CC9966" BorderStyle="None" BorderWidth="1px" CellPadding="4" Width="100%">
<Columns>
<asp:BoundField DataField="EmpName" HeaderText="Name" />
<asp:BoundField DataField="ConNo" HeaderText="Number" />
</Columns>
<FooterStyle BackColor="#FFFFCC" ForeColor="#330099" />
<HeaderStyle BackColor="#990000" Font-Bold="True" ForeColor="#FFFFCC" />
<PagerStyle BackColor="#FFFFCC" ForeColor="#330099" HorizontalAlign="Center" />
<RowStyle BackColor="White" ForeColor="#330099" />
<SelectedRowStyle BackColor="#FFCC66" Font-Bold="True" ForeColor="#663399" />
<SortedAscendingCellStyle BackColor="#FEFCEB" />
<SortedAscendingHeaderStyle BackColor="#AF0101" />
<SortedDescendingCellStyle BackColor="#F6F0C0" />
<SortedDescendingHeaderStyle BackColor="#7E0000" />
</asp:GridView>
</div>
</div>
</div>
</section>
</asp:Panel>
[WebMethod] public static string InsertData(string EmpName, string ConNo) { string msg = string.Empty; using (SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["ConStr"].ConnectionString)) { using (SqlCommand cmd = new SqlCommand("sp_insertEmp", con)) { con.Open(); cmd.CommandType = CommandType.StoredProcedure; cmd.Parameters.AddWithValue("@EmpName", EmpName); cmd.Parameters.AddWithValue("@ConNo", ConNo); int i = cmd.ExecuteNonQuery(); con.Close(); if (i == 1) {
[WebMethod] public static string InsertData(string EmpName,string ConNo){string msg = string.Empty; using(SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings [“ConStr”]。ConnectionString)){using(SqlCommand cmd = new SqlCommand(“sp_insertEmp”,con)){con.Open(); cmd.CommandType = CommandType.StoredProcedure; cmd.Parameters.AddWithValue(“@ EmpName”,EmpName); cmd.Parameters.AddWithValue(“@ ConNo”,ConNo); int i = cmd.ExecuteNonQuery(); con.Close(); if(i == 1){
msg = "True";
}
else
{
msg = "false";
}
}
}
return msg;
}
#1
2
Vague question. However, do the following step:
模糊的问题。但是,请执行以下步骤:
- Are you getting any error in browser console which breaks your javascript code?
- 您是否在浏览器控制台中出现任何错误,从而破坏了您的javascript代码?
- Have you captured any error in "Error:" section?
- 您是否在“错误:”部分中捕获了任何错误?
- If above both looks okay, have you put dubugger in your webmethod to check whether it gets hit or not?
- 如果上面两个看起来都没问题,你有没有把dubugger放在你的web方法中来检查它是否被击中?
- After all this,is there in any server side exception (in your webmethod)?
- 在这之后,是否存在任何服务器端异常(在您的webmethod中)?
- Answer of 4) is yes, post the exception
- 回答4)是,发布例外
#2
0
Well as i can see you got problem with your ajax call. It should be:
好吧,我可以看到你的ajax调用有问题。它应该是:
$.ajax({
type: "POST",
dataType: "json",
contentType: "application/json; charset=utf-8",
url: "Soap-Service.aspx/SaveUser", //Here you got wrong method
//right here you should pass object (not string) with right property names (like you have in your web method)
data: { FName: Name, LNAme: LName, Company: Company},
success: function (Record) {
$('#txtName').val();
$('#txtlName').val();
$('#txtCompany').val();
if (Record.d == true) {
$('#Result').text("Your Record insert");
}
else {
$('#Result').text("Your Record Not Insert");
}
},
error: function (textMsg) {
$('#Result').text("Error: " + Error);
}
});
Above your webmethod you should add:
在您的网络方法之上,您应该添加:
[WebInvoke(Method="POST",ResponseFormat=WebMessageFormat.Json)]
#3
0
Finally, My code is running Well here is the Code There is some few Changes in web application
最后,我的代码运行良好这里是代码Web应用程序中有一些变化
<script type="text/javascript" src="http://code.jquery.com/jquery-1.8.2.js"></script>
<script type="text/javascript">
$(function () {
$('#btnSubmit').click(function () {
var Name = $('#txtname').val();
var Contact = $('#txtcontact').val();
//var body = $('#txtbody').val();
if (Name != '' && Contact != '') {
$.ajax({
type: "POST",
contentType: "application/json; charset=utf-8",
url: "Soap-Jquery.aspx/InsertData",
data: "{'EmpName':'" + Name + "','ConNo':'" + Contact + "'}",
dataType: "json",
success: function (data) {
var obj = data.d;
if (obj == 'true') {
$('#txtname').val('');
$('#txtcontact').val('');
//$('#txtbody').val('');
$('#lblmsg').html("Details Submitted Successfully");
window.location.reload();
}
},
//error: function (result) {
// alert("Error");
//}
});
}
else {
alert('Please enter all the fields')
return false;
}
})
});
</script>
<asp:Panel ID="pnl_add_payment" runat="server">
<asp:ScriptManager ID="ScriptManager1" runat="server"></asp:ScriptManager>
<section>
<div class="container">
<div class="row">
<div class="form-group">
<button type="button" class="btn btn-primary ribbon">Data Insert Using Jquery</button>
<asp:Label ID="lblmsg" runat="server" Text="" ForeColor="Red"></asp:Label>
<div class="col-md-12" style="margin-top: 15px;">
<div class="col-md-2">Enter Name</div>
<div class="col-md-4">
<asp:TextBox ID="txtname" runat="server" class="form-control" placeholder="Enter Name"></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ControlToValidate="txtname" ErrorMessage="*" ForeColor="Red" ValidationGroup="a"></asp:RequiredFieldValidator>
</div>
<div class="col-md-2">Enter Contact No</div>
<div class="col-md-4">
<asp:TextBox ID="txtcontact" runat="server" class="form-control" placeholder="Enter Contact No"></asp:TextBox>
<asp:RegularExpressionValidator ID="RegularExpressionValidator1" ControlToValidate="txtcontact" runat="server" ErrorMessage="Only Numbers allowed" ValidationExpression="\d+" ForeColor="Red" ValidationGroup="a"></asp:RegularExpressionValidator>
<asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server" ControlToValidate="txtcontact" ErrorMessage="*" ForeColor="Red" ValidationGroup="a"></asp:RequiredFieldValidator>
</div>
<div class="col-md-6">
<asp:Button ID="btnSubmit" runat="server" Text="Submit" value="Submit" ValidationGroup="a" />
</div>
</div>
</div>
<%--End Form Group--%>
</div>
<%--End Row--%>
</div>
<%--End Container--%>
</section>
<br /><br />
<section>
<div class="container">
<div class="row">
<div class="col-md-6">
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" BackColor="White" BorderColor="#CC9966" BorderStyle="None" BorderWidth="1px" CellPadding="4" Width="100%">
<Columns>
<asp:BoundField DataField="EmpName" HeaderText="Name" />
<asp:BoundField DataField="ConNo" HeaderText="Number" />
</Columns>
<FooterStyle BackColor="#FFFFCC" ForeColor="#330099" />
<HeaderStyle BackColor="#990000" Font-Bold="True" ForeColor="#FFFFCC" />
<PagerStyle BackColor="#FFFFCC" ForeColor="#330099" HorizontalAlign="Center" />
<RowStyle BackColor="White" ForeColor="#330099" />
<SelectedRowStyle BackColor="#FFCC66" Font-Bold="True" ForeColor="#663399" />
<SortedAscendingCellStyle BackColor="#FEFCEB" />
<SortedAscendingHeaderStyle BackColor="#AF0101" />
<SortedDescendingCellStyle BackColor="#F6F0C0" />
<SortedDescendingHeaderStyle BackColor="#7E0000" />
</asp:GridView>
</div>
</div>
</div>
</section>
</asp:Panel>
[WebMethod] public static string InsertData(string EmpName, string ConNo) { string msg = string.Empty; using (SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["ConStr"].ConnectionString)) { using (SqlCommand cmd = new SqlCommand("sp_insertEmp", con)) { con.Open(); cmd.CommandType = CommandType.StoredProcedure; cmd.Parameters.AddWithValue("@EmpName", EmpName); cmd.Parameters.AddWithValue("@ConNo", ConNo); int i = cmd.ExecuteNonQuery(); con.Close(); if (i == 1) {
[WebMethod] public static string InsertData(string EmpName,string ConNo){string msg = string.Empty; using(SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings [“ConStr”]。ConnectionString)){using(SqlCommand cmd = new SqlCommand(“sp_insertEmp”,con)){con.Open(); cmd.CommandType = CommandType.StoredProcedure; cmd.Parameters.AddWithValue(“@ EmpName”,EmpName); cmd.Parameters.AddWithValue(“@ ConNo”,ConNo); int i = cmd.ExecuteNonQuery(); con.Close(); if(i == 1){
msg = "True";
}
else
{
msg = "false";
}
}
}
return msg;
}