Search code examples
c#asp.netsql-serverweb-serviceswebmethod

Request / Respond web service


I'm new to web services so I could use a little help.

I have a project that a web service will request data from me and I will respond with a web service giving the data. I've created the response web service as you can see below:

Person.cs

using System.Collections.Generic;  
using System.Linq;  
using System.Web;  

namespace TestWebServices  
{  
    public class Person  
    {  
        public string IdNo { get; set; }  
        public string FirstName { get; set; }  
        public string LastName { get; set; }  
    }  
}  

[WebMethod]

[WebMethod(Description = "Return Applicants")]  
publicPerson[] retApplicants(String idno)  
{    
    string connString =  ConfigurationManager.ConnectionStrings["conn"].ConnectionString;
    SqlConnection connection = new SqlConnection(connString);


    SqlCommand command = new SqlCommand("selectApplicant", connection);  
    command.CommandType = System.Data.CommandType.StoredProcedure;  
    command.Parameters.Add("@idno", SqlDbType.VarChar).Value = idno;  
    connection.Open();  
    SqlDataReader reader = command.ExecuteReader();  

    List<Person> persons = new List<Person>();  
    Person persReturned;  

    while (reader.Read())  
    {  
        persReturned = new Person();  
        persReturned.IDNO = reader["IdNo"].ToString();  
        persReturned.FirstName = reader["FirstName"].ToString();  
        persReturned.LastName= reader["LastName"].ToString();  
        persons.Add(persReturned);  
    }  

    return persons.ToArray();  
}  

I tested it on my browser by invoking and it works fine.

How can I make it respond to the requested idno from the other web service?

Thank you in advance.


Solution

  • You have to consume your created Web service. Created one can be added as reference for other project's and through C# code you can consume and get the response. Sample Code Snippet.

            Service1 webService = new Service1();
    
            Console.WriteLine(webService.MyFirstWebMethod(“Bradd”, “Pitt”));
    
            Console.ReadLine();
    

    For more details refer http://www.csharptutorial.in/37/csharp-net-how-to-consume-a-web-service-in-csharp-net-visual-studio-2010