Subscribe to Subscribers

Resources » Code Snippets » ASP.NET WebForms

Webservice using Generic Collections


Posted Date:     Category: ASP.NET WebForms    
Author: Member Level: Gold    Points: 25


Below code is a Example for Webservice using Generic Collections. Advantage of using Generic Collections is it lets client applications bind directly to the output.Generic list is the one of the way to return more than one value from webservices.



This is a ASP.NET Webservice using C#, List and SQL Server 2008.

This code returns the country, capital and country code against a given Country Name from database. So you should create a SQL connection in the webservice and then open the connection to work on database. Store the data in a reader. Create a object of type CountryDetails List. Then add the values to the List object. Then we get Generic List of a class CountryDetails type that returns three variables as output of the webservice.


using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Services;
using System.Data.SqlClient;

namespace Webservice
{
[WebService(Namespace = "http://Your_URL.in/",Description="Country capital and their codes",Name="Country Details")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]

public class WebService1 : System.Web.Services.WebService
{
SqlConnection con = new SqlConnection("Connection String");
public class CountryDetails
{
public string country;
public string capital;
public int Code;
}

[WebMethod]
public List country(string Country)
{
string sqlQuery = "select * from country where Country_Name='"+Country+"' ";
con.Open();
SqlCommand com = new SqlCommand(sqlQuery, con);
SqlDataReader read = com.ExecuteReader();
var stri = new List();
read.Read();

while (read.HasRows)
{
var s = new CountryDetails();
s.Code = Convert.ToInt32(read["Country_Code"].ToString());
s.capital = read["Capital"].ToString();
s.country = read["Country_Name"].ToString();
stri.Add(s);
while (read.Read())
{
var s1 = new CountryDetails();
s1.Code = Convert.ToInt32(read["Country_Code"].ToString());
s1.capital = read["Capital"].ToString();
s1.country = read["Country_Name"].ToString();
stri.Add(s1);
}
read.NextResult();
}
return stri.ToList();
}
}
}





Did you like this resource? Share it with your friends and show your love!


Responses to "Webservice using Generic Collections"

No responses found. Be the first to respond...

Feedbacks      

Post Comment:




  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:   Sign In to fill automatically.
    Email: (Will not be published, but required to validate comment)



    Type the numbers and letters shown on the left.


    Next Resource: Webservice using XML
    Previous Resource: How to import bulk data from excel sheet to SQL Server?
    Return to Resources
    Post New Resource
    Category: ASP.NET WebForms


    Post resources and earn money!
     
    More Resources
    Popular Tags   Tag posting guidelines   Search Tags  
    Webservice example  .  List in weservice  .  

    Active Members
    TodayLast 7 Daysmore...

    Awards & Gifts
    Talk to Webmaster Tony John
    Copyright © SpiderWorks Technologies Pvt Ltd., Kochi, India
    2005 - 2013 All Rights Reserved.
    .NET and other trademarks mentioned in this site belong to Microsoft and other respective trademark owners.
    Articles, tutorials and all other content offered here is for educational purpose only.
    We are not associated with Microsoft or its partners.