Ajax函数执行但数据未插入数据库

时间:2016-09-02 07:17:06

标签: c# jquery sql-server ajax

当我在表单中插入详细信息并单击插入按钮时,它会显示"用户已成功添加。"但数据未插入表中。我还尝试通过按F12检查浏览器中的错误,它显示对象con为null。怎么解决这个?提前致谢

C#网络方法:

[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代码:

$(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

3 个答案:

答案 0 :(得分:0)

InserData函数不会调用任何会在数据库命令中保留数据的ExecuteNonQuery

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

答案 1 :(得分:0)

添加所有参数后,您缺少这两行代码

  scon.Open();
  cmd.ExecuteNonQuery();

答案 2 :(得分:0)

如评论中所述,您必须使用InsertData方法调用执行存储过程。

尝试像这样修改:

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

      scon.Open();
      cmd.ExecuteNonQuery(); 
    }
  }
}

不调用ExecuteNonQuery InsertData方法成功执行,导致AJAX调用成功并显示错误消息。