[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Web Service question namespace

Posted on 2012-08-28
1
Medium Priority
?
629 Views
Last Modified: 2012-08-31
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

0
Comment
Question by:sbornstein2
1 Comment
 
LVL 25

Accepted Solution

by:
apeter earned 2000 total points
ID: 38345450
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

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This tutorial will show you how to add an attribute to an XML (http://en.wikipedia.org/wiki/XML) stream returned from a Windows Communication Foundation (http://en.wikipedia.org/wiki/Windows_Communication_Foundation) (WCF) Web Service.  Some knowled…
Here I am going to explain creating proxies at runtime for WCF Service. So basically we use to generate proxies using Add Service Reference and then giving the Url of the WCF service then generate proxy files at client side. Ok, what if something ge…
This Micro Tutorial will teach you how to add a cinematic look to any film or video out there. There are very few simple steps that you will follow to do so. This will be demonstrated using Adobe Premiere Pro CS6.
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …
Suggested Courses

834 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question