Solved

Array within Web Service

Posted on 2011-09-25
9
283 Views
Last Modified: 2012-06-27
Writing a basic web service and it works fine for a single row result, but if I wanted multiple results to be outputted, how do I change the response into an array? Or is that not how I would return multiple responses?


<%@ WebService Language="C#" Class="webServices" %>

using System;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.Data.SqlClient;

[WebService(Namespace = "http://dev.domain.com/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]

public class webServices  : System.Web.Services.WebService {
    public struct logResponse
    {
        public string Valid;
        public string Response;
    }
        
    [WebMethod]
    public logResponse lookupCustomer(string username, string password, string customernumber, string customername, string zipcode, string phonenumber, string emailaddress, string ordernumber, string invoicenumber) {
        logResponse cdResult;		
	string cString = System.Web.Configuration.WebConfigurationManager.ConnectionStrings["db"].ConnectionString;
        SqlConnection conn = new SqlConnection(cString);
        SqlCommand comm = new SqlCommand();
        SqlDataReader dr;
        int company = 0;
        cdResult.Valid = "";
        cdResult.Response = "";
        try
        {
            conn.Open();
            comm.CommandType = System.Data.CommandType.StoredProcedure;
            comm.CommandText = "lookupCustomer";
            comm.Connection = conn;
            comm.Parameters.AddWithValue("@username", username);
            comm.Parameters.AddWithValue("@password", password);
            comm.Parameters.AddWithValue("@customernumber", customernumber);
            comm.Parameters.AddWithValue("@customername", customername);
            comm.Parameters.AddWithValue("@zipcode", zipcode);
            comm.Parameters.AddWithValue("@phonenumber", phonenumber);
            comm.Parameters.AddWithValue("@emailaddress", emailaddress);
            comm.Parameters.AddWithValue("@ordernumber", ordernumber);
            comm.Parameters.AddWithValue("@invoicenumber", invoicenumber);
            dr = comm.ExecuteReader(System.Data.CommandBehavior.Default);
            comm.Parameters.Clear();
            
            while (dr.Read()) {
            	cdResult.Response = (string)dr["cm_name"];
            }
            
            return cdResult;
        }
        finally {
        }
    }
}

Open in new window

0
Comment
Question by:ScubeduFan
  • 5
  • 4
9 Comments
 
LVL 21

Accepted Solution

by:
Dale Burrell earned 500 total points
ID: 36596767
public LogResponse[] LookupCustomer()
{
  List<LogResponse> TempResults = new List<LogResponse>();
  ....
  TempResults.Add(new LogResponse(NewValid, NewResponse));
  ....
  return TempResults.ToArray();
}
0
 

Assisted Solution

by:ScubeduFan
ScubeduFan earned 0 total points
ID: 36596794
Dale ... I get the error ...

The type or namespace name 'List' could not be found (are you missing a using directive or an assembly reference?)

on the line ....

List<logResponse> TempResults = new List<logResponse>();


Thanks for your help on this
0
 
LVL 21

Assisted Solution

by:Dale Burrell
Dale Burrell earned 500 total points
ID: 36596828
0
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 

Author Comment

by:ScubeduFan
ID: 36596840
Next error ....

The type 'webServices.logResponse' has no constructors defined
0
 
LVL 21

Assisted Solution

by:Dale Burrell
Dale Burrell earned 500 total points
ID: 36596846
   public struct logResponse
    {
        public string Valid;
        public string Response;

        public logResponse (string Valid, Response)
        {
          this.Valid = Valid;
          this.Response = Response;
        }
    }
       
0
 

Author Comment

by:ScubeduFan
ID: 36596878
I've requested that this question be closed as follows:

Accepted answer: 0 points for ScubeduFan's comment http:/Q_27327019.html#36596794

for the following reason:

Thank You!
0
 

Author Comment

by:ScubeduFan
ID: 36596879
Points not given
0
 
LVL 21

Expert Comment

by:Dale Burrell
ID: 36596880
Um... I think I answered the question? One normally awards the points in this situation.
0
 

Author Closing Comment

by:ScubeduFan
ID: 36890355
Thanks for proving me assistance on this.
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

837 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