Chetan Sarode Chetan Sarode - 3 months ago 14
Ajax Question

Ajax function executes but data is not inserting to database

When I insert the details in form and click on insert button, it shows "User has been added successfully." But the data is not inserted into the table. I also try to check the error in browser by pressing F12, it shows the object con is null. How to solve this? Thanks in advance

C# web method:

[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);
}
}
}


Ajax, jQuery code:

$(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 stored procedure:

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

Answer

InserData function dosen't call any ExecuteNonQuery which will persist data in database command.

public static void InsertData(ConsigneeMast Consignee)
    {
        using(var scon=new SqlConnection(strConnection))
        {
            using(var cmd=new SqlCommand())
            {
                scon.open();
                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);
                cmd.ExecuteNonQuery(); 
            }
        }
    }