當我在表單中插入細節並單擊插入按鈕時,它顯示「用戶已成功添加」。但數據沒有插入到表格中。我也嘗試通過按F12來檢查瀏覽器中的錯誤,它顯示對象con爲空。如何解決這個問題?在此先感謝Ajax函數執行但數據未插入到數據庫中
C#Web方法:
[WebMethod]
public static void InsertData(ConsigneeMast Consignee)
{
using(var scon = new SqlConnection(strConnection))
{
using(var cmd = new SqlCommand())
{
cmd.CommandText = "AddInConsigneeTable";
cmd.CommandType = CommandType.StoredProcedure;
cmd.Connection = scon;
cmd.Parameters.AddWithValue("@Code",Consignee.Code);
cmd.Parameters.AddWithValue("@Name",Consignee.Name);
cmd.Parameters.AddWithValue("@Address1",Consignee.Address1);
cmd.Parameters.AddWithValue("@Address2",Consignee.Address2);
cmd.Parameters.AddWithValue("@Address3",Consignee.Address3);
cmd.Parameters.AddWithValue("@City",Consignee.City);
cmd.Parameters.AddWithValue("@PinCode",Consignee.Pincode);
cmd.Parameters.AddWithValue("@Country",Consignee.Country);
cmd.Parameters.AddWithValue("@TelNo1",Consignee.TelNo1);
cmd.Parameters.AddWithValue("@TelNo2",Consignee.TelNo2);
cmd.Parameters.AddWithValue("@TelNo3",Consignee.TelNo3);
cmd.Parameters.AddWithValue("@EmailID",Consignee.EmailID);
cmd.Parameters.AddWithValue("@ContactPerson",Consignee.ContactPerson);
cmd.Parameters.AddWithValue("@Remark",Consignee.Remark);
}
}
}
阿賈克斯,jQuery代碼:
$(document).ready(function() {
$("#btnInsertInConsignee").click(function() {
var con = {};
debugger;
con.Name = $("#ConsigneeName").val();
con.Address1 = $("#RegOfficeAddress1").val();
con.Address2 = $("#RegOfficeAddress2").val();
con.Address3 = $("#RegOfficeAddress3").val();
con.City = $("#City").val();
con.Country = $("#CountryAddress").val();
con.Pincode = $("#PinCode").val();
con.TelNo1 = $("#TelephoneNo").val();
con.TelNo2 = $("#FaxNo").val();
con.TelNo3 = $("#MobileNo").val();
con.ContactPerson = $("#ContactPerson").val();
con.EmailID = $("#Email").val();
con.Remark = $("#Remark").val();
$.ajax({
type: "POST",
url: "ConsigneeWithBoot.aspx/InsertData",
data: '{Consignee: ' + JSON.stringify(con) + '}',
dataType: "json",
contentType: "application/json; charset=utf-8",
success: function() {
debugger;
alert("User has been added successfully.");
debugger;
//window.location.reload();
},
error: function() {
debugger;
alert("Error while inserting data");
}
});
return false;
});
});
SQL Server存儲過程:
ALTER PROCEDURE [dbo].[AddInConsigneeTable]
@Code nvarchar(6) = '',
@Name nvarchar(75),
@Address1 nvarchar(75),
@Address2 nvarchar(75),
@Address3 nvarchar(75),
@City nvarchar(15),
@PinCode nvarchar(10),
@Country nvarchar(40),
@TelNo1 nvarchar(50),
@TelNo2 nvarchar(50),
@TelNo3 nvarchar(50),
@EmailID nvarchar(250),
@ContactPerson nvarchar(75),
@Remark nvarchar(max)
as begin
declare @Totalcount int
declare @Count nvarchar(10)
select @Totalcount = (select COUNT(Code) from ConsigneeMast)
if @Totalcount is null
set @Count = 'A' + CONVERT(nvarchar(10), 1001)
else
set @Count = 'A' + CONVERT(nvarchar(10), 1001 + (@Totalcount))
insert into ConsigneeMast ([Code], [Name], [Address1], [Address2],[Address3], [City], [Country], [Pincode], [TelNo1], [TelNo2], [TelNo3],[ContactPerson], [EmailID], [Remark])
values (upper(@Count), upper(@Name), upper(@Address1), upper(@Address2), upper(@Address3), upper(@City), upper(@Country), upper(@PinCode), upper(@TelNo1), upper(@TelNo2), upper(@TelNo3), upper(@ContactPerson), upper(@EmailID), upper(@Remark))
end
從你發佈的'InsertData'函數永遠不會調用ExecuteNonQuery命令。由於該函數執行時沒有錯誤,因此AJAX調用會接收到成功。 –
謝謝..對於帖子... –
你應該看看[我們可以停止使用AddWithValue()了嗎?](http://blogs.msmvps.com/jcoehoorn/blog/2014/05/12/can-we -stop-using-addwithvalue-already /)並停止使用'.AddWithValue()' - 它會導致意想不到的結果。 –