Solved

Array within Web Service

Posted on 2011-09-25
9
285 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial

713 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