Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

This question is an exact duplicate of:

I am trying to create a webservice which will return more than 1 string. it will return 4 strings. I didt webservices before and I used to return only true or false values. but now I need more data.

here is my webservice.

[WebMethod]
    public string get_currency(string date, string cur_code) {
        string rtn = "";
        try
        {
            using (SqlConnection conn = new SqlConnection("Data Source=xxx-xxxxx;Initial Catalog=xxx;Persist Security Info=True;User ID=xxx;Password=xxx"))
            {
                string selectSql = "select date,code,banknote_buying,banknote_selling from Currencies where date = '" + date + "' and code ='" + cur_code + "'";
                SqlCommand comm = new SqlCommand(selectSql, conn);
                conn.Open();
                SqlDataReader dr = comm.ExecuteReader();
                if (dr.Read()) {
                    rtn = dr["date"].ToString() +  dr["code"].ToString() +  dr["banknote_buying"].ToString() + dr["banknote_selling"].ToString();
                }
            }
        }
        catch (Exception ex)
        {
            return "Fail";
        }

        return rtn;
    }

How can I do return them as a proper SOAP object

share|improve this question
    
Return a string[]. –  kostas ch. Jun 14 '13 at 12:21
1  
Delimit the strings with a special marker, return an array of strings, build a composite type with 4 string properties, ect. Lots of options. What have you tried? –  asawyer Jun 14 '13 at 12:21
    
this is what I tried above, @asawyer. I asking how to send it as a proper soap object –  ayilmaz Jun 14 '13 at 12:23
1  
Anything that can be serialized in a soap envelope is a "proper soap object" You also have a glaring sql injection attack vulnerability. –  asawyer Jun 14 '13 at 12:25
add comment

marked as duplicate by CodeCaster, asawyer, nvoigt, JMK, vorrtex Jun 16 '13 at 10:13

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

2 Answers

up vote 1 down vote accepted

I have done what I wanted at last. here is my code.

    [WebMethod]
    public Currency_Object get_currency(string date, string cur_code) {

        Currency_Object co = new Currency_Object();
        try
        {
            using (SqlConnection conn = new SqlConnection("Data Source=xxx-xxx;Initial Catalog=MB_DB;Persist Security Info=True;User ID=xxx;Password=xxx"))
            {
                string selectSql = "select date,code,banknote_buying,banknote_selling from Currencies where date = '" + date + "' and code ='" + cur_code + "'";
                SqlCommand comm = new SqlCommand(selectSql, conn);
                conn.Open();
                SqlDataReader dr = comm.ExecuteReader();

                if (dr.Read()) {
                    co.date_ = dr["date"].ToString();
                    co.code_ = dr["code"].ToString();
                    co.banknote_buying_ = dr["banknote_buying"].ToString();
                    co.banknote_selling_ = dr["banknote_selling"].ToString();
                }
            }
        }
        catch (Exception ex) { return null; }
        return co;
    }
}

and this is what is returned

 <Currency_Object xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://tempuri.org/">
  <date>06.06.2013 00:00:00</date>
  <code>USD</code>
  <banknote_buying>1,8847</banknote_buying>
  <banknote_selling>1,8922</banknote_selling>
 </Currency_Object>
share|improve this answer
1  
I'm happy for you, but please find time to address the sql injection issues you have here. It could be as simple as just parameterizing the command text. You will also want either some kind of error logging, or for Currency_Object to have the ability to report exceptions to the caller as part of the soap message. –  asawyer Jun 14 '13 at 12:48
add comment

Try to return string[]

[WebMethod]
public **string[]** get_currency(string date, string cur_code) {
    **List<string> rtn = new List<string> ();**
    try
    {
        using (SqlConnection conn = new SqlConnection("Data Source=xxx-xxxxx;Initial Catalog=xxx;Persist Security Info=True;User ID=xxx;Password=xxx"))
        {
            string selectSql = "select date,code,banknote_buying,banknote_selling from Currencies where date = '" + date + "' and code ='" + cur_code + "'";
            SqlCommand comm = new SqlCommand(selectSql, conn);
            conn.Open();
            SqlDataReader dr = comm.ExecuteReader();
            if (dr.Read()) {
                rtn.Add( dr["date"].ToString());
                rtn.Add(dr["code"].ToString());
                 rtn.Add(dr["banknote_buying"].ToString());
                 rtn.Add(dr["banknote_selling"].ToString());
            }
        }
    }
    catch (Exception ex)
    {
        rtn.Add("Fail");
    }

    return rtn.ToArray();
}
share|improve this answer
add comment

Not the answer you're looking for? Browse other questions tagged or ask your own question.