2

In my Ajax function i tried to pass a int Parameter to the Webmethod but it's not success. Here i paste my code

Ajax Function

    $('#drpChurchNames').on('change', function () {
        //alert($(this).val());
        LoadFathersToChurch(churchId)
    });

    function LoadFathersToChurch(churchId) {

        var url = '<%=ResolveUrl("WebMethods.aspx/GetFatherNames") %>';
        $.ajax({
            url: url,
            type: "GET",
            dataType: "json",
            data:'{ Id: " '+churchId +' "}',
            contentType: "application/json; charset=utf-8",
            success: function (Result) {
                $.each(Result.d, function (key, value) {
                    $("#drprevfather").append($("<option></option>").val
                    (value.Id).html(value.FatherName));
                });
            },
            error: function (e, x) {
                alert(x.ResponseText);
            }
        });

    }

Here is my WebMethod

      [WebMethod]
 [ScriptMethod(UseHttpGet = true)]
 public static List<FatherNames> GetFatherNames(int ChurchId)
 {
     List<FatherNames> FathersList = new List<FatherNames>();
     try
     {
         SqlCommand comChurchNames = new SqlCommand("GetFathers", conDB);
         comChurchNames.CommandType = CommandType.StoredProcedure;
         comChurchNames.Parameters.Add("@Id", SqlDbType.Int);
        comChurchNames.Parameters["@Id"].Value = ChurchId;
        if (conDB.State == ConnectionState.Closed)
            conDB.Open();

        SqlDataReader rdr = comChurchNames.ExecuteReader();
        DataTable dt = new DataTable();
        dt.Load(rdr);
        foreach (DataRow r in dt.Rows)
        {
            FathersList.Add(new FatherNames
            {
                Id = (int)r["Id"],
                FatherName = r["FatherName"].ToString()

            });
        }
    }

Here is my SP

ALTER PROCEDURE [dbo].[GetFathers]
@SelectIndexName int
AS
BEGIN
Select * from dbo.RevFathers
Where ChurchId = @SelectIndexName
END

2 Answers 2

2

You are passing Id as parameter and the correct is ChurchId just like webmethod signature GetFatherNames(int ChurchId).

There is the correct way:

$.ajax({
        url: url,
        type: "GET",
        dataType: "json",
        data:'{ ChurchId: " '+churchId +' "}',
        contentType: "application/json; charset=utf-8",
        success: function (Result) {
            $.each(Result.d, function (key, value) {
                $("#drprevfather").append($("<option></option>").val
                (value.Id).html(value.FatherName));
            });
        },
        error: function (e, x) {
            alert(x.ResponseText);
        }
    });
Sign up to request clarification or add additional context in comments.

Comments

0

Is your webmethod actually getting executed? If it is and you think it should be returning data, it could be that your webmethod is not returning JSON, thus jQuery may be generating and error and not firing success.

I have this in my global.asax to configure the output for both XML and JSON:

using System.Net.Http.Formatting;
using System.Net.Http.Headers;
using System.Web.Http;

Placed in app_start

GlobalConfiguration.Configuration.Formatters.JsonFormatter.MediaTypeMappings.Add(new QueryStringMapping("type", "json", new MediaTypeHeaderValue("application/json")));
GlobalConfiguration.Configuration.Formatters.XmlFormatter.MediaTypeMappings.Add(new QueryStringMapping("type", "xml", new MediaTypeHeaderValue("application/xml")));

I know this isn't a direct answer, but I can't post comments yet so I would have just asked the above question first about is the webmthod getting called.

Comments

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.