Web Service question namespace

Hello all,

I am still somewhat getting acclimated to web services.  So using VS2010 I created a new web application project then added a web service asmx to it and it of course adds the ASMX and CS file.   So I am playing around with returning a DataTable as JSON format using this link I found.  My goal is to get this into JSON format and load a dropdown with JQuery all new to me as well.

http://schotime.net/blog/index.php/2008/07/27/dataset-datatable-to-json/

I created a class in the web application that exists in the same namespace of course and then I have my web methods.   When I run in debug mode first I was getting an error.

This web service is using http://tempuri.org/ as its default namespace.
Recommendation: Change the default namespace before the XML Web service is made public.

How do I deal with that error do I just place in my actual namespace that is in the web service class?  I am posting that here.  I tried just setting that to the outer namespace as below then when I run in debug I don't see the dictionary GetStudies() I have maybe that is okay but it does not show anything when I run it just says:

wsSpotfireCricit

The following operations are supported. For a formal definition, please review the Service Description.

Attached code:
using System;
using System.Web;
using System.IO;
using System.Collections;
using System.Collections.Generic;
using System.Configuration;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.Data;
using System.Web.Script.Serialization;
using System.Web.Script.Services;
using Oracle.DataAccess.Client;

namespace SpotfireCricitHarness.WebService
{
    /// <summary>
    /// Web Service wsSpotfireCricit for making JSON format response methods
    /// </summary>
    [WebService(Namespace = "SpotfireCricitHarness.WebService")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [ScriptService]
    public class wsSpotfireCricit : System.Web.Services.WebService
    {
        public wsSpotfireCricit()
        {
        //Uncomment the following line if using designed components 
        //InitializeComponent(); 
        }

        [WebMethod(Description = "Gets studies based on user policy.")]
        [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
        public static Dictionary<string, object> GetStudies()
        {
            string sql = "select studyid from R_DM_MV group by studyid order by studyid";
            string cconn = ConfigurationManager.ConnectionStrings["ConnCricit"].ConnectionString;
            return JsonMethods.ToJson(GetDataTable(sql, cconn));
        }

        private static DataTable GetDataTable(string query, string connectionString)
        {
            DataTable table = new DataTable();
            try
            {
                using (OracleConnection connection = new OracleConnection(connectionString))
                {
                    connection.Open();

                    //set client info - apply policy to only view allowed data
                    string user = HttpContext.Current.Request.ServerVariables["LOGON_USER"].ToUpper();
                    OracleCommand cmd = new OracleCommand("CDIP.SET_CLIENT_INFO", connection);
                    cmd.CommandType = CommandType.StoredProcedure;
                    AddInputParameter(cmd, "p_u521", OracleDbType.Varchar2, user);
                    cmd.ExecuteNonQuery();

                    using (OracleCommand oracleCommand = new OracleCommand())
                    {
                        oracleCommand.CommandText = query;
                        oracleCommand.Connection = connection;
                        using (
                            OracleDataReader dataReader = oracleCommand.ExecuteReader(CommandBehavior.CloseConnection))
                        {
                            table.Load(dataReader);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                using (StreamWriter writer = new StreamWriter(ConfigurationManager.AppSettings["LogFilePath"]))
                {
                    writer.WriteLine("Error: " + ex.Message);
                }  
            }
            return table;
        }

        private static OracleParameter AddInputParameter(OracleCommand cmd, string paramName, OracleDbType type, object paramValue)
        {
            OracleParameter param = cmd.Parameters.Add(paramName.Trim(), type);
            param.Direction = ParameterDirection.Input;
            param.Value = paramValue;
            return param;
        }
    }
}

Open in new window


Then my class I have not sure if this is the best place to place the methods in a class like this outside the ASMX CS but thought it was fine.
using System.Collections.Generic;
using System.Data;

namespace SpotfireCricitHarness
{
    public static class JsonMethods
    {
        private static List<Dictionary<string, object>>
        RowsToDictionary(DataTable table)
        {
            List<Dictionary<string, object>> objs =
                new List<Dictionary<string, object>>();
            foreach (DataRow dr in table.Rows)
            {
                Dictionary<string, object> drow = new Dictionary<string, object>();
                for (int i = 0; i < table.Columns.Count; i++)
                {
                    drow.Add(table.Columns[i].ColumnName, dr[i]);
                }
                objs.Add(drow);
            }

            return objs;
        }

        public static Dictionary<string, object> ToJson(DataTable table)
        {
            Dictionary<string, object> d = new Dictionary<string, object>();
            d.Add(table.TableName, RowsToDictionary(table));
            return d;
        }

        public static Dictionary<string, object> ToJson(DataSet data)
        {
            Dictionary<string, object> d = new Dictionary<string, object>();
            foreach (DataTable table in data.Tables)
            {
                d.Add(table.TableName, RowsToDictionary(table));
            }
            return d;
        }

    }
}

Open in new window

sbornstein2Asked:
Who is Participating?
 
apeterConnect With a Mentor Commented:
SpotfireCricitHarness.WebService, should actually point a real URL or URI in the web. Try by exeuting with empty namespace.

Hope below url gives you more info.
http://msdn.microsoft.com/en-us/library/system.web.services.webserviceattribute(vs.71).aspx
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.