Click here to Skip to main content
12,747,366 members (36,413 online)
Rate this:
Please Sign up or sign in to vote.
See more: C# WebService
I want to create a service which is called by a web server. I am a beginner in this part. So please give me some hints or direction about how should I start my work.
Thank you.
Posted 4-Sep-12 21:08pm
Are you sure you want a Web service? Will WCF service work for you?

1 solution

Rate this: bad
Please Sign up or sign in to vote.

Solution 1

Goto to add new Item from your Project
Add WebService.asmx
one file is added under App_Code folder WebService.cs
Goto That file
Here you have to write which method you want to create..Here i am giving you an example:
[WebService(Namespace = "")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[WebMethod]//added these else code doesn't work
public List<City> GetNews()
    List<City> list = new List<City>();
    string constr = "Data Source=localhost;Initial Catalog=DBname;User Id=sa;Password=lock";
    string query = "SELECT top 20 news_id,news_title FROM news order by news_id DESC";
    SqlDataAdapter da = new SqlDataAdapter(query, con);
    DataSet ds = new DataSet();
    list = ds.Tables[0].AsEnumerable().Select(data => new City() { Id = (int)data["news_id"], title = (string)data["news_title"] }).ToList();
    return list;
public class City
    public int Id { get; set; }
    public string title { get; set; }

Now i have called these web service from another page like this way
<script type="text/javascript" src=""></script>
<script type="text/javascript">
    $(document).ready(function () {
            type: "POST",
            url: "WebService.asmx/GetNews",
            data: "{}",
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: function (response) {
                var cars = response.d;
                $.each(cars, function (index, car) {
                    var url = "Mobile.aspx?id=" + car.Id;
                    $('#output4').append('<li><a href="' + url + '">' + car.title + '</a></li>');
                $('#output4').append('<li style="color:Red; font-size:small; text-align:right"><a href="AllMobiles.aspx">view more</a></li>');
            failure: function (msg) {

If you still having problem feel free to ask...always here to support...

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

    Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Mobile
Web01 | 2.8.170215.1 | Last Updated 5 Sep 2012
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100