Why I need a local variables to store value in SqlParameter from Ajax to WCF method

I'm currently work with WCF and Jquery ajax for testing purposes. I'm quite new with WCF. Anyways, I have a service is called "ProductsService" has four parameters that is invoked from Jquery Ajax. I had a little problem with it, but thankfully I resolved it. My solution by estimation, so can any body explain to me why this problem solved this way?

Here the problem scenario.

I inserted new product from Ajax to WCF service method that will store the parameters in SqlParameter. However, only one SqlParameter has a value and the rest of SqlParameter were null. I restructured my method a bit, but still the same. Then I tried to add local variables to see if it would hold the values and store them into SqlParameter.

This is the method

public void insertProdect(int categoryId, string name, string discrption, decimal price)
    {
        // for debuge

        int t1 = categoryId;
        String t2 = name;
        String t3 = discrption;
        decimal t4 = price;


        String sc = ConfigurationManager.ConnectionStrings["BDCS"].ConnectionString;
        using (SqlConnection con = new SqlConnection(sc))
        {

            //
            SqlCommand cmd = new SqlCommand("spInsertNewProductByCategoryId", con);
            cmd.CommandType = CommandType.StoredProcedure;

            //
            SqlParameter CategoryId = new SqlParameter();
            CategoryId.ParameterName = "@CategoryId";
            //CategoryId.Value = categoryId;
            CategoryId.Value = t1;

            //
            SqlParameter ProductName = new SqlParameter();
            ProductName.ParameterName = "@ProductName";
            //ProductName.Value = ProductName;
            ProductName.Value = t2;

            //
            SqlParameter ProductDescription = new SqlParameter();
            ProductDescription.ParameterName = "@ProductDescription";
            //ProductDescription.Value = ProductDescription;
            ProductDescription.Value = t3;

            //
            SqlParameter ProductPrice = new SqlParameter();
            ProductPrice.ParameterName = "@ProductPrice";
            //ProductPrice.Value = price;
            ProductPrice.Value = t4; 

            //
            cmd.Parameters.Add(CategoryId);
            cmd.Parameters.Add(ProductName);
            cmd.Parameters.Add(ProductDescription);
            cmd.Parameters.Add(ProductPrice);

            //
            con.Open(); 
            cmd.ExecuteNonQuery();

        }

ajax call

     $(document).on("click", "#doInsertNewProduct", function () {

    $.when(
        $.ajax({
            url: "../ProductsService.svc/insertProdect",
            method: "post",
            contentType: "application/json; charset=utf-8",

            data: JSON.stringify({
                categoryId: $("#InsertCatogeryTextName").val(),
                name: $("#InsertProductTextName").val(),
                discrption: $("#InsertProductTextDiscrption").val(),
                price: $("#InsertProductTextPrice").val()
            }),
            success: function () {
                location.reload();
                console.log("Done! Successfully insert new Product");
            },
            error: function () {
                console.log("Error! unbale to insert new Product");
            }
        }));

});

Thanks all

Jon Skeet
people
quotationmark

You don't need local variables for this - either for the individual SqlParameters or the values. Your code would be simpler as:

public void InsertProduct(int categoryId, string name, string description, decimal price)
{
    String sc = ConfigurationManager.ConnectionStrings["BDCS"].ConnectionString;
    using (SqlConnection con = new SqlConnection(sc))
    {
        con.Open(); 
        using (SqlCommand cmd = new SqlCommand("spInsertNewProductByCategoryId", con))
        {
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.Add("@CategoryId").Value = categoryId;
            cmd.Parameters.Add("@ProductName").Value = name;
            cmd.Parameters.Add("@ProductDescription").Value = description;
            cmd.Parameters.Add("@ProductPrice").Value = price;
            cmd.ExecuteNonQuery();
        }
    }
}

I'd strongly advise you to specify the types of the parameters as well, mind you, e.g.

cmd.Parameters.Add("@CategoryId", SqlDbType.Int).Value = categoryId;

people

See more on this question at Stackoverflow