Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 431
  • Last Modified:

When I run my webservice, I do not see GetData or GetCount

Hi Experts,

I just wrote my first WebService, and it compiles, but I only see HelloWorld and not GetData or GetCount. Could you look at my code and tell me what I'm missing?

Thanks!
rss2
using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Collections.Generic;
using System.Text;
using System.Web;
using System.Collections;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.ComponentModel;
using Telerik.Web.UI;
using System.Configuration;
using System.Data;
using System.Data.Common;
 
namespace WebService1
{
    /// <summary>
    /// Summary description for Service2
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.Web.Script.Services.ScriptService]
    [ToolboxItem(false)]
    public class Service2 : System.Web.Services.WebService
    {
 
        [WebMethod]
        public string HelloWorld()
        {
            return "Hello World";
        }
 
        [WebMethod]
        public static int GetCount(List<GridFilterExpression> filterExpressions)
        {
            int count = 0;
 
            using (SqlConnection connection = new SqlConnection(ConfigurationManager.ConnectionStrings["TCA"].ConnectionString))
            {
                int counter = 0;
                List<DbParameter> parameters = new List<DbParameter>();
 
                StringBuilder sqlBuilder = new StringBuilder();
                sqlBuilder.Append("SELECT COUNT(*) FROM tblTCA_Claims");
 
                if(filterExpressions.Count > 0)
                    sqlBuilder.Append(" WHERE ");
 
                foreach (GridFilterExpression expression in filterExpressions)
                {
 
                    counter++;
                    Pair parameter = BuildParameter(expression);
                    parameters.Add((DbParameter)parameter.Second);
 
                    sqlBuilder.AppendFormat((string)parameter.First);
                    if (counter < filterExpressions.Count)
                    {
                        sqlBuilder.AppendFormat(" AND ");
                    }
                }
 
                SqlCommand command = new SqlCommand(sqlBuilder.ToString(),connection);
 
                parameters.ForEach(delegate(DbParameter parameter) { if (parameter != null) command.Parameters.Add(parameter); });
 
                connection.Open();
 
                SqlDataReader reader = command.ExecuteReader();
 
                while (reader.Read())
                {
                    count = reader.GetInt32(0);
                }
 
                reader.Close();
 
            }
            return count;
        }
 
        [WebMethod]
        public static List<Claim> GetData(int startIndex, int maximumRows, string sortExpressions, List<GridFilterExpression> filterExpressions)
        {
            startIndex = startIndex + 1;
 
            List<Claim> list = new List<Claim>();
 
            StringBuilder sqlBuilder = new StringBuilder();
            sqlBuilder.AppendLine("DECLARE @first_id int, @startrow int    ");
            sqlBuilder.AppendLine("SET ROWCOUNT @startRowIndex");
            sqlBuilder.AppendLine("SELECT ");
            sqlBuilder.AppendLine("      @first_id = ClaimID ");
            sqlBuilder.AppendLine("FROM tblTCA_Claims ");
            sqlBuilder.AppendLine("ORDER BY ClaimID");
            sqlBuilder.AppendLine("SET ROWCOUNT @maximumRows");
            sqlBuilder.AppendLine("SELECT c.*");
            sqlBuilder.AppendLine("FROM tblTCA_Claims c");
            sqlBuilder.AppendLine("WHERE ClaimID >= @first_id");
 
            int counter = 0;
            List<DbParameter> parameters = new List<DbParameter>();
 
            if (filterExpressions.Count > 0)
                sqlBuilder.AppendLine(" AND ");
 
            foreach (GridFilterExpression expression in filterExpressions)
            {
                counter++;
                Pair parameter = BuildParameter(expression);
                parameters.Add((DbParameter)parameter.Second);
 
                sqlBuilder.AppendFormat((string)parameter.First);
                if (counter < filterExpressions.Count)
                {
                    sqlBuilder.AppendFormat(" AND ");
                }
 
            }
 
            if (sortExpressions != String.Empty)
            {
                sqlBuilder.AppendLine(String.Format(" ORDER BY {0}", sortExpressions));
            }
 
            using (SqlConnection connection = new SqlConnection(ConfigurationManager.ConnectionStrings["TCA"].ConnectionString))
            {
                SqlCommand command = new SqlCommand(sqlBuilder.ToString(),connection);
 
                command.Parameters.AddWithValue("@startRowIndex", startIndex);
                command.Parameters.AddWithValue("@maximumRows",maximumRows);
 
                parameters.ForEach(delegate(DbParameter parameter) {if (parameter != null) command.Parameters.Add(parameter); });
 
                connection.Open();
 
                SqlDataReader reader = command.ExecuteReader();
 
                try
                {
                    while (reader.Read())
                    {
                        Claim newClaim = new Claim();
 
                        for (int i = 0; i < reader.FieldCount; i++)
                        {
                            switch (reader.GetName(i))
                            {
                                case "ClaimID":
                                    newClaim.ClaimID = reader.GetInt32(i);
                                    break;
                                case "Role":
                                    newClaim.Role = reader.GetString(i);
                                    break;
                                case "Insured":
                                    newClaim.Insured = reader.GetString(i);
                                    break;
                                case "Date Added":
                                    newClaim.DateAdded = reader.GetDateTime(i);
                                    break;
                                default:
                                    break;
                            }
                        }
                        list.Add(newClaim);
                    }
                }
                finally{
                    reader.Close();
                }
        }
            return list;
    }
        private static Pair BuildParameter(GridFilterExpression expression)
        {
            string fieldName = expression.FieldName.Trim().Split(' ')[0];
            GridKnownFunction filterFunction = (GridKnownFunction)Enum.Parse(typeof(GridKnownFunction),expression.FilterFunction);
 
            SqlParameter sqlParameter = null;
 
            string filterExpression = string.Empty;
            switch (filterFunction)
            {
 
                case GridKnownFunction.NoFilter:
                    filterExpression = "1 = 1";
                    break;
                case GridKnownFunction.Contains:
                    filterExpression = string.Format("[{0}] LIKE @{0}", fieldName);
                    sqlParameter = new SqlParameter(string.Format("@{0}",fieldName),
                        string.Format("%{0}%", Convert.ChangeType (expression.FieldValue, Type.GetType(expression.DataTypeName))));
                    break;
                case GridKnownFunction.DoesNotContain:
                    filterExpression = string.Format("[{0}] NOT LIKE @{0}", fieldName);
                    sqlParameter = new SqlParameter(string.Format("@{0}", fieldName),
                        string.Format("%{0}%", Convert.ChangeType (expression.FieldValue, Type.GetType(expression.DataTypeName))));
                    break;
                case GridKnownFunction.EndsWith:
                    filterExpression = string.Format("[{0}] LIKE @{0}", fieldName);
                    sqlParameter = new SqlParameter(string.Format("@{0}", fieldName),
                        string.Format("%{0}",Convert.ChangeType (expression.FieldValue, Type.GetType(expression.DataTypeName))));
                    break;
                case GridKnownFunction.EqualTo:
                    filterExpression = string.Format("[{0}] = @{0}", fieldName);
                    sqlParameter =
                        new SqlParameter(string.Format("@{0}", fieldName),
                                         Convert.ChangeType(expression.FieldValue, Type.GetType(expression.DataTypeName)));
                    break;
                case GridKnownFunction.NotEqualTo:
                    filterExpression = string.Format("[{0}] <> @{0}", fieldName);
                    sqlParameter =
                        new SqlParameter(string.Format("@{0}", fieldName),
                                         Convert.ChangeType(expression.FieldValue, Type.GetType(expression.DataTypeName)));
                    break;
                case GridKnownFunction.GreaterThan:
                    filterExpression = string.Format("[{0}] > @{0}", fieldName);
                    sqlParameter =
                        new SqlParameter(string.Format("@{0}", fieldName),
                                        Convert.ChangeType(expression.FieldValue, Type.GetType(expression.DataTypeName)));
                    break;
                case GridKnownFunction.LessThan:
                    filterExpression = string.Format("[{0}] < @{0}", fieldName);
                    sqlParameter =
                        new SqlParameter(string.Format("@{0}", fieldName),
                                         Convert.ChangeType(expression.FieldValue, Type.GetType(expression.DataTypeName)));
                    break;
                case GridKnownFunction.GreaterThanOrEqualTo:
                    filterExpression = string.Format("[{0}] >= @{0}", fieldName);
                    sqlParameter =
                        new SqlParameter(string.Format("@{0}", fieldName),
                                        Convert.ChangeType(expression.FieldValue, Type.GetType(expression.DataTypeName)));
                    break;
                case GridKnownFunction.LessThanOrEqualTo:
                    filterExpression = string.Format("[{0}] <= @{0}", fieldName);
                    sqlParameter =
                        new SqlParameter(string.Format("@{0}", fieldName),
                                         Convert.ChangeType(expression.FieldValue, Type.GetType(expression.DataTypeName)));
                    break;
                case GridKnownFunction.Between:
                    filterExpression = string.Format(" ([{0}] >= @{0}) AND ([{0}] <= @{0})", fieldName);
                    sqlParameter =
                        new SqlParameter(string.Format("@{0}", fieldName),
                                        Convert.ChangeType(expression.FieldValue, Type.GetType(expression.DataTypeName)));
                    break;
                case GridKnownFunction.NotBetween:
                    filterExpression = string.Format(" ([{0}] < @{0}) OR ([{0}] > @{0})", fieldName);
                    sqlParameter =
                        new SqlParameter(string.Format("@{0}", fieldName),
                                        Convert.ChangeType(expression.FieldValue, Type.GetType(expression.DataTypeName)));
                    break;
                case GridKnownFunction.IsEmpty:
                    filterExpression = string.Format(" [{0}] = @{0}", fieldName);
                    sqlParameter =
                        new SqlParameter(string.Format("@{0}", fieldName),
                                         string.Empty);
                    break;
                case GridKnownFunction.NotIsEmpty:
                    filterExpression = string.Format(" [{0}] <> @{0}", fieldName);
                    sqlParameter =
                        new SqlParameter(string.Format("@{0}", fieldName),
                                          string.Empty);
                    break;
                case GridKnownFunction.IsNull:
                    filterExpression = string.Format(" [{0}] = @{0}", fieldName);
                    sqlParameter =
                        new SqlParameter(string.Format("@{0}", fieldName),
                                         DBNull.Value);
                    break;
                case GridKnownFunction.NotIsNull:
                    filterExpression = string.Format(" [{0}] <> @{0}", fieldName);
                    sqlParameter =
                        new SqlParameter(string.Format("@{0}", fieldName),
                                         DBNull.Value);
                    break;
            }
 
            return new Pair(filterExpression,sqlParameter);
        }
 
        public class Claim
        {
            private int _ClaimID;
            private string _Role;
            private string _Insured;
            private System.Nullable<System.DateTime> _DateAdded;
 
            public Claim()
            {
 
            }
 
            public int ClaimID
            {
                get
                {
                    return this._ClaimID;
                }
                set
                {
                    if ((this._ClaimID != value))
                    {
                        this._ClaimID = value;
                    }
                }
            }
 
            public string Role
            {
                get
                {
                    return this._Role;
                }
                set
                {
                    if ((this._Role != value))
                    {
                        this._Role = value;
                    }
                }
            }
 
            public string Insured
            {
                get
                {
                    return this._Insured;
                }
                set
                {
                    if ((this._Insured != value))
                    {
                        this._Insured = value;
                    }
                }
            }
 
            public System.Nullable<System.DateTime> DateAdded
            {
                get{
                    return this._DateAdded;
                }
                set{
                    if ((this._DateAdded != value))
                    {
                        this._DateAdded = value;
                    }
                }
            }
 
        }
        }
 
}

Open in new window

0
rss2
Asked:
rss2
2 Solutions
 
Gorkem YukselCommented:
Hi,

Try removing the STATIC identifier from the method.  You will not see static methods/functions from an instantiated object.

Here is a quick example to show the difference between static and non-static functions:

// To call a non static function
SomeClass myclass = new SomeClass();
myclass.GetCount();

// To call a static function
SomeClass.GetCount();

As you can see, to call static functions, you have to use the class object directly..  if this is not what you want to do, then you will have to remove the word STATIC from your function.

Cheers,

G.
0
 
Anurag ThakurCommented:
0

Featured Post

Cyber Threats to Small Businesses (Part 1)

This past May, Webroot surveyed more than 600 IT decision-makers at medium-sized companies to see how these small businesses perceived new threats facing their organizations.  Read what Webroot CISO, Gary Hayslip, has to say about the survey in part 1 of this 2-part blog series.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now