Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8174151
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T22:22:40+00:00 2026-06-06T22:22:40+00:00

I made a Web service in which I have a function to count some

  • 0

I made a Web service in which I have a function to count some data in my SQL data base. Here the code of my WebService.asmx :

[System.Web.Script.Services.ScriptService]
public class WebService1 : System.Web.Services.WebService
{

    [WebMethod]
    public int SalesNumberMonth(int i)
    {
        int total = 0;
        SqlConnection connection = new SqlConnection(ConfigurationManager.ConnectionStrings["Sql"].ConnectionString);
        try
        {
            string request = "SELECT * FROM sales_Ventes V INNER JOIN sys_AnneesFiscales A ON V.AnneeFiscale = A.Code INNER JOIN sys_Mois M ON V.Mois = M.Code WHERE M.Code='" + i + "'" + " AND Active = 'true'";
            connection.Open();
            SqlCommand Req = new SqlCommand(request, connection);

            SqlDataReader Reader = Req.ExecuteReader();
            while (Reader.Read())
            {
                total++;
            }
            Reader.Close();
        }
        catch
        {

        }
        connection.Close();
        return total;
    }
}

and here my script.js :

var sin = [], cos = [];
for (var i = 1; i < 13; i += 1) {
    GestionPro.WebService1.SalesNumberMonth(i,  function (e) { sin.push([i, e]); }  ,function (response) { alert(response); }  );
    cos.push([i, 2]);
}
var plot = $.plot($("#mws-test-chart"),
       [{ data: sin, label: "Sin(x)²", color: "#eeeeee" }, { data: cos, label: "Cos(x)", color: "#c5d52b"}], {
           series: {
               lines: { show: true },
               points: { show: true }
           },
           grid: { hoverable: true, clickable: true }
       });

My probleme is on this line :

GestionPro.WebService1.SalesNumberMonth(i,  function (e) { sin.push([i, e]); }  ,function (response) { alert(response); }  );

When I swap the two functions, the alerts are displayed well but in this order I can’t add the value of my function in sin[]. I should miss something but don’t know what …

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T22:22:41+00:00Added an answer on June 6, 2026 at 10:22 pm

    There are enormously lots of issues with your code:

    • You are triggering AJAX requests in the for loop. It would be far more optimal to trigger a single AJAX request that will return the entire result. It’s always better to send fewer requests that send more data rather than lots of small AJAX requests
    • You are using SELECT * and then counting on the client code in a loop instead of using the COUNT SQL aggregate function
    • You are not disposing properly any of the IDisposable resources such as database connections, commands and readers
    • You are using a string concatenation to build your SQL query instead of using parametrized queries
    • You are not taking into account the asynchronous nature of AJAX

    The issues being mentioned, let’s start by fixing them.

    Let’s first fix the server side code:

    [System.Web.Script.Services.ScriptService]
    public class WebService1 : System.Web.Services.WebService
    {
        [WebMethod]
        public int[] SalesNumbersMonths(int[] months)
        {
            // Could use LINQ instead but since I don't know which version
            // of the framework you are using I am providing the naive approach
            // here. Also the fact that you are using ASMX web services which are
            // a completely obsolete technology today makes me think that you probably
            // are using something pre .NET 3.0
            List<int> result = new List<int>();
            foreach (var month in months)
            {
                result.Add(SalesNumberMonth(month));
            }
            return result.ToArray();
        }
    
    
        [WebMethod]
        public int SalesNumberMonth(int i)
        {
            using (SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["Sql"].ConnectionString))
            using (SqlCommand cmd = conn.CreateCommand())
            {
                conn.Open();
                cmd.CommandText = "SELECT COUNT(*) FROM sales_Ventes V INNER JOIN sys_AnneesFiscales A ON V.AnneeFiscale = A.Code INNER JOIN sys_Mois M ON V.Mois = M.Code WHERE M.Code=@Code AND Active = 'true'";  
                cmd.Parameters.AddWithValue("@Code", i);
                return (int)cmd.ExecuteScalar();
            }
        }
    }
    

    OK, you will notice now the new method that I added and which allows to calculate totals for a number of months and returning them as an array of integers to avoid wasting bandwidth in meaningless AJAX requests.

    Now let’s fix your client side code:

    var months = [];
    
    for (var i = 1; i < 13; i += 1) {
        months.push(i);
    }
    
    GestionPro.WebService1.SalesNumbersMonths(months, function (e) { 
        // and once the web service succeeds in the AJAX request we could build the chart:
        var sin = [],
            cos = [];
    
        for (var i = 0; i < e.length; i++) {
            cos.push([i, 2]);
            sin.push([i, e[i]]);
        }
    
        var chart = $('#mws-test-chart'),
        var data = [
            { data: sin, label: 'Sin(x)²', color: '#eeeeee' }, 
            { data: cos, label: 'Cos(x)', color: '#c5d52b' }
        ];
    
        var series = { 
            series: {
                lines: { show: true },
                points: { show: true }
            }
        };
    
        var plot = $.plot(
            chart, 
            data, 
            series, 
            grid: { hoverable: true, clickable: true }
        );
    
        // TODO: do something with the plot
    
    }, function (response) { 
        // that's the error handler
        alert(response); 
    });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have converted my web service to wcf service which has some datacontracts. As
I have an IntentService which queues up web service calls to be made. I
I made an android application which connects to a local web service on my
I made a WPF example that consumes a web service ( www.webservicex.com/globalweather.asmx ) in
I have just made a portlet with web service for liferay to learn how
We have a web service which provides search over hotels. There is a problem
Here is the situation: We have to build a system made of web services
We have a simple ASP.Net WCF Ajax enabled web service which is called via
I have a web service running in http://server/abc/service which is being accessed by Flash
I have a WCF web service which is working seamlessly. Now the aim is

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.