2
votes

Question Overview: I am creating an application in which I have a html select list from where I select a category and from that category I get items and there images using ajax webmethod.

Problem Overview: I faced many 500 error in ajax linq to sql and fix it. But now I am working on ado.net application and The problem which I was facing is When I select a category from select list(catlist) its shows me error from here error: function (xhr) { alert(xhr.status);}.

  [ArgumentException]: Unknown web method elist.
 Parameter name: methodName
 at System.Web.Script.Services.WebServiceData.GetMethodData(String methodName)
 at System.Web.Handlers.ScriptModule.OnPostAcquireRequestState(Object sender, EventArgs eventArgs)
 at System.Web.HttpApplication.SyncEventExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute()
 at System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously)
.

How can I fix this error. I thought I briefly describe my question.

Default.Aspx Code Overview:

Below is my HTML

<table>
    <tr>
        <td>
            <select id="catlist" runat="server" onchange="getImageUrl()"></select>
        </td>
        <td></td>
    </tr>
    <tr>
        <td>
            <img id="imgload" width="180" height="100" src="" alt="No Image Found" />
        </td>
        <td>
            <ul>
                <li>Description</li>
                <li>Loreum IspumLoreum IspumLoreum IspumLoreum IspumLoreum IspumLoreum IspumLoreum Ispum</li>
            </ul>
        </td>
    </tr>
</table>

Below is my Javascript Function

function getImageUrl() {
        var catid = $("#catlist")[0].value;
        $.ajax({
            url: "Default.aspx/elist",
            data: { catId: catid },
            contentType: "Application/json; charset=utf-8",
            responseType: "json",
            method: "POST",
            success: function (response) {
                alert(response.d);
            },
            error: function (xhr) {
                alert(xhr.status);
            },
            Failure: function (response) {
                alert(response);
            }
        });
    }

Default.Aspx.cs Code Overview:

Below is my custom class

public class events
{
    public string EVE_NAME { get; set; }
    public string EVE_IMG_URL { get; set; }
    public string EVE_DESCRIPTION_SHORT { get; set; }
}

Below is the Datatable method

private static DataTable dt2(int catId)
{
    DataTable dataTable = new DataTable();
    SqlConnection conn = new SqlConnection("Data Source=.;Initial Catalog=EVENT;Persist Security Info=True;User ID=sa;Password = 123");
        string query = "sp_view";

    SqlCommand cmd = new SqlCommand(query, conn);
    cmd.CommandType = CommandType.StoredProcedure;
    cmd.Parameters.Add("@catID", SqlDbType.Int).Value = catId;
    conn.Open();
    SqlDataAdapter da = new SqlDataAdapter(cmd);
    da.Fill(dataTable);
    return dataTable;
 }

Below is the WebMethod

[WebMethod]
private static List<events> elist(int catId)
{
     List<events> eve = new List<events>();
     eve = (from DataRow row in dt2(catId).Rows
           select new events
           {
                   EVE_NAME = row["EVE_NAME"].ToString(),
                   EVE_IMG_URL = row["EVE_IMG_URL"].ToString(),
                   EVE_DESCRIPTION_SHORT = row["EVE_DESCRIPTION_SHORT"].ToString(),
           }).ToList();
     return eve;
 }

NOTE: Database column names and events class properties names are same.

3
I note your elist method is private. I can't say I've done much of this myself, but perhaps that's the problem? What happens if you make it public?Jon Skeet
You might be interessed by this thread: stackoverflow.com/questions/18244696/… because the native behaviour of WebMethod returns XML, and you seems to want some Json instead.Didier Aupest
@JonSkeet Yes, my first mistake is did't make my method public.Ahmer Ali Ahsan
@DidierAupest Thanks for above thread. What I am just missing is [ScriptMethod(UseHttpGet = true, ResponseFormat = ResponseFormat.Json)] at below my [WebMethod] attribute which I saw in your above thread. Now I finally getting reponse in json format. Again thanks for your precious time.Ahmer Ali Ahsan
Third I am posting my clear code so If others developers stuck they easily get answer.Ahmer Ali Ahsan

3 Answers

3
votes

According to the documentation of the attribute WebMethod:

https://msdn.microsoft.com/en-us/library/byxd99hx(v=vs.90).aspx

Attaching the WebMethod attribute to a Public method indicates that you want the method exposed as part of the XML Web service. You can also use the properties of this attribute to further configure the behavior of the XML Web service method. For more information, see Code Model for XML Web Services in Managed Code.

I guess, Jon Skeet is right. :)

1
votes

WebMethod must be public, or it will not be reachable from outside.

0
votes

I just put [ScriptMethod(UseHttpGet = true, ResponseFormat = ResponseFormat.Json)] line after my web method attribute and make my all method associated with web method to public. Now my Clear code is:

    [WebMethod]
    [ScriptMethod(UseHttpGet = true, ResponseFormat = ResponseFormat.Json)]
    public static List<events> elist(int catId)
    {
        List<events> eve = new List<events>();
        eve = (from DataRow row in dt2(catId).Rows
               select new events
               {
                   EVE_NAME = row["EVE_NAME"].ToString(),
                   EVE_IMG_URL = row["EVE_IMG_URL"].ToString(),
                   EVE_DESCRIPTION_SHORT = row["EVE_DESCRIPTION_SHORT"].ToString(),
               }).ToList();
        return eve;
    }

    public static DataTable dt2(int catId)
    {
        DataTable dataTable = new DataTable();
        SqlConnection conn = new SqlConnection("Data Source=.;Initial Catalog=EVENT;Persist Security Info=True;User ID=sa;Password = 123");
        string query = "sp_view";

        SqlCommand cmd = new SqlCommand(query, conn);
        cmd.CommandType = CommandType.StoredProcedure;
        cmd.Parameters.Add("@catID", SqlDbType.Int).Value = catId;
        conn.Open();
        SqlDataAdapter da = new SqlDataAdapter(cmd);
        da.Fill(dataTable);
        conn.Close();
        da.Dispose();
        return dataTable;
    }