1

I have to pass the value in JSON.stringifyand receive in Server side.

Note: when I try to pass the value directly without using JQuery variable, It's working fine.

Without Jquery variable(it's working)

data: JSON.stringify({ "VendorID": "1106", "Date": "2016-12-30" }),

When I try to pass value through JQuery variable,

Error message

it throws internal server error

"Procedure or function 'FVendorAging' expects parameter '@Date', which was not supplied."

converting date format from(dd-MM-yyyy) to (yyyy-MM-dd) using JQuery

 if ($('#ContentPlaceHolder1_txtDate').val() !== '') {
        dateAr = $('#ContentPlaceHolder1_txtDate').val().split('-');
        var newDates = dateAr[2] + '-' + dateAr[1] + '-' + dateAr[0];
        alert('Date is not empty = ' + newDates);  //2017-07-11
    }

Ajax code

$.ajax({
        type:"POST",
        contentType:"application/json;charset=utf-8",
        url: "GPCreateCheque.aspx/BindData",
        //data:"{}",
        data: JSON.stringify({ "VendorID": +selecteVendorID, "Date": +newDates }),
        dataType:"json",
        success: function (data) {
            alert("success = " + data.d[0].DocumnetNo);
            for(var i=0;i<data.d.length;i++){
                $("#ContentPlaceHolder1_GridView1").append("<tr><td><input id=\"Checkbox" + i + "\" class=\"checkBoxClass\" type=\"checkbox\" /></td><td>" + data.d[i].DocumnetNo + "</td><td>" + data.d[i].Date + "</td><td class='ActualAmount' >" + data.d[i].OriginalAmount + "</td></tr>");
            }
        },
        error:function(result){
            alert("Error");
        }
    })

C# code

[WebMethod]
    public static UserDetails[] BindData(string VendorID,string Date)
    {
        DataTable dt = new DataTable();
        List<UserDetails> details = new List<UserDetails>();

        string cs = ConfigurationManager.ConnectionStrings["GPMatajerAlSaudia"].ConnectionString;
        using (SqlConnection con = new SqlConnection(cs))
        {
            SqlCommand cmd = new SqlCommand("FVendorAging", con);
            cmd.CommandType = CommandType.StoredProcedure;

            //cmd.Parameters.AddWithValue("@Date", "2017-07-11");
            //cmd.Parameters.AddWithValue("@VendorID", "1106");

            cmd.Parameters.AddWithValue("@Date", Date);
            cmd.Parameters.AddWithValue("@VendorID", VendorID);

            con.Open();
            SqlDataAdapter da = new SqlDataAdapter(cmd);
            da.Fill(dt);
            foreach (DataRow drow in dt.Rows)
            {
                UserDetails user = new UserDetails();
                user.DocumnetNo = drow["DocumnetNo"].ToString();
                user.Date = drow["Date"].ToString();
                user.OriginalAmount = drow["OriginalAmount"].ToString();
                details.Add(user);
            }
        }
        return details.ToArray();
    }

1 Answer 1

4

The issue is because you're prefixing the date variables with a +. This means that you're attempting to parse a date formatted string as a Number() - which will cause problems.

To fix the issue just remove the +. Also note that as you've specified the contentType as JSON you don't need to manually call JSON.stringify as jQuery will do that for you. Try this:

data: { 
  "VendorID": selecteVendorID, 
  "Date": newDates 
},
Sign up to request clarification or add additional context in comments.

1 Comment

Genius, you are correct. It's working. But I cannot accept answer for another 9 min. I will accept answer later.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.