0

I have a standalone .ASPX page (no code behind) which holds a simple WebMethod that I'm calling via JQuery Ajax. See below:

(Please be aware this is proof-of-concept code, not production code!)

<!DOCTYPE html>
<%@ Page Language="C#" %>
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.SqlClient" %>

<html>
<head runat="server">
<title>Untitled</title>

<script runat="server" type="text/c#">
[System.Web.Services.WebMethod] 
public static int GetData(int Id)
{
    string connectionString = "Data Source=xxx;Initial Catalog=xxx;User Id=xxx;Password=xxx;";
    using (SqlConnection connection = new SqlConnection(connectionString))
    {
        string query = "SELECT AVG(Number) FROM Data WHERE ID = @Id;";
        using (SqlCommand command = new SqlCommand(query, connection))
        {
            command.Parameters.AddWithValue("@Id", Id);
            connection.Open();
            return (int)command.ExecuteScalar();
        }
    }
}
</script>

<script src="http://code.jquery.com/jquery.min.js" type="text/javascript"></script>

<script type="text/javascript">
    function getAvg() {
        $.ajax({
            type: 'POST',
            url: "WebMethodAjaxExample.aspx/GetData",
            data: JSON.stringify({Id: $('#tbId').val()}),
            contentType: 'application/json',
            success: function (data, textStatus) {
                $('#theParagraph').text(data.d);
            }
        })
    }
</script>
</head>
<body>
<div>
<input type="text" id="tbId" />
<button onclick="getAvg()">Go</button>
</div>
<p id="theParagraph"></p>
</body>
</html>

I've sanity checked the the SQL query and know that it returns a FLOAT (seven-and-a-bit). However, as you can see my WebMethod returns an int. Hence my page was always rounding the number.

I decided to change the WebMethod return type and the ExecuteScalar casting to Double. But still the page was returning "7".

After a bit of tinkering the most interesting fact I learned was that when I decided to change the WebMethod name in the C# code to GetDatum, and made the relevant change to the JQuery Ajax function name too, running the page this time I get a return status 500 from the Ajax call with "Unknown Web Method" as the error message.

It feels like the page is not being dynamically recompiled as I would expect it to be, instead it is still using a cached version even though the request header states "no-cache".

If it's of any help, the page is hosted in Sharepoint 2010.

Can anyone understand what's going on?

UPDATE: Recycling the application pool makes the most up to date WebMethod code work, but further updates are not reflected until the application pool is reset again.

chrsmrrtt
  • 197
  • 3
  • 14
  • Why is your web method returning int when you want float? AVG function only returns float if "Number" column is float. – fenix2222 Jun 07 '12 at 01:47
  • Try also using parseFloat function in javascript to make sure your result is definitely float. – fenix2222 Jun 07 '12 at 01:55
  • The code above is the "before" code. The description afterwards tells you of the changes I make. I'd say the issue is not the coding, more the caching. – chrsmrrtt Jun 07 '12 at 08:08
  • Wow... I've been going NUTS over this issue and the only way I was able to fix it was by copying the web service to a new file...you've finally given me an answer. One thing, how do you "recycle the application pool"? Thanks! – Jared May 09 '13 at 15:51
  • Figured our a cool trick to let you do it without recycling the app pool! Posting to answers. – Jared May 09 '13 at 19:40
  • Not a week goes by (and barely a day) where, in our line of work, "have you tried turning it off an on again" isn't a solution to the problem. My golden rule when things don't work and I'm sure they should: restart everything possible. It's crap, but that's how it is. – Jason Snelders Jun 27 '14 at 09:54

1 Answers1

0

I grabbed this from another answer that I can't seem to find the link for, but you don't need to reset the whole application pool! I have an ASMX file that uses AJAX to pull data from a web service. I was going crazy because updating the web service was not making changes to the data that the web page was pulling.

Add this in your code behind's Page Load and it'll clear the cache and use the newly updated web service:

Response.Cache.SetCacheability(HttpCacheability.NoCache);
Response.Cache.SetExpires(DateTime.Now);
Response.Cache.SetNoServerCaching();
Response.Cache.SetNoStore();

This does exactly what I need it to, but I think I'd really only need the SetExpires line to clear the cache for this page.

Jared
  • 878
  • 3
  • 11
  • 19