0

I have a simple web service which grabs data from database, and i want to return this data as dataset,string,XML. But the web service xml is not getting what type i returned. The question is, is it possible to return as object and webservice XML to display correct tag. at the moment it displays this tag anytype instead of string for example.

here is the method

 [WebMethod]
public Object serviceForPostcode(String postCode, String type)
{
/**
* 
*  create dataset to store query
*  convert dataset to required return type
*  create types for returned object
*  
* 
**/

    SqlDataAdapter adapter = new SqlDataAdapter();
    SqlCommand cmd = new SqlCommand();
    SqlConnection conn = new SqlConnection(dbConnection());
/**
* 
*              -- types--
* 
**/
    XmlDocument xmlD = new XmlDocument();
    StringWriter sw = new StringWriter();
    DataSet dataSet = new DataSet();
/**
* 
* 
**/ 

    // connection and query string
    string dbQuery = "SELECT id, address1, address2, address3, address4, address5, post_code, country FROM paf WHERE (post_code = @postCode)";
    try
    {
        cmd.Parameters.Add(new SqlParameter("@postcode",SqlDbType.NChar, 10)).Value = postCode.ToUpper();
        cmd.CommandText = (dbQuery);
        cmd.CommandType = CommandType.Text;
        cmd.Connection = conn;
        adapter.SelectCommand = cmd;
        adapter.Fill(dataSet,"post codes");

    }
    catch(Exception e)
    {
        System.Web.HttpContext.Current.Response.Write(e.Message);

    }
    finally
    {
       //close database connection
        conn.Close();
    }
    if (type.Equals("xml", StringComparison.InvariantCultureIgnoreCase))
    {
        return dataSet.GetXml();
    }
    else if (type.Equals("string", StringComparison.InvariantCultureIgnoreCase))
    {

        dataSet.WriteXml(sw);
        return sw.ToString(); 
    }
    else if(type.Equals("dataset",StringComparison.InvariantCultureIgnoreCase))
    {
        return "";
    }

        return "error";
}
1
  • So you want one method that can return 3 different types? Commented May 30, 2012 at 14:50

2 Answers 2

1

Why not create three different methods?

public string serviceForPostcodeString(String postCode)

public XElement serviceForPostcodeXML(String postCode)

public DataSet serviceForPostcodeDataSet(String postCode)

Or return always a DataSet and let the consumer convert it to XML or String.

EDIT

One trick would be using generic types, but that won't work on webservices. But you could also wrap your call to the webservice with a generic method on the client side. Something like this:

public T serviceForPostcodeWrapped<T>(String postCode)
{
    // ... do some checking for allowed types here ...
    return (T)serviceForPostcode(postCode, typeof(T).Name);
}

Disclaimer: I said you could, not you should

Sign up to request clarification or add additional context in comments.

1 Comment

Ye i could, i just thought there might be a way of making this work
0

try this:

 [WebMethod]
public string serviceForPostcode(String postCode, String type)

1 Comment

then all objects are type string. i might return xmldocument, this would not work then.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.