Click here to Skip to main content
15,867,756 members
Please Sign up or sign in to vote.
1.33/5 (2 votes)
See more:
Hi guys,

I have asp.net web site in c#.

I'm using Ajax to retrieve data from database.

Code:

<script type="text/javascript">
        function GetDescription(a) {
            alert(a); // the dropdown item selected value
                $.ajax({
                    type: 'POST',
                    contentType: "application/json; charset-8;",
                    url: 'Default.aspx/GetRef',
                    dataType: 'json',
                    data: "{ 'id':'" + a + "'}",
                    success: function (result) {
                        alert(result);
                    }
                });

        }
    </script>


Dropdownlist onchange()
<asp:DropDownList ID="ddlsch" runat="server" Width="80px"                                                         onchange="GetDescription(this.value)" >
</asp:DropDownList>


Default.aspx/GetRef
[WebMethod]
    public string GetRef(string id)
    {
        DataTable dt = new DataTable();
        SqlParameter[] p = new SqlParameter[1];
        p[0] = new SqlParameter("@RefID", Convert.ToInt32(id));
        dt = dl.GetDataWithParameters("Sp_WT_GetRef", p);

        string data = dt.Rows[0]["Description"].ToString() +"|"+ dt.Rows[0]["PriceInUSD"].ToString();

        return data;
    }


Error
http://localhost:54576/AutomobileWebApp/Default.aspx/GetRef Failed to load resource: the server responded with a status of 500 (Internal Server Error)
http://localhost:54576/resources/demos/style.css Failed to load resource: the server responded with a status of 404 (Not Found)


Can anyone please help me where i'm wrong...

Thanks
Posted
Updated 13-May-15 7:42am
v2
Comments
Sergey Alexandrovich Kryukov 13-May-15 12:56pm    
Sorry, "not working" is not informative.
—SA
abdul subhan mohammed 13-May-15 13:43pm    
http://localhost:54576/AutomobileWebApp/Default.aspx/GetRef Failed to load resource: the server responded with a status of 500 (Internal Server Error)
http://localhost:54576/resources/demos/style.css Failed to load resource: the server responded with a status of 404 (Not Found)
abdul subhan mohammed 3-Jun-15 10:14am    
please help me in this:
http://www.codeproject.com/Questions/997253/How-to-Save-JQXGrid-data-into-SQL-SERVER-in-asp-ne
Afzaal Ahmad Zeeshan 13-May-15 13:07pm    
What is the exception actually? Please check your Console for more details on this. Press F12 for Console and other inspection tools on your browser.
abdul subhan mohammed 13-May-15 13:43pm    
http://localhost:54576/AutomobileWebApp/Default.aspx/GetRef Failed to load resource: the server responded with a status of 500 (Internal Server Error)
http://localhost:54576/resources/demos/style.css Failed to load resource: the server responded with a status of 404 (Not Found)

Make sure the webmethod is static, and use the .d to get the result. If you're getting "undefined" in the first alert box then the problem is that your js isn't reading the value of the dropdown correctly.

XML
<asp:DropDownList ID="ddlsch" runat="server" Width="80px" onchange="GetDescription(this.value)" >
    <asp:ListItem Text="One" Value="1" />
    <asp:ListItem Text="Two" Value="2" />
    <asp:ListItem Text="Three" Value="3" />
</asp:DropDownList>

<script type="text/javascript">
    function GetDescription(a) {
        alert(a); // the dropdown item selected value
        $.ajax({
            type: 'POST',
            contentType: "application/json; charset-8;",
            url: 'Default.aspx/GetRef',
            dataType: 'json',
            data: "{ 'id':'" + a + "'}",
            success: function (result) {
                alert(result.d);
            }
        });

    }
</script>


C#
[WebMethod]
public static string GetRef(string id)
{
    // your actual code here
    return id;
}
 
Share this answer
 
Comments
abdul subhan mohammed 14-May-15 9:00am    
No no, not in first alert box... the undefined is coming in error one.
the GetRef(string id) is not getting its argument... i dont know why.
abdul subhan mohammed 14-May-15 9:01am    
i think error is in this line: data: "{ 'id':'" + a + "'}",
F-ES Sitecore 14-May-15 9:04am    
What is in "a" then?
abdul subhan mohammed 3-Jun-15 10:15am    
plz help me in this:
http://www.codeproject.com/Questions/997253/How-to-Save-JQXGrid-data-into-SQL-SERVER-in-asp-ne
abdul subhan mohammed 14-May-15 9:07am    
a is ddl selected value, i'm getting a. but not in GetRef(string id)
Your ajax code is working. Your server-side code is not working and is generating run-time error. You have said the same thing in your comment. 500 HTTP error code means that there was an error while running the code.

In your code, (In my opinion, the culprit is)

C#
// Conversion
Convert.ToInt32(id)


In this code, if the id is not a valid integer. It would throw exception. Which is most likely to be a cause of this problem. The exception would be sent as an HTTP 500 status code. Thus terminating the execution and causing you too see as if "Ajax code is not working".

Finally, 404's do not generate problems in requests. Unless you are downloading a resource which would terminate the downloading (ever wondered why a broken image is shown?). Solution to this would be to debug the application. Set a break point to your function and run the application again and check your application's state.

For the above 500 problem, if the exception is raised at this line. Please read my recent answer in this thread[^]. It gives a solution for handling this error.

Edit

If the success function returns the [object Object], then it is correct. This is the representation of the object in JavaScript. What you wanted to do was to act wisely on this object as a JSON object. Extract nodes and other members from this JSON object.
 
Share this answer
 
v2
Comments
abdul subhan mohammed 14-May-15 4:48am    
I have made my GetRef() as "Static" n its working now, but not taking arguments which
i'm passing from ajax code, 'data:{id: a}' and on success its not returning the result instead its returning 'object object'. Can u plz help me in this.

Thanks
Afzaal Ahmad Zeeshan 14-May-15 7:06am    
Re-read the solution, also read the comment that VICK has posted to your post. Add an error handler to your code.
F-ES Sitecore 14-May-15 7:59am    
Use Fiddler, or the browser dev tools, to look at the network traffic for your calls. It will show any errors being masked by the calling code, and will show you exactly what is being returned to the calling code. To access the results of a webmethod the data is put in a property called "d"

alert(result.d);
abdul subhan mohammed 14-May-15 8:03am    
its not taking the argument, which i'm passing in 'data:{id: id}' from ajax code to GetRef(string id) method... plz help me.
Afzaal Ahmad Zeeshan 14-May-15 8:04am    
Why not debug your application on server-side? Debug the ASP.NET code which handles the requests.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900